MySQL高可用之MHA搭建

测试环境

节点1 172.16.200.231 6666               master        
节点2 172.16.200.27 6666 slave1                       
节点3 172.16.200.60 5200   slave2       

 

 

安装软件包

节点3:安装MHA-manager

wget http://www.mysql.gr.jp/frame/modules/bwiki/index.php?plugin=attach&pcmd=open&file=mha4mysql-manager-0.56-0.el6.noarch.rpm&refer=matsunob

依赖包

yum -y install perl-DBD-MySQL

所有节点:安装MHA-note

wget http://www.mysql.gr.jp/frame/modules/bwiki/index.php?plugin=attach&pcmd=open&file=mha4mysql-manager-0.56-0.el6.noarch.rpm&refer=matsunobu

依赖包

yum -y install perl-DBD-MySQL
yum -y install perl-Config-Tiny
yum -y install perl-Log-Dispatch
yum -y install perl-Parallel-ForkManager    

 

 

配置

[root@python /]# cat /etc/mha/test.cnf

[server default] # working directory on the manager manager_workdir
=/var/log/masterha/test # manager log file manager_log=/var/log/masterha/test/test.log # working directory on MySQL servers remote_workdir=/var/log/masterha/test master_binlog_dir=/data0/mysql/6666_test repl_user=repl repl_password='repl' ssh_user=root [server1] hostname=172.16.200.231 port=6666 user=root password='' [server2] hostname=172.16.200.60 port=5200 user=root password='' [server3] hostname=172.16.200.27 port=6666 user=root password=''

注:gtid模式可以设置[binlog1]模块,此处已开启gtid模式但是没有设置binlog server

Requirement

  • SSH认证
  • candidate masters要开启log-bin
  • 关闭自动清理relay log的选项,改为定期使用MHA note提供的脚本purge_relay_logs手动进行

         0 5 * * * app /usr/bin/purge_relay_logs --user=root --password=PASSWORD --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1

  • Do not use LOAD DATA INFILE with Statement Based Binary Logging

         If you want to use LOAD DATA, SET sql_log_bin=0; LOAD DATA … ; SET sql_log_bin=1; is more recommended approach.

  • 都需要有复制账号

验证

masterha_check_ssh --conf=/etc/mha/test.cnf
masterha_check_repl --conf=/etc/mha/test.cnf

注:通过打印perl代码可以看出每台主机先ssh到自己再ssh到其他几点上进行验证,所以除了各节点之间进行秘钥配置外,还需要各自对自己进行秘钥设置

检查通过之后启动manager

masterha_manager --conf=/etc/mha/test.cnf  

查看manager状态,已正常启动,实时监测各节点

masterha_check_status --conf=/etc/mha/test.cnf 
test (pid:20483) is running(0:PING_OK), master:172.16.200.231

 

 

开始演练测试

手动failover,需要先关闭manager 监控

masterha_stop --conf=/etc/mha/test.cnf 
masterha_master_switch --master_state=dead --conf=/etc/mha/test.cnf 

 

 

过程:

  • 检查是否存在全局配置文件/etc/masterha_default.cnf
  • 读取指定的配置文件,监测各节点的数据库状态以及GTID是否开启
  • FLUSH NO_WRITE_TO_BINLOG TABLES
  • 检查复制状态
  • 检查配置文件中是否指定candidate masters
  • 如果没有指定,则将应用日志最新的slave提升为master
  • old master上执行FLUSH TABLES WITH READ LOCK
  • 获得old master上的当前的binlog文件号和position
  • new master应用中继日志到和old master数据一致
  • 其他的slave应用所有的中继日志
  • 其 他的slave指向new master:CHANGE MASTER TO MASTER_HOST='172.16.200.60', MASTER_PORT=5200, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='xxx';
原文地址:https://www.cnblogs.com/Bccd/p/5802571.html