# mydb1 mydb2grant all privileges on *.* to 'root'@'localhost' identified by 'root123' with grant option;grant all privileges on *.* to 'root'@'127.0.0.1' identified by 'root123' with grant option;grant all privileges on *.* to 'root'@'10.245%' identified by 'root123' with grant option;grant replication slave on *.* to rep@'10.245.231.202' identified by 'rep123';grant replication slave on *.* to rep@'10.245.231.203' identified by 'rep123';# mydb1show master status\G;*************************** 1. row *************************** File: binlog.000005 Position: 540 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)# mydb2CHANGE MASTER TO MASTER_HOST='10.245.231.202', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='rep123', MASTER_LOG_FILE='binlog.000005', MASTER_LOG_POS=540;start slave;show slave status\G;
/usr/local/bin/masterha_check_ssh --conf=/u01/mha/etc/app.cnf/usr/local/bin/masterha_check_repl --conf=/u01/mha/etc/app.cnf/usr/local/bin/masterha_manager --conf=/u01/mha/etc/app.cnf &/usr/local/bin/masterha_check_status --conf=/u01/mha/etc/app.cnf/usr/local/bin/masterha_stop --conf=/u01/mha/etc/app.cnf# 检测ssh等价性配置/usr/local/bin/masterha_check_ssh --conf=/u01/mha/etc/app.cnf#----------------------------------------------------------Sat Apr 811:42:09 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.Sat Apr 811:42:09 2017 - [info] Reading application default configuration from /u01/mha/etc/app.cnf..Sat Apr 811:42:09 2017 - [info] Reading server configuration from /u01/mha/etc/app.cnf..Sat Apr 811:42:09 2017 - [info] Starting SSH connection tests..Sat Apr 811:42:102017 - [debug] Sat Apr 811:42:09 2017 - [debug] Connecting via SSH from root@mydb1(10.245.231.202:22) to root@mydb2(10.245.231.203:22)..Sat Apr 811:42:102017 - [debug] ok.Sat Apr 811:42:102017 - [debug] Sat Apr 811:42:102017 - [debug] Connecting via SSH from root@mydb2(10.245.231.203:22) to root@mydb1(10.245.231.202:22)..Sat Apr 811:42:102017 - [debug] ok.Sat Apr 811:42:102017 - [info] All SSH connection tests passed successfully. # successfully代表SSH等价性配置成功#----------------------------------------------------------# 检测MHA/usr/local/bin/masterha_check_repl --conf=/u01/mha/etc/app.cnf## 检测问题和解决办法#----------------------------------------------------------# 问题1:Sat Apr 811:44:152017 - [info] read_only=1 is not set on slave mydb2(10.245.231.203:3306).Sat Apr 811:44:152017 - [warning] relay_log_purge=0 is not set on slave mydb2(10.245.231.203:3306)# 问题2:Can notexec"mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 106.mysqlbinlog version command failed with rc 1:0, please verify PATH, LD_LIBRARY_PATH, and client options at /usr/local/bin/apply_diff_relay_logs line 493# 问题3:Sat Apr 811:44:172017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln205] Slaves settings check failed!Sat Apr 811:44:172017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln413] Slave configuration failed.Sat Apr 811:44:172017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations. at /usr/local/bin/masterha_check_repl line 48Sat Apr 811:44:172017 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln523] Error happened on monitoring servers.....MySQL Replication Health is NOT OK! # NOT OK,MHA有问题需要解决,以下是解决步骤#----------------------------------------------------------# mydb2set global read_only=1; set global relay_log_purge=0;# mydb1/mydb2ln -s /u01/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlogln -s /u01/mysql/bin/mysql /usr/bin/mysql# 解决问题后再检测时状态是OK#----------------------------------------------------------Sat Apr 813:39:332017 - [info] OK.Sat Apr 813:39:332017 - [warning] shutdown_script is not defined.Sat Apr 813:39:332017 - [info] Got exit code 0 (Not master dead).MySQL Replication Health is OK.#----------------------------------------------------------# 启动MHA后查看状态/usr/local/bin/masterha_manager --conf=/u01/mha/etc/app.cnf &/usr/local/bin/masterha_check_status --conf=/u01/mha/etc/app.cnf#----------------------------------------------------------app (pid:10163) is running(0:PING_OK), master:mydb1#----------------------------------------------------------/usr/local/bin/masterha_stop --conf=/u01/mha/etc/app.cnf
MHA failover故障切换
模拟主库宕机
ssh mydb1 "killall -r mysqld"
查看管理节点日志,可以看到VIP已经漂移
cat /u01/mha/log/manager.log |grep -i vip#----------------------------------------------------------Disabling the VIP on old master: mydb1 Enabling the VIP - 10.245.231.204/24 on the new master - mydb2 #----------------------------------------------------------
tail /u01/mha/log/manager.log#----------------------------------------------------------Started automated(non-interactive) failover.Invalidated master IP address on mydb1(10.245.231.202:3306)The latest slave mydb2(10.245.231.203:3306) has all relay logs for recovery.Selected mydb2(10.245.231.203:3306) as a new master.mydb2(10.245.231.203:3306): OK: Applying all logs succeeded.mydb2(10.245.231.203:3306): OK: Activated master IP address.Generating relay diff files from the latest slave succeeded.mydb2(10.245.231.203:3306): Resetting slave info succeeded.Master failover to mydb2(10.245.231.203:3306) completed successfully.#----------------------------------------------------------
new master(old slave)
show master status\G*************************** 1. row *************************** File: binlog.000007 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
new slave(old:master)
# 打开MySQLmysqld_safe --defaults-file=/u01/conf/my3306.cnf &mysql -uroot -proot123 --socket=/u01/run/3306/mysql.sock# 检查数据库mysql> show master status\G*************************** 1. row *************************** File: binlog.000013 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)mysql> show slave status\GEmpty set (0.00 sec)# 在管理节点日志中查主库的日志文件和位置cat /u01/mha/log/manager.log |grep -i change#----------------------------------------------------------Sat Apr 814:55:242017 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='mydb2 or 10.245.231.203', MASTER_PORT=3306, MASTER_LOG_FILE='binlog.000007', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx';#----------------------------------------------------------# 在slave连接masterCHANGE MASTER TO MASTER_HOST='10.245.231.203', MASTER_PORT=3306, MASTER_LOG_FILE='binlog.000007', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='rep123';start slave;
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是日本的一位MySQL专家采用Perl语言编写的一个脚本管理工具,目的在于维持MySQL Replication中Master库的高可用性,其最大特点是可以修复多个Slave之间的差异日志,最终使所有Slave保持数据一致,然后从中选择一个充当新的Master,并将其它Slave指向它。