分类: Mysql/postgreSQL
2017-11-02 16:25:17
1. 环境说明
本文所介绍的部署方案基于如下环境:
? 版本:mysql-5.6.26-linux-glibc2.5-x86_64
? MHA节点:192.168.10.61、192.168.10.62、192.168.10.63
? MHA 管理节点:192.168.10.63
后续的介绍基于三台MySQL服务器已经搭建部署完成。
2. MySQL环境准备
mysql> grant replication slave on *.* to 'repadmin'@'%' identified by 'Password'; flush privileges;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
grant all on *.* to 'root'@'192.168.10.61' identified by 'Password'; flush privileges;
grant all on *.* to 'root'@'192.168.10.62' identified by 'Password'; flush privileges;
grant all on *.* to 'root'@'192.168.10.63' identified by 'Password'; flush privileges;
mysql> change master to master_host='192.168.10.61',master_port=3306,master_user='repadmin', master_password='Password', master_log_file='mysql-bin.000006',master_log_pos=2111;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
重点查看如下信息是否正常:
? Slave_IO_Running: Yes
? Slave_SQL_Running: Yes
? Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
3. MHA配置
vim /etc/hosts
192.168.10.61 uafetbmasterdb01.uaf.com.cn uafetbmasterdb01
192.168.10.62 uafetbmasterdb02.uaf.com.cn uafetbmasterdb02
192.168.10.63 uafetbslavedb01.uaf.com.cn uafetbslavedb01
注:以上操作在所有节点进行
[root@uafetbslavedb01 mysql-01]# ssh-keygen -t rsa
[root@uafetbslavedb01 mysql-01]# ssh-copy-id -i .ssh/id_rsa.pub “-p 32123 root@192.168.10.61”
[root@uafetbslavedb01 mysql-01]# ssh-copy-id -i .ssh/id_rsa.pub “-p 32123 root@192.168.10.62”
[root@uafetbslavedb01 mysql-01]# ssh-copy-id -i .ssh/id_rsa.pub “-p 32123 root@192.168.10.63”
[root@uafetbslavedb01 mysql-01]# ssh-keygen -t rsa
注:以上操作在所有节点进行
[root@uafetbmasterdb01 ~]$ ssh 192.168.10.62 -p 32123
Last login: Mon Sep 21 18:51:23 2015 from 168.33.5.112
[root@uatetbmasterdb02 ~]$ ssh 192.168.10.63 -p 32123
Last login: Mon Sep 21 18:51:25 2015 from 168.33.5.112
[root@uafetbslavedb03 ~]$ ssh 192.168.10.61 -p 32123
Last login: Mon Sep 21 18:13:40 2015 from 168.33.5.112
yum -y install perl-DBI perl-DBD-MySQL
rpm -ivh /tmp/mha4mysql-node-0.54-0.el6.noarch.rpm
[root@uafetbslavedb01 tmp]$ sudo yum –y install perl-Params-Validate
[root@uafetbslavedb01 tmp]$ sudo rpm -ivh perl-Config-Tiny-2.12-1.el6.rfx.noarch.rpm
[root@uafetbslavedb01 tmp]$ sudo rpm -ivh perl-Time-HiRes-1.9724-1.el6.rfx.x86_64.rpm
[root@uafetbslavedb01 tmp]$ sudo rpm -ivh perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm --nodeps
[root@uafetbslavedb01 tmp]$ sudo rpm -ivh perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
[root@uafetbslavedb01 tmp]$ sudo rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/app1.log
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/app1.log
remote_workdir=/var/log/masterha/app1
user=root
password=Password
ssh_user=root
repl_user=repadmin
repl_password=Password
#ping_interval=1
#shutdown_script=""
#master_ip_failover_script=/usr/local/bin/master_ip_failover
#master_ip_online_change_script=/usr/local/bin/master_ip_online_change_script
#report_script=""
[server1]
hostname=192.168.10.61
master_binlog_dir=/Mydata/mysql-01/
ssh_port=32123
candidate_master=1
[server2]
hostname=192.168.10.62
master_binlog_dir=/Mydata/mysql-01/
ssh_port=32123
candidate_master=1
[server3]
hostname=192.168.10.63
master_binlog_dir=/Mydata/mysql-01/
ssh_port=32123
no_master=1
4. 配置测试
? 测试ssh免密码认证是否配置正常:
masterha_check_ssh --conf=/etc/masterha/app1.cnf
? 测试MySQL服务器之间的主从是否正常:
masterha_check_repl --conf=/etc/masterha/app1.cnf
5. 切换测试
master:61,backup:62,slave(MHA manager):63
在/etc/masterha/两个conf文件,app1.cnf(对应初始状态),app2.cnf(对应切换后的状态)
l 正常情况
所有节点均正常,且通过“masterha_check_repl”检测正常
l master:61——>62
masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.10.62 --orig_master_is_new_slave
l master:62——>61
masterha_master_switch --conf=/etc/masterha/app2.cnf --master_state=alive --new_master_host=192.168.10.61 --orig_master_is_new_slave
l 参数说明
master_state=alive,表示当前的主状态正常,为存活状态
orig_master_is_new_slave,表示切换后,当前主自动更改为新主的从服务器
l 异常情况
61异常宕机或者mysql服务停止
l 手动切换:61——>62
masterha_master_switch --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.10.61 --master_state=dead --new_master_host=192.168.10.62 --ignore_last_failover
l 参数说明:
dead_master_host,出现异常的当前主
new_master_host,状态正常的新主
master_state=dead,申明当前主已经退出服务
l 自动切换:61——>62
在63运行nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log&1 &
master自动切换到62
将61服务重启,数据同步后
将62作为61的主:change master to master_host='192.168.10.62',master_port=3306,master_user='repadmin',master_password=password',master_log_file='mysql-bin.000004',master_log_pos=120;
检查主从复制正常:masterha_check_repl --conf=/etc/masterha/app2.cnf
确认状态后进行切换:masterha_master_switch --conf=/etc/masterha/app2.cnf --master_state=alive --new_master_host=192.168.10.61 --orig_master_is_new_slave