mysql高可用方案之MHA
环境规划:节点说明 主机名 IP地址
管理节点 tong3 192.168.1.249
主节点 tong2 192.168.1.248
主节点 tong1 192.168.1.247
mysql dba技术群 378190849
武汉-linux运维群 236415619
1.网络和主机名配置
设置每个主机的IP地址和/etc/hosts文件互相解析
# cat /etc/hosts
192.168.1.247 tong1
192.168.1.248 tong2
192.168.1.249 tong3
# ping tong1 -c1 --网络必须ping通
PING tong1 (192.168.1.247) 56(84) bytes of data.
64 bytes from localhost (192.168.1.247): icmp_seq=1 ttl=64 time=0.021 ms
--- tong1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.021/0.021/0.021/0.000 ms
# ping tong2 -c1
PING tong2 (192.168.1.248) 56(84) bytes of data.
64 bytes from tong2 (192.168.1.248): icmp_seq=1 ttl=64 time=0.109 ms
--- tong2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.109/0.109/0.109/0.000 ms
# ping tong3 -c1
PING tong3 (192.168.1.249) 56(84) bytes of data.
64 bytes from tong3 (192.168.1.249): icmp_seq=1 ttl=64 time=0.124 ms
--- tong3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.124/0.124/0.124/0.000 ms
#
2.安装mha管理软件mha manager
tong3管理节点:
# yum installperl-DBD-MySQL cpan --安装perl工具
# tar xvf mha4mysql-manager-0.53.tar.gz-C /usr/local/
# cd /usr/local/mha4mysql-manager-0.53/
# perl Makefile.PL
# echo $?
0
# make && make install
# echo $?
0
#
3.在各数据节点安装mha node
# tar xvf mha4mysql-node-0.53.tar.gz
# cd mha4mysql-node-0.53
# yum install perl-DBD-mysql cpan -y
# perl Makefile.PL
# make && make install
#echo $?
0
#
4.各节点ssh互相信任
# ssh-keygen-t dsa
# cd .ssh
# cat> # scp 192.168.1.247:/root/.ssh/id_dsa.pub 247
# scp 192.168.1.248:/root/.ssh/id_dsa.pub 248
# cat 248247 >> authorized_keys
# scp authorized_keys 192.168.1.248:/root/.ssh/
authorized_keys 100% 1800 1.8KB/s 00:00
# scp authorized_keys 192.168.1.247:/root/.ssh/
root@192.168.1.247's password:
authorized_keys 100% 1800 1.8KB/s 00:00
# ssh tong1 date
Tue Apr 28 12:57:02 CST 2015
# ssh tong2 date
Tue Apr 28 12:59:57 CST 2015
# ssh tong3 date
Tue Apr 28 12:57:25 CST 2015
#
5.在管理节点编辑配置文件
# mkdir /etc/mysqlmha
# cd /etc/mysqlmha/
# cp -a /usr/local/mha4mysql-manager-0.53/samples/* .
# vim conf/app1.cnf
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
user=root1 --远程登陆用户
password=system
ssh_user=root
repl_user=repl_user --复制用户
repl_password=system!#%246
ping_interval=1 --心跳检测
hostname=192.168.1.249
master_binlog_dir="/usr/local/mysql-5.6.23/data/"
# candidate_master=1
no_master=1 --不能切换成主数据库
hostname=192.168.1.248
master_binlog_dir="/usr/local/mysql-5.6.23/data/" --二进制日志文件存放
candidate_master=1 --可以切换成主数据库
hostname=192.168.1.247
master_binlog_dir="/usr/local/mysql-5.6.23/data/"
candidate_master=1 -可以切换成主数据库
# masterha_check_ssh --conf=/etc/mysqlmha/conf/app1.cnf
Tue Apr 28 15:39:21 2015 - Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Apr 28 15:39:21 2015 - Reading application default configurations from /etc/mysqlmha/conf/app1.cnf..
Tue Apr 28 15:39:21 2015 - Reading server configurations from /etc/mysqlmha/conf/app1.cnf..
Tue Apr 28 15:39:21 2015 - Starting SSH connection tests..
Tue Apr 28 15:39:22 2015 -
Tue Apr 28 15:39:21 2015 - Connecting via SSH from root@192.168.1.249(192.168.1.249:22) to root@192.168.1.248(192.168.1.248:22)..
Tue Apr 28 15:39:22 2015 - ok.
Tue Apr 28 15:39:22 2015 - Connecting via SSH from root@192.168.1.249(192.168.1.249:22) to root@192.168.1.247(192.168.1.247:22)..
Tue Apr 28 15:39:22 2015 - ok.
Tue Apr 28 15:39:23 2015 -
Tue Apr 28 15:39:22 2015 - Connecting via SSH from root@192.168.1.248(192.168.1.248:22) to root@192.168.1.249(192.168.1.249:22)..
Tue Apr 28 15:39:22 2015 - ok.
Tue Apr 28 15:39:22 2015 - Connecting via SSH from root@192.168.1.248(192.168.1.248:22) to root@192.168.1.247(192.168.1.247:22)..
Tue Apr 28 15:39:23 2015 - ok.
Tue Apr 28 15:39:24 2015 -
Tue Apr 28 15:39:22 2015 - Connecting via SSH from root@192.168.1.247(192.168.1.247:22) to root@192.168.1.249(192.168.1.249:22)..
Tue Apr 28 15:39:23 2015 - ok.
Tue Apr 28 15:39:23 2015 - Connecting via SSH from root@192.168.1.247(192.168.1.247:22) to root@192.168.1.248(192.168.1.248:22)..
Tue Apr 28 15:39:23 2015 - ok.
Tue Apr 28 15:39:24 2015 - All SSH connection tests passed successfully.
#
6.修改数据库配置文件和创建用户
在所有节点创建相同的用户:
mysql> grant all privileges on *.* to root1@'192.168.1.%'>
mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.%'> tong1节点:
# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 20
socket = /tmp/mysql.sock
replicate-do-db=tong
replicate-ignore-db=mysql
log-bin=mysql-bin
log-bin-index=mysql-bin-index
auto_increment_offset=1
auto_increment_increment=2
relay_log_purge=0
read-only=1
#
tong2节点:
# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 10
socket = /tmp/mysql.sock
replicate-do-db=tong
replicate-ignore-db=mysql
log-bin=mysql-bin
log-bin-index=mysql-bin-index
auto_increment_offset=2
auto_increment_increment=2
read-only=1
relay_log_purge=0
#
tong3节点:
# vim /etc/my.cnf
basedir = /usr/local/mysql-5.6.23
datadir = /usr/local/mysql-5.6.23/data
port = 3306
server_id = 30
socket = /tmp/mysql.sock
replicate-do-db=tong
replicate-ignore-db=mysql
#
7.将tong1和tong2搭建为主主模式
tong1主机:
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection> Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>change master to master_host='192.168.1.248',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000010',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.45 sec)
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql>
tong1主机:
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection> Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.1.247',master_port=3306,master_user='repl_user',master_password='system!#%246',master_log_file='mysql-bin.000010',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.45 sec)
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql>
8.检查复制是否有错
# masterha_check_repl --conf=/etc/mysqlmha/conf/app1.cnf
Tue Apr 28 15:53:23 2015 - Checking replication health on 192.168.1.249..
Tue Apr 28 15:53:23 2015 - ok.
Tue Apr 28 15:53:23 2015 - Checking replication health on 192.168.1.248..
Tue Apr 28 15:53:23 2015 - ok.
Tue Apr 28 15:53:23 2015 - master_ip_failover_script is not defined.
Tue Apr 28 15:53:23 2015 - shutdown_script is not defined.
Tue Apr 28 15:53:23 2015 - Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
# nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf>> /tmp/mha_manager 2>&1 &
# jobs
+Running nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf >> /tmp/mha_manager 2>&1 &
# masterha_check_status --conf=/etc/mysqlmha/conf/app1.cnf
app1 (pid:24330) is running(0:PING_OK), master:192.168.1.247 --此时主节点在247服务器上
#
9.故障测试
tong1节点:
# /etc/init.d/mysqld stop --停掉tong1节点的数据库
Shutting down MySQL............ SUCCESS!
# /etc/init.d/mysqld start --节点必须启动才能做切换
Starting MySQL. SUCCESS!
#
查看tong2主机的日志状态,将tong1节点必须要同步到tong2节点上才可以切换(change master to master_host='192.168.1.248,master_ ....................)
tong3节点:
# rm -rf /var/log/masterha/app1/app1.failover.complete
+Done nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf >> /tmp/mha_manager 2>&1 --必须删除管理节点的监控文件
# nohup masterha_manager --conf=/etc/mysqlmha/conf/app1.cnf>> /tmp/mha_manager 2>&1 &
# masterha_check_status --conf=/etc/mysqlmha/conf/app1.cnf
app1 (pid:27870) is running(0:PING_OK), master:192.168.1.248
#
页:
[1]