当前位置:   article > 正文

MySQL高可用架构之MHA_mysql mha 和consule

mysql mha 和consule

简介:

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA主要支持一主多从的架构要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。

我们自己使用其实也可以使用1主1从,但是master主机宕机后无法切换,以及无法补全binlog。master的mysqld进程crash后,还是可以切换成功,以及补全binlog的。

官方介绍:https://code.google.com/p/mysql-master-ha/

图01展示了如何通过MHA Manager管理多组主从复制。可以将MHA工作原理总结为如下:

 

                                 ( 图01 )

(1)从宕机崩溃的master保存二进制日志事件(binlog events);

(2)识别含有最新更新的slave;

(3)应用差异的中继日志(relay log)到其他的slave;

(4)应用从master保存的二进制日志事件(binlog events);

(5)提升一个slave为新的master;

(6)使其他的slave连接新的master进行复制;

MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下。

Manager工具包主要包括以下几个工具:

  1. masterha_check_ssh 检查MHA的SSH配置状况
  2. masterha_check_repl 检查MySQL复制状况
  3. masterha_manger 启动MHA
  4. masterha_check_status 检测当前MHA运行状态
  5. masterha_master_monitor 检测master是否宕机
  6. masterha_master_switch 控制故障转移(自动或者手动)
  7. masterha_conf_host 添加或删除配置的server信息

Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具: 

  1. save_binary_logs 保存和复制master的二进制日志
  2. apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
  3. filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
  4. purge_relay_logs 清除中继日志(不会阻塞SQL线程)

注意:

为了尽可能的减少主库硬件损坏宕机造成的数据丢失,因此在配置MHA的同时建议配置成MySQL 5.5的半同步复制。关于半同步复制原理各位自己进行查阅。(不是必须)

1.部署MHA

接下来部署MHA,具体的搭建环境如下(所有操作系统均为centos 6.2 64bit,不是必须,server03和server04是server02的从,复制环境搭建后面会简单演示,但是相关的安全复制不会详细说明,需要的童鞋请参考前面的文章,MySQL Replication需要注意的问题):

  1. 角色 ip地址 主机名 server_id 类型
  2. Monitor host 192.168.0.20 server01 - 监控复制组
  3. Master 192.168.0.50 server02 1 写入
  4. Candicate master 192.168.0.60 server03 2
  5. Slave 192.168.0.70 server04 3

其中master对外提供写服务,备选master(实际的slave,主机名server03)提供读服务,slave也提供相关的读服务,一旦master宕机,将会把备选master提升为新的master,slave指向新的master

(1)在所有节点安装MHA node所需的perl模块(DBD:mysql),安装脚本如下:

  1. [root@192.168.0.50 ~]# cat install.sh
  2. #!/bin/bash
  3. wget http://xrl.us/cpanm --no-check-certificate
  4. mv cpanm /usr/bin
  5. chmod 755 /usr/bin/cpanm
  6. cat > /root/list << EOF
  7. install DBD::mysql
  8. EOF
  9. for package in `cat /root/list`
  10. do
  11. cpanm $package
  12. done
  13. [root@192.168.0.50 ~]#

如果有安装epel源,也可以使用yum安装 

yum install perl-DBD-MySQL -y

(2)在所有的节点安装mha node: 

  1. wget http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.53.tar.gz
  2. tar xf mha4mysql-node-0.53.tar.gz
  3. cd mha4mysql-node-0.53
  4. perl Makefile.PL
  5. make && make install

安装完成后会在/usr/local/bin目录下生成以下脚本文件:

  1. [root@192.168.0.50 bin]# pwd
  2. /usr/local/bin
  3. [root@192.168.0.50 bin]# ll
  4. total 40
  5. -r-xr-xr-x 1 root root 15498 Apr 20 10:05 apply_diff_relay_logs
  6. -r-xr-xr-x 1 root root 4807 Apr 20 10:05 filter_mysqlbinlog
  7. -r-xr-xr-x 1 root root 7401 Apr 20 10:05 purge_relay_logs
  8. -r-xr-xr-x 1 root root 7263 Apr 20 10:05 save_binary_logs
  9. [root@192.168.0.50 bin]#

关于上面脚本的功能,上面已经介绍过了,这里不再重复了。

2.安装MHA Manager

MHA Manager中主要包括了几个管理员的命令行工具,例如master_manger,master_master_switch等。MHA Manger也依赖于perl模块,具体如下:

(1)安装MHA Node软件包之前需要安装依赖。我这里使用yum完成,没有epel源的可以使用上面提到的脚本(epel源安装也简单)。注意:在MHA Manager的主机也是需要安装MHA Node。

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install perl-DBD-MySQL -y

安装MHA Node软件包,和上面的方法一样,如下: 

  1. wget http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.53.tar.gz
  2. tar xf mha4mysql-node-0.53.tar.gz
  3. cd mha4mysql-node-0.53
  4. perl Makefile.PL
  5. make && make install

(2)安装MHA Manager。首先安装MHA Manger依赖的perl模块(我这里使用yum安装): 

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

安装MHA Manager软件包: 

  1. wget http://mysql-master-ha.googlecode.com/files/mha4mysql-manager-0.53.tar.gz
  2. tar xf mha4mysql-manager-0.53.tar.gz
  3. cd mha4mysql-manager-0.53
  4. perl Makefile.PL
  5. make && make install

安装完成后会在/usr/local/bin目录下面生成以下脚本文件,前面已经说过这些脚本的作用,这里不再重复 

  1. [root@192.168.0.20 bin]# pwd
  2. /usr/local/bin
  3. [root@192.168.0.20 bin]# ll
  4. total 76
  5. -r-xr-xr-x 1 root root 15498 Apr 20 10:58 apply_diff_relay_logs
  6. -r-xr-xr-x 1 root root 4807 Apr 20 10:58 filter_mysqlbinlog
  7. -r-xr-xr-x 1 root root 1995 Apr 20 11:33 masterha_check_repl
  8. -r-xr-xr-x 1 root root 1779 Apr 20 11:33 masterha_check_ssh
  9. -r-xr-xr-x 1 root root 1865 Apr 20 11:33 masterha_check_status
  10. -r-xr-xr-x 1 root root 3201 Apr 20 11:33 masterha_conf_host
  11. -r-xr-xr-x 1 root root 2517 Apr 20 11:33 masterha_manager
  12. -r-xr-xr-x 1 root root 2165 Apr 20 11:33 masterha_master_monitor
  13. -r-xr-xr-x 1 root root 2373 Apr 20 11:33 masterha_master_switch
  14. -r-xr-xr-x 1 root root 3749 Apr 20 11:33 masterha_secondary_check
  15. -r-xr-xr-x 1 root root 1739 Apr 20 11:33 masterha_stop
  16. -r-xr-xr-x 1 root root 7401 Apr 20 10:58 purge_relay_logs
  17. -r-xr-xr-x 1 root root 7263 Apr 20 10:58 save_binary_logs
  18. [root@192.168.0.20 bin]#

复制相关脚本到/usr/local/bin目录(软件包解压缩后就有了,不是必须,因为这些脚本不完整,需要自己修改,这是软件开发着留给我们自己发挥的,如果开启下面的任何一个脚本对应的参数,而对应这里的脚本又没有修改,则会抛错,自己被坑的很惨) 

  1. [root@192.168.0.20 scripts]# pwd
  2. /root/mha4mysql-manager-0.53/samples/scripts
  3. [root@192.168.0.20 scripts]# ll
  4. total 32
  5. -rwxr-xr-x 1 root root 3443 Jan 8 2012 master_ip_failover #自动切换时vip管理的脚本,不是必须,如果我们使用keepalived的,我们可以自己编写脚本完成对vip的管理,比如监控mysql,如果mysql异常,我们停止keepalived就行,这样vip就会自动漂移
  6. -rwxr-xr-x 1 root root 9186 Jan 8 2012 master_ip_online_change #在线切换时vip的管理,不是必须,同样可以可以自行编写简单的shell完成
  7. -rwxr-xr-x 1 root root 11867 Jan 8 2012 power_manager #故障发生后关闭主机的脚本,不是必须
  8. -rwxr-xr-x 1 root root 1360 Jan 8 2012 send_report #因故障切换后发送报警的脚本,不是必须,可自行编写简单的shell完成。
  9. [root@192.168.0.20 scripts]# cp * /usr/local/bin/
  10. [root@192.168.0.20 scripts]#

3.配置SSH登录无密码验证(使用key登录,工作中常用)我的测试环境已经是使用key登录,服务器之间无需密码验证的。关于配置使用key登录,我想我不再重复。但是有一点需要注意:不能禁止 password 登陆,否则会出现错误

4.搭建主从复制环境

注意:binlog-do-db 和 replicate-ignore-db 设置必须相同。 MHA 在启动时候会检测过滤规则,如果过滤规则不同,MHA 不启动监控和故障转移。

(1)在server02上执行备份(192.168.0.50)

[root@192.168.0.50 ~]# mysqldump --master-data=2 --single-transaction -R --triggers -A > all.sql

其中--master-data=2代表备份时刻记录master的Binlog位置和Position,--single-transaction意思是获取一致性快照,-R意思是备份存储过程和函数,--triggres的意思是备份触发器,-A代表备份所有的库。更多信息请自行mysqldump --help查看。

(2)在server02上创建复制用户:

  1. mysql> grant replication slave on *.* to 'repl'@'192.168.0.%' identified by '123456';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> flush privileges;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql>

(3)查看主库备份时的binlog名称和位置,MASTER_LOG_FILE和MASTER_LOG_POS: 

  1. [root@192.168.0.50 ~]# head -n 30 all.sql | grep 'CHANGE MASTER TO'
  2. -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=112;
  3. [root@192.168.0.50 ~]#

(4)把备份复制到server03和server04,也就是192.168.0.60和192.168.0.70 

  1. scp all.sql server03:/data/
  2. scp all.sql server04:/data/

(5)导入备份到server03,执行复制相关命令 

mysql < /data/all.sql
  1. mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.50',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=112;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> start slave;
  4. Query OK, 0 rows affected (0.01 sec)
  5. mysql>

查看复制状态(可以看见复制成功): 

  1. [root@192.168.0.60 ~]# mysql -e 'show slave status\G' | egrep 'Slave_IO|Slave_SQL'
  2. Slave_IO_State: Waiting for master to send event
  3. Slave_IO_Running: Yes
  4. Slave_SQL_Running: Yes
  5. [root@192.168.0.60 ~]#

(6)在server04(192.168.0.70)上搭建复制环境,操作和上面一样。 

mysql < /data/all.sql
  1. mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.50',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=112;
  2. Query OK, 0 rows affected (0.07 sec)
  3. mysql> start slave;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql>

查看复制状态: 

  1. [root@192.168.0.70 ~]# mysql -e 'show slave status\G' | egrep 'Slave_IO|Slave_SQL'
  2. Slave_IO_State: Waiting for master to send event
  3. Slave_IO_Running: Yes
  4. Slave_SQL_Running: Yes
  5. [root@192.168.0.70 ~]#

(7)两台slave服务器设置read_only(从库对外提供读服务,只所以没有写进配置文件,是因为随时slave会提升为master) 

  1. [root@192.168.0.60 ~]# mysql -e 'set global read_only=1'
  2. [root@192.168.0.60 ~]#
  1. [root@192.168.0.70 ~]# mysql -e 'set global read_only=1'
  2. [root@192.168.0.70 ~]#

(8)创建监控用户(在master上执行,也就是192.168.0.50): 

  1. mysql> grant all privileges on *.* to 'root'@'192.168.0.%' identified by '123456';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> flush privileges;
  4. Query OK, 0 rows affected (0.01 sec)
  5. mysql>

到这里整个集群环境已经搭建完毕,剩下的就是配置MHA软件了。

5.配置MHA

(1)创建MHA的工作目录,并且创建相关配置文件(在软件包解压后的目录里面有样例配置文件)。

  1. [root@192.168.0.20 ~]# mkdir -p /etc/masterha
  2. [root@192.168.0.20 ~]# cp mha4mysql-manager-0.53/samples/conf/app1.cnf /etc/masterha/
  3. [root@192.168.0.20 ~]#

修改app1.cnf配置文件,修改后的文件内容如下(注意,配置文件中的注释需要去掉,我这里是为了解释清楚): 

  1. [root@192.168.0.20 ~]# cat /etc/masterha/app1.cnf
  2. [server default]
  3. manager_workdir=/var/log/masterha/app1.log //设置manager的工作目录
  4. manager_log=/var/log/masterha/app1/manager.log //设置manager的日志
  5. master_binlog_dir=/data/mysql //设置master 保存binlog的位置,以便MHA可以找到master的日志,我这里的也就是mysql的数据目录
  6. master_ip_failover_script= /usr/local/bin/master_ip_failover //设置自动failover时候的切换脚本
  7. master_ip_online_change_script= /usr/local/bin/master_ip_online_change //设置手动切换时候的切换脚本
  8. password=123456 //设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码
  9. user=root 设置监控用户root
  10. ping_interval=1 //设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行railover
  11. remote_workdir=/tmp //设置远端mysql在发生切换时binlog的保存位置
  12. repl_password=123456 //设置复制用户的密码
  13. repl_user=repl //设置复制环境中的复制用户名
  14. report_script=/usr/local/send_report //设置发生切换后发送的报警的脚本
  15. secondary_check_script= /usr/local/bin/masterha_secondary_check -s server03 -s server02
  16. shutdown_script="" //设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机放在发生脑裂,这里没有使用)
  17. ssh_user=root //设置ssh的登录用户名
  18. [server1]
  19. hostname=192.168.0.50
  20. port=3306
  21. [server2]
  22. hostname=192.168.0.60
  23. port=3306
  24. candidate_master=1 //设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave
  25. check_repl_delay=0 //默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
  26. [server3]
  27. hostname=192.168.0.70
  28. port=3306
  29. [root@192.168.0.20 ~]#

(2)设置relay log的清除方式(在每个slave节点上): 

  1. [root@192.168.0.60 ~]# mysql -e 'set global relay_log_purge=0'
  2. [root@192.168.0.70 ~]# mysql -e 'set global relay_log_purge=0'

注意:

MHA在发生切换的过程中,从库的恢复过程中依赖于relay log的相关信息,所以这里要将relay log的自动清除设置为OFF,采用手动清除relay log的方式。在默认情况下,从服务器上的中继日志会在SQL线程执行完毕后被自动删除。但是在MHA环境中,这些中继日志在恢复其他从服务器时可能会被用到,因此需要禁用中继日志的自动删除功能。定期清除中继日志需要考虑到复制延时的问题。在ext3的文件系统下,删除大的文件需要一定的时间,会导致严重的复制延时。为了避免复制延时,需要暂时为中继日志创建硬链接,因为在linux系统中通过硬链接删除大文件速度会很快。(在mysql数据库中,删除大表时,通常也采用建立硬链接的方式)

MHA节点中包含了pure_relay_logs命令工具,它可以为中继日志创建硬链接,执行SET GLOBAL relay_log_purge=1,等待几秒钟以便SQL线程切换到新的中继日志,再执行SET GLOBAL relay_log_purge=0。

pure_relay_logs脚本参数如下所示:

  1. --user mysql 用户名
  2. --password mysql 密码
  3. --port 端口号
  4. --workdir 指定创建relay log的硬链接的位置,默认是/var/tmp,由于系统不同分区创建硬链接文件会失败,故需要执行硬链接具体位置,成功执行脚本后,硬链接的中继日志文件被删除
  5. --disable_relay_log_purge 默认情况下,如果relay_log_purge=1,脚本会什么都不清理,自动退出,通过设定这个参数,当relay_log_purge=1的情况下会将relay_log_purge设置为0。清理relay log之后,最后将参数设置为OFF

(3)设置定期清理relay脚本(两台slave服务器) 

  1. [root@192.168.0.60 ~]# cat purge_relay_log.sh
  2. #!/bin/bash
  3. user=root
  4. passwd=123456
  5. port=3306
  6. log_dir='/data/masterha/log'
  7. work_dir='/data'
  8. purge='/usr/local/bin/purge_relay_logs'
  9. if [ ! -d $log_dir ]
  10. then
  11. mkdir $log_dir -p
  12. fi
  13. $purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port --workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1
  14. [root@192.168.0.60 ~]#

添加到crontab定期执行 

  1. [root@192.168.0.60 ~]# crontab -l
  2. 0 4 * * * /bin/bash /root/purge_relay_log.sh
  3. [root@192.168.0.60 ~]#

purge_relay_logs脚本删除中继日志不会阻塞SQL线程。下面我们手动执行看看什么情况。 

  1. [root@192.168.0.60 ~]# purge_relay_logs --user=root --password=123456 --port=3306 -disable_relay_log_purge --workdir=/data/
  2. 2014-04-20 15:47:24: purge_relay_logs script started.
  3. Found relay_log.info: /data/mysql/relay-log.info
  4. Removing hard linked relay log files server03-relay-bin* under /data/.. done.
  5. Current relay log file: /data/mysql/server03-relay-bin.000002
  6. Archiving unused relay log files (up to /data/mysql/server03-relay-bin.000001) ...
  7. Creating hard link for /data/mysql/server03-relay-bin.000001 under /data//server03-relay-bin.000001 .. ok.
  8. Creating hard links for unused relay log files completed.
  9. Executing SET GLOBAL relay_log_purge=1; FLUSH LOGS; sleeping a few seconds so that SQL thread can delete older relay log files (if it keeps up); SET GLOBAL relay_log_purge=0; .. ok.
  10. Removing hard linked relay log files server03-relay-bin* under /data/.. done.
  11. 2014-04-20 15:47:27: All relay log purging operations succeeded.
  12. [root@192.168.0.60 ~]#

6.检查SSH配置

检查MHA Manger到所有MHA Node的SSH连接状态:

  1. [root@192.168.0.20 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
  2. Sun Apr 20 17:17:39 2014 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  3. Sun Apr 20 17:17:39 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
  4. Sun Apr 20 17:17:39 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf..
  5. Sun Apr 20 17:17:39 2014 - [info] Starting SSH connection tests..
  6. Sun Apr 20 17:17:40 2014 - [debug]
  7. Sun Apr 20 17:17:39 2014 - [debug] Connecting via SSH from root@192.168.0.50(192.168.0.50:22) to root@192.168.0.60(192.168.0.60:22)..
  8. Sun Apr 20 17:17:39 2014 - [debug] ok.
  9. Sun Apr 20 17:17:39 2014 - [debug] Connecting via SSH from root@192.168.0.50(192.168.0.50:22) to root@192.168.0.70(192.168.0.70:22)..
  10. Sun Apr 20 17:17:39 2014 - [debug] ok.
  11. Sun Apr 20 17:17:40 2014 - [debug]
  12. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.60(192.168.0.60:22) to root@192.168.0.50(192.168.0.50:22)..
  13. Sun Apr 20 17:17:40 2014 - [debug] ok.
  14. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.60(192.168.0.60:22) to root@192.168.0.70(192.168.0.70:22)..
  15. Sun Apr 20 17:17:40 2014 - [debug] ok.
  16. Sun Apr 20 17:17:41 2014 - [debug]
  17. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.70(192.168.0.70:22) to root@192.168.0.50(192.168.0.50:22)..
  18. Sun Apr 20 17:17:40 2014 - [debug] ok.
  19. Sun Apr 20 17:17:40 2014 - [debug] Connecting via SSH from root@192.168.0.70(192.168.0.70:22) to root@192.168.0.60(192.168.0.60:22)..
  20. Sun Apr 20 17:17:41 2014 - [debug] ok.
  21. Sun Apr 20 17:17:41 2014 - [info] All SSH connection tests passed successfully.

可以看见各个节点ssh验证都是ok的。

7.检查整个复制环境状况。

通过masterha_check_repl脚本查看整个集群的状态

  1. [root@192.168.0.20 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
  2. Sun Apr 20 18:36:55 2014 - [info] Checking replication health on 192.168.0.60..
  3. Sun Apr 20 18:36:55 2014 - [info] ok.
  4. Sun Apr 20 18:36:55 2014 - [info] Checking replication health on 192.168.0.70..
  5. Sun Apr 20 18:36:55 2014 - [info] ok.
  6. Sun Apr 20 18:36:55 2014 - [info] Checking master_ip_failover_script status:
  7. Sun Apr 20 18:36:55 2014 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306
  8. Bareword "FIXME_xxx" not allowed while "strict subs" in use at /usr/local/bin/master_ip_failover line 88.
  9. Execution of /usr/local/bin/master_ip_failover aborted due to compilation errors.
  10. Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln214] Failed to get master_ip_failover_script status with return code 255:0.
  11. Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln383] Error happend on checking configurations. at /usr/local/bin/masterha_check_repl line 48
  12. Sun Apr 20 18:36:55 2014 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln478] Error happened on monitoring servers.
  13. Sun Apr 20 18:36:55 2014 - [info] Got exit code 1 (Not master dead).
  14. MySQL Replication Health is NOT OK!

发现最后的结论说我的复制不是ok的。但是上面的信息明明说是正常的,自己也进数据库查看了。这里一直踩坑。一直纠结,后来无意中发现火丁笔记的博客,这才知道了原因,原来Failover两种方式:一种是虚拟IP地址,一种是全局配置文件。MHA并没有限定使用哪一种方式,而是让用户自己选择,虚拟IP地址的方式会牵扯到其它的软件,比如keepalive软件,而且还要修改脚本master_ip_failover。(最后修改脚本后才没有这个报错,自己不懂perl也是折腾的半死,去年买了块表)

如果发现如下错误:

  1. Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 99.
  2. mysqlbinlog version not found!
Testing mysql connection and privileges..sh: mysql: command not found

解决方法如下,添加软连接(所有节点) 

ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

所以先暂时注释master_ip_failover_script= /usr/local/bin/master_ip_failover这个选项。后面引入keepalived后和修改该脚本以后再开启该选项。 

  1. [root@192.168.0.20 ~]# grep master_ip_failover /etc/masterha/app1.cnf
  2. #master_ip_failover_script= /usr/local/bin/master_ip_failover
  3. [root@192.168.0.20 ~]#

再次进行状态查看: 

  1. Sun Apr 20 18:46:08 2014 - [info] Checking replication health on 192.168.0.60..
  2. Sun Apr 20 18:46:08 2014 - [info] ok.
  3. Sun Apr 20 18:46:08 2014 - [info] Checking replication health on 192.168.0.70..
  4. Sun Apr 20 18:46:08 2014 - [info] ok.
  5. Sun Apr 20 18:46:08 2014 - [warning] master_ip_failover_script is not defined.
  6. Sun Apr 20 18:46:08 2014 - [warning] shutdown_script is not defined.
  7. Sun Apr 20 18:46:08 2014 - [info] Got exit code 0 (Not master dead).
  8. MySQL Replication Health is OK.

已经没有明显报错,只有两个警告而已,复制也显示正常了。
8.检查MHA Manager的状态:

通过master_check_status脚本查看Manager的状态:

  1. [root@192.168.0.20 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
  2. app1 is stopped(2:NOT_RUNNING).
  3. [root@192.168.0.20 ~]#

注意:如果正常,会显示"PING_OK",否则会显示"NOT_RUNNING",这代表MHA监控没有开启。
9.开启MHA Manager监控 

  1. [root@192.168.0.20 ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
  2. [1] 30867
  3. [root@192.168.0.20 ~]#

启动参数介绍:

--remove_dead_master_conf      该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除。

--manger_log                            日志存放位置

--ignore_last_failover                 在缺省情况下,如果MHA检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。该参数代表忽略上次MHA触发切换产生的文件,默认情况下,MHA发生切换后会在日志目录,也就是上面我设置的/data产生app1.failover.complete文件,下次再次切换的时候如果发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方便,这里设置为--ignore_last_failover。

查看MHA Manager监控是否正常:

  1. [root@192.168.0.20 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
  2. app1 (pid:20386) is running(0:PING_OK), master:192.168.0.50
  3. [root@192.168.0.20 ~]#

可以看见已经在监控了,而且master的主机为192.168.0.50

10.查看启动日志

  1. [root@192.168.0.20 ~]# tail -n20 /var/log/masterha/app1/manager.log
  2. Sun Apr 20 19:12:01 2014 - [info] Connecting to root@192.168.0.70(192.168.0.70:22)..
  3. Checking slave recovery environment settings..
  4. Opening /data/mysql/relay-log.info ... ok.
  5. Relay log found at /data/mysql, up to server04-relay-bin.000002
  6. Temporary relay log file is /data/mysql/server04-relay-bin.000002
  7. Testing mysql connection and privileges.. done.
  8. Testing mysqlbinlog output.. done.
  9. Cleaning up test file(s).. done.
  10. Sun Apr 20 19:12:01 2014 - [info] Slaves settings check done.
  11. Sun Apr 20 19:12:01 2014 - [info]
  12. 192.168.0.50 (current master)
  13. +--192.168.0.60
  14. +--192.168.0.70
  15. Sun Apr 20 19:12:01 2014 - [warning] master_ip_failover_script is not defined.
  16. Sun Apr 20 19:12:01 2014 - [warning] shutdown_script is not defined.
  17. Sun Apr 20 19:12:01 2014 - [info] Set master ping interval 1 seconds.
  18. Sun Apr 20 19:12:01 2014 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s server03 -s server02 --user=root --master_host=server02 --master_ip=192.168.0.50 --master_port=3306
  19. Sun Apr 20 19:12:01 2014 - [info] Starting ping health check on 192.168.0.50(192.168.0.50:3306)..
  20. Sun Apr 20 19:12:01 2014 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
  21. [root@192.168.0.20 ~]#

其中"Ping(SELECT) succeeded, waiting until MySQL doesn't respond.."说明整个系统已经开始监控了。
11.关闭MHA Manage监控

关闭很简单,使用masterha_stop命令完成。

  1. [root@192.168.0.20 ~]# masterha_stop --conf=/etc/masterha/app1.cnf
  2. Stopped app1 successfully.
  3. [1]+ Exit 1 nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover --manager_log=/data/mamanager.log
  4. [root@192.168.0.20 ~]#

12.配置VIP
vip配置可以采用两种方式,一种通过keepalived的方式管理虚拟ip的浮动;另外一种通过脚本方式启动虚拟ip的方式(即不需要keepalived或者heartbeat类似的软件)。

1.keepalived方式管理虚拟ip,keepalived配置方法如下:

(1)下载软件进行并进行安装(两台master,准确的说一台是master,另外一台是备选master,在没有切换以前是slave):

[root@192.168.0.50 ~]# wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
  1. tar xf keepalived-1.2.12.tar.gz
  2. cd keepalived-1.2.12
  3. ./configure --prefix=/usr/local/keepalived
  4. make && make install
  5. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  6. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  7. mkdir /etc/keepalived
  8. cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  9. cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

(2)配置keepalived的配置文件,在master上配置(192.168.0.50) 

  1. [root@192.168.0.50 ~]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. saltstack@163.com
  6. }
  7. notification_email_from dba@dbserver.com
  8. smtp_server 127.0.0.1
  9. smtp_connect_timeout 30
  10. router_id MySQL-HA
  11. }
  12. vrrp_instance VI_1 {
  13. state BACKUP
  14. interface eth1
  15. virtual_router_id 51
  16. priority 150
  17. advert_int 1
  18. nopreempt
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 192.168.0.88
  25. }
  26. }
  27. [root@192.168.0.50 ~]#

其中router_id MySQL HA表示设定keepalived组的名称,将192.168.0.88这个虚拟ip绑定到该主机的eth1网卡上,并且设置了状态为backup模式,将keepalived的模式设置为非抢占模式(nopreempt),priority 150表示设置的优先级为150。下面的配置略有不同,但是都是一个意思。
在候选master上配置(192.168.0.60) 

  1. [root@192.168.0.60 ~]# cat /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. saltstack@163.com
  6. }
  7. notification_email_from dba@dbserver.com
  8. smtp_server 127.0.0.1
  9. smtp_connect_timeout 30
  10. router_id MySQL-HA
  11. }
  12. vrrp_instance VI_1 {
  13. state BACKUP
  14. interface eth1
  15. virtual_router_id 51
  16. priority 120
  17. advert_int 1
  18. nopreempt
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 192.168.0.88
  25. }
  26. }
  27. [root@192.168.0.60 ~]#

(3)启动keepalived服务,在master上启动并查看日志 

  1. [root@192.168.0.50 ~]# /etc/init.d/keepalived start
  2. Starting keepalived: [ OK ]
  3. [root@192.168.0.50 ~]# tail -f /var/log/messages
  4. Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Opening file '/etc/keepalived/keepalived.conf'.
  5. Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Configuration is using : 7231 Bytes
  6. Apr 20 20:22:16 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
  7. Apr 20 20:22:16 192 kernel: IPVS: ipvs loaded.
  8. Apr 20 20:22:16 192 Keepalived_healthcheckers[15334]: Using LinkWatch kernel netlink reflector...
  9. Apr 20 20:22:19 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Transition to MASTER STATE
  10. Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Entering MASTER STATE
  11. Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) setting protocol VIPs.
  12. Apr 20 20:22:20 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88
  13. Apr 20 20:22:20 192 Keepalived_healthcheckers[15334]: Netlink reflector reports IP 192.168.0.88 added
  14. Apr 20 20:22:25 192 Keepalived_vrrp[15335]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88

发现已经将虚拟ip 192.168.0.88绑定了网卡eth1上。
(4)查看绑定情况 

  1. [root@192.168.0.50 ~]# ip addr | grep eth1
  2. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  3. inet 192.168.0.50/24 brd 192.168.0.255 scope global eth1
  4. inet 192.168.0.88/32 scope global eth1
  5. [root@192.168.0.50 ~]#

在另外一台服务器,候选master上启动keepalived服务,并观察 

  1. [root@192.168.0.60 ~]# /etc/init.d/keepalived start ; tail -f /var/log/messages
  2. Starting keepalived: [ OK ]
  3. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Registering gratuitous ARP shared channel
  4. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Opening file '/etc/keepalived/keepalived.conf'.
  5. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Configuration is using : 62976 Bytes
  6. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Using LinkWatch kernel netlink reflector...
  7. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP_Instance(VI_1) Entering BACKUP STATE
  8. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
  9. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.80.138 added
  10. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.0.60 added
  11. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6a9e added
  12. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6aa8 added
  13. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink reflector
  14. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink command channel
  15. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Opening file '/etc/keepalived/keepalived.conf'.
  16. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Configuration is using : 7231 Bytes
  17. Apr 20 20:26:18 192 kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP)
  18. Apr 20 20:26:18 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
  19. Apr 20 20:26:18 192 kernel: IPVS: ipvs loaded.
  20. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Using LinkWatch kernel netlink reflector...

从上面的信息可以看到keepalived已经配置成功。
注意:

上面两台服务器的keepalived都设置为了BACKUP模式,在keepalived中2种模式,分别是master->backup模式和backup->backup模式。这两种模式有很大区别。在master->backup模式下,一旦主库宕机,虚拟ip会自动漂移到从库,当主库修复后,keepalived启动后,还会把虚拟ip抢占过来,即使设置了非抢占模式(nopreempt)抢占ip的动作也会发生。在backup->backup模式下,当主库宕机后虚拟ip会自动漂移到从库上,当原主库恢复和keepalived服务启动后,并不会抢占新主的虚拟ip,即使是优先级高于从库的优先级别,也不会发生抢占。为了减少ip漂移次数,通常是把修复好的主库当做新的备库。

(5)MHA引入keepalived(MySQL服务进程挂掉时通过MHA 停止keepalived):

要想把keepalived服务引入MHA,我们只需要修改切换是触发的脚本文件master_ip_failover即可,在该脚本中添加在master发生宕机时对keepalived的处理。

编辑脚本/usr/local/bin/master_ip_failover,修改后如下,我对perl不熟悉,所以我这里完整贴出该脚本(主库上操作,192.168.0.50)。

在MHA Manager修改脚本修改后的内容如下(参考资料比较少):

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use Getopt::Long;
  5. my (
  6. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  7. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  8. );
  9. my $vip = '192.168.0.88';
  10. my $ssh_start_vip = "/etc/init.d/keepalived start";
  11. my $ssh_stop_vip = "/etc/init.d/keepalived stop";
  12. GetOptions(
  13. 'command=s' => \$command,
  14. 'ssh_user=s' => \$ssh_user,
  15. 'orig_master_host=s' => \$orig_master_host,
  16. 'orig_master_ip=s' => \$orig_master_ip,
  17. 'orig_master_port=i' => \$orig_master_port,
  18. 'new_master_host=s' => \$new_master_host,
  19. 'new_master_ip=s' => \$new_master_ip,
  20. 'new_master_port=i' => \$new_master_port,
  21. );
  22. exit &main();
  23. sub main {
  24. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  25. if ( $command eq "stop" || $command eq "stopssh" ) {
  26. my $exit_code = 1;
  27. eval {
  28. print "Disabling the VIP on old master: $orig_master_host \n";
  29. &stop_vip();
  30. $exit_code = 0;
  31. };
  32. if ($@) {
  33. warn "Got Error: $@\n";
  34. exit $exit_code;
  35. }
  36. exit $exit_code;
  37. }
  38. elsif ( $command eq "start" ) {
  39. my $exit_code = 10;
  40. eval {
  41. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  42. &start_vip();
  43. $exit_code = 0;
  44. };
  45. if ($@) {
  46. warn $@;
  47. exit $exit_code;
  48. }
  49. exit $exit_code;
  50. }
  51. elsif ( $command eq "status" ) {
  52. print "Checking the Status of the script.. OK \n";
  53. #`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
  54. exit 0;
  55. }
  56. else {
  57. &usage();
  58. exit 1;
  59. }
  60. }
  61. # A simple system call that enable the VIP on the new master
  62. sub start_vip() {
  63. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  64. }
  65. # A simple system call that disable the VIP on the old_master
  66. sub stop_vip() {
  67. return 0 unless ($ssh_user);
  68. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  69. }
  70. sub usage {
  71. print
  72. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
  73. }

现在已经修改这个脚本了,我们现在打开在上面提到过的参数,再检查集群状态,看是否会报错。 

  1. [root@192.168.0.20 ~]# grep 'master_ip_failover_script' /etc/masterha/app1.cnf
  2. master_ip_failover_script= /usr/local/bin/master_ip_failover
  3. [root@192.168.0.20 ~]#
  1. [root@192.168.0.20 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
  2. Sun Apr 20 23:10:01 2014 - [info] Slaves settings check done.
  3. Sun Apr 20 23:10:01 2014 - [info]
  4. 192.168.0.50 (current master)
  5. +--192.168.0.60
  6. +--192.168.0.70
  7. Sun Apr 20 23:10:01 2014 - [info] Checking replication health on 192.168.0.60..
  8. Sun Apr 20 23:10:01 2014 - [info] ok.
  9. Sun Apr 20 23:10:01 2014 - [info] Checking replication health on 192.168.0.70..
  10. Sun Apr 20 23:10:01 2014 - [info] ok.
  11. Sun Apr 20 23:10:01 2014 - [info] Checking master_ip_failover_script status:
  12. Sun Apr 20 23:10:01 2014 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306
  13. Sun Apr 20 23:10:01 2014 - [info] OK.
  14. Sun Apr 20 23:10:01 2014 - [warning] shutdown_script is not defined.
  15. Sun Apr 20 23:10:01 2014 - [info] Got exit code 0 (Not master dead).
  16. MySQL Replication Health is OK.

可以看见已经没有报错了。哈哈
 /usr/local/bin/master_ip_failover添加或者修改的内容意思是当主库数据库发生故障时,会触发MHA切换,MHA Manager会停掉主库上的keepalived服务,触发虚拟ip漂移到备选从库,从而完成切换。当然可以在keepalived里面引入脚本,这个脚本监控mysql是否正常运行,如果不正常,则调用该脚本杀掉keepalived进程。

2.通过脚本的方式管理VIP。这里是修改/usr/local/bin/master_ip_failover,也可以使用其他的语言完成,比如php语言。使用php脚本编写的failover这里就不介绍了。修改完成后内容如下,而且如果使用脚本管理vip的话,需要手动在master服务器上绑定一个vip(发现修改修改对perl竟然有感觉了。难道我适合学Perl?^_^)

[root@192.168.0.50 ~]# /sbin/ifconfig eth1:1 192.168.0.88/24

通过脚本来维护vip的测试我这里就不说明了,童鞋们自行测试,脚本如下(测试通过) 

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use Getopt::Long;
  5. my (
  6. $command, $ssh_user, $orig_master_host, $orig_master_ip,
  7. $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  8. );
  9. my $vip = '192.168.0.88/24';
  10. my $key = '1';
  11. my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
  12. my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";
  13. GetOptions(
  14. 'command=s' => \$command,
  15. 'ssh_user=s' => \$ssh_user,
  16. 'orig_master_host=s' => \$orig_master_host,
  17. 'orig_master_ip=s' => \$orig_master_ip,
  18. 'orig_master_port=i' => \$orig_master_port,
  19. 'new_master_host=s' => \$new_master_host,
  20. 'new_master_ip=s' => \$new_master_ip,
  21. 'new_master_port=i' => \$new_master_port,
  22. );
  23. exit &main();
  24. sub main {
  25. print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  26. if ( $command eq "stop" || $command eq "stopssh" ) {
  27. my $exit_code = 1;
  28. eval {
  29. print "Disabling the VIP on old master: $orig_master_host \n";
  30. &stop_vip();
  31. $exit_code = 0;
  32. };
  33. if ($@) {
  34. warn "Got Error: $@\n";
  35. exit $exit_code;
  36. }
  37. exit $exit_code;
  38. }
  39. elsif ( $command eq "start" ) {
  40. my $exit_code = 10;
  41. eval {
  42. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  43. &start_vip();
  44. $exit_code = 0;
  45. };
  46. if ($@) {
  47. warn $@;
  48. exit $exit_code;
  49. }
  50. exit $exit_code;
  51. }
  52. elsif ( $command eq "status" ) {
  53. print "Checking the Status of the script.. OK \n";
  54. exit 0;
  55. }
  56. else {
  57. &usage();
  58. exit 1;
  59. }
  60. }
  61. sub start_vip() {
  62. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  63. }
  64. sub stop_vip() {
  65. return 0 unless ($ssh_user);
  66. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  67. }
  68. sub usage {
  69. print
  70. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
  71. }

为了防止脑裂发生,推荐生产环境采用脚本的方式来管理虚拟ip,而不是使用keepalived来完成。到此为止,基本MHA集群已经配置完毕。接下来就是实际的测试环节了。通过一些测试来看一下MHA到底是如何进行工作的。下面将从MHA自动failover,我们手动failover,在线切换三种方式来介绍MHA的工作情况。

一.自动Failover(必须先启动MHA Manager,否则无法自动切换,当然手动切换不需要开启MHA Manager监控。各位童鞋请参考前面启动MHA Manager)

测试环境再次贴一下,文章太长,自己都搞晕了。

  1. 角色 ip地址 主机名 server_id 类型
  2. Monitor host 192.168.0.20 server01 - 监控复制组
  3. Master 192.168.0.50 server02 1 写入
  4. Candicate master 192.168.0.60 server03 2
  5. Slave 192.168.0.70 server04 3

自动failover模拟测试的操作步骤如下。
(1)使用sysbench生成测试数据(使用yum快速安装) 

yum install sysbench -y

在主库(192.168.0.50)上进行sysbench数据生成,在sbtest库下生成sbtest表,共100W记录。 

[root@192.168.0.50 ~]# sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=1800 --mysql-user=root --mysql-socket=/tmp/mysql.sock --mysql-password=123456 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex prepare

(2)停掉slave sql线程,模拟主从延时。(192.168.0.60) 

  1. mysql> stop slave io_thread;
  2. Query OK, 0 rows affected (0.08 sec)
  3. mysql>

另外一台slave我们没有停止io线程,所以还在继续接收日志。

(3)模拟sysbench压力测试。

在主库上(192.168.0.50)进行压力测试,持续时间为3分钟,产生大量的binlog。

  1. [root@192.168.0.50 ~]# sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=180 --mysql-user=root --mysql-socket=/tmp/mysql.sock --mysql-password=123456 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex run
  2. sysbench 0.4.12: multi-threaded system evaluation benchmark
  3. Running the test with following options:
  4. Number of threads: 16
  5. Initializing random number generator from timer.
  6. Doing OLTP test.
  7. Running mixed OLTP test
  8. Using Uniform distribution
  9. Using "BEGIN" for starting transactions
  10. Using auto_inc on the id column
  11. Threads started!
  12. Time limit exceeded, exiting...
  13. (last message repeated 15 times)
  14. Done.
  15. OLTP test statistics:
  16. queries performed:
  17. read: 15092
  18. write: 5390
  19. other: 2156
  20. total: 22638
  21. transactions: 1078 (5.92 per sec.)
  22. deadlocks: 0 (0.00 per sec.)
  23. read/write requests: 20482 (112.56 per sec.)
  24. other operations: 2156 (11.85 per sec.)
  25. Test execution summary:
  26. total time: 181.9728s
  27. total number of events: 1078
  28. total time taken by event execution: 2910.4518
  29. per-request statistics:
  30. min: 934.29ms
  31. avg: 2699.86ms
  32. max: 7679.95ms
  33. approx. 95 percentile: 4441.47ms
  34. Threads fairness:
  35. events (avg/stddev): 67.3750/1.49
  36. execution time (avg/stddev): 181.9032/0.11

(4)开启slave(192.168.0.60)上的IO线程,追赶落后于master的binlog。 

  1. mysql> start slave io_thread;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>

(5)杀掉主库mysql进程,模拟主库发生故障,进行自动failover操作。 

[root@192.168.0.50 ~]# pkill -9 mysqld

(6)查看MHA切换日志,了解整个切换过程,在192.168.0.20上查看日志: 

  1. [root@192.168.0.20 ~]# cat /var/log/masterha/app1/manager.log
  2. Mon Apr 21 20:15:45 2014 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
  3. Mon Apr 21 20:15:45 2014 - [info] Executing seconary network check script: /usr/local/bin/masterha_secondary_check -s server03 -s server02 --user=root --master_host=server02 --master_ip=192.168.0.50 --master_ Creating /tmp if not exists.. ok.
  4. Checking output directory is accessible or not..
  5. ok.
  6. Binlog found at /data/mysql, up to mysql-bin.000018
  7. Mon Apr 21 20:15:48 2014 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  8. Mon Apr 21 20:15:48 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
  9. Mon Apr 21 20:15:48 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf..
  10. ble from server03. OK.
  11. Monitoring server server02 is reachable, Master is not reachable from server02. OK.
  12. Mon Apr 21 20:15:46 2014 - [info] Master is not reachable from all other monitoring servers. Failover should start.
  13. Mon Apr 21 20:15:46 2014 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
  14. Mon Apr 21 20:15:46 2014 - [warning] Connection failed 1 time(s)..
  15. Mon Apr 21 20:15:47 2014 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
  16. Mon Apr 21 20:15:47 2014 - [warning] Connection failed 2 time(s)..
  17. Mon Apr 21 20:15:48 2014 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
  18. Mon Apr 21 20:15:48 2014 - [warning] Connection failed 3 time(s)..
  19. Mon Apr 21 20:15:48 2014 - [warning] Master is not reachable from health checker!
  20. Mon Apr 21 20:15:48 2014 - [warning] Master 192.168.0.50(192.168.0.50:3306) is not reachable!
  21. Mon Apr 21 20:15:48 2014 - [warning] SSH is reachable.
  22. Mon Apr 21 20:15:48 2014 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/masterha/app1.cnf again, and trying to connect to all servers to check server status..
  23. Mon Apr 21 20:15:48 2014 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  24. Mon Apr 21 20:15:48 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
  25. Mon Apr 21 20:15:48 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf..
  26. Mon Apr 21 20:15:48 2014 - [info] Dead Servers:
  27. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.50(192.168.0.50:3306)
  28. Mon Apr 21 20:15:48 2014 - [info] Alive Servers:
  29. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.60(192.168.0.60:3306)
  30. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.70(192.168.0.70:3306)
  31. Mon Apr 21 20:15:48 2014 - [info] Alive Slaves:
  32. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  33. Mon Apr 21 20:15:48 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  34. Mon Apr 21 20:15:48 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  35. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  36. Mon Apr 21 20:15:48 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  37. Mon Apr 21 20:15:48 2014 - [info] Checking slave configurations..
  38. Mon Apr 21 20:15:48 2014 - [info] Checking replication filtering settings..
  39. Mon Apr 21 20:15:48 2014 - [info] Replication filtering check ok.
  40. Mon Apr 21 20:15:48 2014 - [info] Master is down!
  41. Mon Apr 21 20:15:48 2014 - [info] Terminating monitoring script.
  42. Mon Apr 21 20:15:48 2014 - [info] Got exit code 20 (Master dead).
  43. Mon Apr 21 20:15:48 2014 - [info] MHA::MasterFailover version 0.53.
  44. Mon Apr 21 20:15:48 2014 - [info] Starting master failover.
  45. Mon Apr 21 20:15:48 2014 - [info]
  46. Mon Apr 21 20:15:48 2014 - [info] * Phase 1: Configuration Check Phase..
  47. Mon Apr 21 20:15:48 2014 - [info]
  48. Mon Apr 21 20:15:48 2014 - [info] Dead Servers:
  49. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.50(192.168.0.50:3306)
  50. Mon Apr 21 20:15:48 2014 - [info] Checking master reachability via mysql(double check)..
  51. Mon Apr 21 20:15:48 2014 - [info] ok.
  52. Mon Apr 21 20:15:48 2014 - [info] Alive Servers:
  53. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.60(192.168.0.60:3306)
  54. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.70(192.168.0.70:3306)
  55. Mon Apr 21 20:15:48 2014 - [info] Alive Slaves:
  56. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  57. Mon Apr 21 20:15:48 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  58. Mon Apr 21 20:15:48 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  59. Mon Apr 21 20:15:48 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  60. Mon Apr 21 20:15:48 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  61. Mon Apr 21 20:15:49 2014 - [info] ** Phase 1: Configuration Check Phase completed.
  62. Mon Apr 21 20:15:49 2014 - [info]
  63. Mon Apr 21 20:15:49 2014 - [info] * Phase 2: Dead Master Shutdown Phase..
  64. Mon Apr 21 20:15:49 2014 - [info]
  65. Mon Apr 21 20:15:49 2014 - [info] Forcing shutdown so that applications never connect to the current master..
  66. Mon Apr 21 20:15:49 2014 - [info] Executing master IP deactivatation script:
  67. Mon Apr 21 20:15:49 2014 - [info] /usr/local/bin/master_ip_failover --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --command=stopssh --ssh_user=root
  68. IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===
  69. Disabling the VIP on old master: 192.168.0.50
  70. Mon Apr 21 20:15:49 2014 - [info] done.
  71. Mon Apr 21 20:15:49 2014 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
  72. Mon Apr 21 20:15:49 2014 - [info] * Phase 2: Dead Master Shutdown Phase completed.
  73. Mon Apr 21 20:15:49 2014 - [info]
  74. Mon Apr 21 20:15:49 2014 - [info] * Phase 3: Master Recovery Phase..
  75. Mon Apr 21 20:15:49 2014 - [info]
  76. Mon Apr 21 20:15:49 2014 - [info] * Phase 3.1: Getting Latest Slaves Phase..
  77. Mon Apr 21 20:15:49 2014 - [info]
  78. Mon Apr 21 20:15:49 2014 - [info] The latest binary log file/position on all slaves is mysql-bin.000018:112
  79. Mon Apr 21 20:15:49 2014 - [info] Latest slaves (Slaves that received relay log files to the latest):
  80. Mon Apr 21 20:15:49 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  81. Mon Apr 21 20:15:49 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  82. Mon Apr 21 20:15:49 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  83. Mon Apr 21 20:15:49 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  84. Mon Apr 21 20:15:49 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  85. Mon Apr 21 20:15:49 2014 - [info] The oldest binary log file/position on all slaves is mysql-bin.000018:112
  86. Mon Apr 21 20:15:49 2014 - [info] Oldest slaves:
  87. Mon Apr 21 20:15:49 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  88. Mon Apr 21 20:15:49 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  89. Mon Apr 21 20:15:49 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  90. Mon Apr 21 20:15:49 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  91. Mon Apr 21 20:15:49 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  92. Mon Apr 21 20:15:49 2014 - [info]
  93. Mon Apr 21 20:15:49 2014 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
  94. Mon Apr 21 20:15:49 2014 - [info]
  95. Mon Apr 21 20:15:49 2014 - [info] Fetching dead master's binary logs..
  96. Mon Apr 21 20:15:49 2014 - [info] Executing command on the dead master 192.168.0.50(192.168.0.50:3306): save_binary_logs --command=save --start_file=mysql-bin.000018 --start_pos=112 --binlog_dir=/data/mysql --output_file=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53
  97. Creating /tmp if not exists.. ok.
  98. Concat binary/relay logs from mysql-bin.000018 pos 112 to mysql-bin.000018 EOF into /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog ..
  99. Dumping binlog format description event, from position 0 to 112.. ok.
  100. Dumping effective binlog data from /data/mysql/mysql-bin.000018 position 112 to tail(131).. ok.
  101. Concat succeeded.
  102. Mon Apr 21 20:15:50 2014 - [info] scp from root@192.168.0.50:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog to local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog succeeded.
  103. Mon Apr 21 20:15:50 2014 - [info] HealthCheck: SSH to 192.168.0.60 is reachable.
  104. Mon Apr 21 20:15:50 2014 - [info] HealthCheck: SSH to 192.168.0.70 is reachable.
  105. Mon Apr 21 20:15:50 2014 - [info]
  106. Mon Apr 21 20:15:50 2014 - [info] * Phase 3.3: Determining New Master Phase..
  107. Mon Apr 21 20:15:50 2014 - [info]
  108. Mon Apr 21 20:15:50 2014 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
  109. Mon Apr 21 20:15:50 2014 - [info] All slaves received relay logs to the same position. No need to resync each other.
  110. Mon Apr 21 20:15:50 2014 - [info] Searching new master from slaves..
  111. Mon Apr 21 20:15:50 2014 - [info] Candidate masters from the configuration file:
  112. Mon Apr 21 20:15:50 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  113. Mon Apr 21 20:15:50 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  114. Mon Apr 21 20:15:50 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  115. Mon Apr 21 20:15:50 2014 - [info] Non-candidate masters:
  116. Mon Apr 21 20:15:50 2014 - [info] Searching from candidate_master slaves which have received the latest relay log events..
  117. Mon Apr 21 20:15:50 2014 - [info] New master is 192.168.0.60(192.168.0.60:3306)
  118. Mon Apr 21 20:15:50 2014 - [info] Starting master failover..
  119. Mon Apr 21 20:15:50 2014 - [info]
  120. From:
  121. 192.168.0.50 (current master)
  122. +--192.168.0.60
  123. +--192.168.0.70
  124. To:
  125. 192.168.0.60 (new master)
  126. +--192.168.0.70
  127. Mon Apr 21 20:15:50 2014 - [info]
  128. Mon Apr 21 20:15:50 2014 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
  129. Mon Apr 21 20:15:50 2014 - [info]
  130. Mon Apr 21 20:15:50 2014 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
  131. Mon Apr 21 20:15:50 2014 - [info] Sending binlog..
  132. Mon Apr 21 20:15:51 2014 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog to root@192.168.0.60:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog succeeded.
  133. Mon Apr 21 20:15:51 2014 - [info]
  134. Mon Apr 21 20:15:51 2014 - [info] * Phase 3.4: Master Log Apply Phase..
  135. Mon Apr 21 20:15:51 2014 - [info]
  136. Mon Apr 21 20:15:51 2014 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
  137. Mon Apr 21 20:15:51 2014 - [info] Starting recovery on 192.168.0.60(192.168.0.60:3306)..
  138. Mon Apr 21 20:15:51 2014 - [info] Generating diffs succeeded.
  139. Mon Apr 21 20:15:51 2014 - [info] Waiting until all relay logs are applied.
  140. Mon Apr 21 20:15:51 2014 - [info] done.
  141. Mon Apr 21 20:15:51 2014 - [info] Getting slave status..
  142. Mon Apr 21 20:15:51 2014 - [info] This slave(192.168.0.60)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000018:112). No need to recover from Exec_Master_Log_Pos.
  143. Mon Apr 21 20:15:51 2014 - [info] Connecting to the target slave host 192.168.0.60, running recover script..
  144. Mon Apr 21 20:15:51 2014 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user=root --slave_host=192.168.0.60 --slave_ip=192.168.0.60 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog --workdir=/tmp --target_version=5.5.19-ndb-7.2.4-gpl-log --timestamp=20140421201548 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53 --slave_pass=xxx
  145. Mon Apr 21 20:15:51 2014 - [info]
  146. Applying differential binary/relay log files /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog on 192.168.0.60:3306. This may take long time...
  147. Applying log files succeeded.
  148. Mon Apr 21 20:15:51 2014 - [info] All relay logs were successfully applied.
  149. Mon Apr 21 20:15:51 2014 - [info] Getting new master's binlog name and position..
  150. Mon Apr 21 20:15:51 2014 - [info] mysql-bin.000022:506716
  151. Mon Apr 21 20:15:51 2014 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.60', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000022', MASTER_LOG_POS=506716, MASTER_USER='repl', MASTER_PASSWORD='xxx';
  152. Mon Apr 21 20:15:51 2014 - [info] Executing master IP activate script:
  153. Mon Apr 21 20:15:51 2014 - [info] /usr/local/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --new_master_host=192.168.0.60 --new_master_ip=192.168.0.60 --new_master_port=3306
  154. IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===
  155. Enabling the VIP - 192.168.0.88 on the new master - 192.168.0.60
  156. Mon Apr 21 20:15:52 2014 - [info] OK.
  157. Mon Apr 21 20:15:52 2014 - [info] Setting read_only=0 on 192.168.0.60(192.168.0.60:3306)..
  158. Mon Apr 21 20:15:52 2014 - [info] ok.
  159. Mon Apr 21 20:15:52 2014 - [info] ** Finished master recovery successfully.
  160. Mon Apr 21 20:15:52 2014 - [info] * Phase 3: Master Recovery Phase completed.
  161. Mon Apr 21 20:15:52 2014 - [info]
  162. Mon Apr 21 20:15:52 2014 - [info] * Phase 4: Slaves Recovery Phase..
  163. Mon Apr 21 20:15:52 2014 - [info]
  164. Mon Apr 21 20:15:52 2014 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
  165. Mon Apr 21 20:15:52 2014 - [info]
  166. Mon Apr 21 20:15:52 2014 - [info] -- Slave diff file generation on host 192.168.0.70(192.168.0.70:3306) started, pid: 31321. Check tmp log /var/log/masterha/app1.log/192.168.0.70_3306_20140421201548.log if it takes time..
  167. Mon Apr 21 20:15:52 2014 - [info]
  168. Mon Apr 21 20:15:52 2014 - [info] Log messages from 192.168.0.70 ...
  169. Mon Apr 21 20:15:52 2014 - [info]
  170. Mon Apr 21 20:15:52 2014 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
  171. Mon Apr 21 20:15:52 2014 - [info] End of log messages from 192.168.0.70.
  172. Mon Apr 21 20:15:52 2014 - [info] -- 192.168.0.70(192.168.0.70:3306) has the latest relay log events.
  173. Mon Apr 21 20:15:52 2014 - [info] Generating relay diff files from the latest slave succeeded.
  174. Mon Apr 21 20:15:52 2014 - [info]
  175. Mon Apr 21 20:15:52 2014 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
  176. Mon Apr 21 20:15:52 2014 - [info]
  177. Mon Apr 21 20:15:52 2014 - [info] -- Slave recovery on host 192.168.0.70(192.168.0.70:3306) started, pid: 31323. Check tmp log /var/log/masterha/app1.log/192.168.0.70_3306_20140421201548.log if it takes time..
  178. Mon Apr 21 20:15:52 2014 - [info]
  179. Mon Apr 21 20:15:52 2014 - [info] Log messages from 192.168.0.70 ...
  180. Mon Apr 21 20:15:52 2014 - [info]
  181. Mon Apr 21 20:15:52 2014 - [info] Sending binlog..
  182. Mon Apr 21 20:15:52 2014 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog to root@192.168.0.70:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog succeeded.
  183. Mon Apr 21 20:15:52 2014 - [info] Starting recovery on 192.168.0.70(192.168.0.70:3306)..
  184. Mon Apr 21 20:15:52 2014 - [info] Generating diffs succeeded.
  185. Mon Apr 21 20:15:52 2014 - [info] Waiting until all relay logs are applied.
  186. Mon Apr 21 20:15:52 2014 - [info] done.
  187. Mon Apr 21 20:15:52 2014 - [info] Getting slave status..
  188. Mon Apr 21 20:15:52 2014 - [info] This slave(192.168.0.70)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000018:112). No need to recover from Exec_Master_Log_Pos.
  189. Mon Apr 21 20:15:52 2014 - [info] Connecting to the target slave host 192.168.0.70, running recover script..
  190. Mon Apr 21 20:15:52 2014 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user=root --slave_host=192.168.0.70 --slave_ip=192.168.0.70 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog --workdir=/tmp --target_version=5.5.19-ndb-7.2.4-gpl-log --timestamp=20140421201548 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53 --slave_pass=xxx
  191. Mon Apr 21 20:15:52 2014 - [info]
  192. Applying differential binary/relay log files /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421201548.binlog on 192.168.0.70:3306. This may take long time...
  193. Applying log files succeeded.
  194. Mon Apr 21 20:15:52 2014 - [info] All relay logs were successfully applied.
  195. Mon Apr 21 20:15:52 2014 - [info] Resetting slave 192.168.0.70(192.168.0.70:3306) and starting replication from the new master 192.168.0.60(192.168.0.60:3306)..
  196. Mon Apr 21 20:15:52 2014 - [info] Executed CHANGE MASTER.
  197. Mon Apr 21 20:15:52 2014 - [info] Slave started.
  198. Mon Apr 21 20:15:52 2014 - [info] End of log messages from 192.168.0.70.
  199. Mon Apr 21 20:15:52 2014 - [info] -- Slave recovery on host 192.168.0.70(192.168.0.70:3306) succeeded.
  200. Mon Apr 21 20:15:52 2014 - [info] All new slave servers recovered successfully.
  201. Mon Apr 21 20:15:52 2014 - [info]
  202. Mon Apr 21 20:15:52 2014 - [info] * Phase 5: New master cleanup phease..
  203. Mon Apr 21 20:15:52 2014 - [info]
  204. Mon Apr 21 20:15:52 2014 - [info] Resetting slave info on the new master..
  205. Mon Apr 21 20:15:53 2014 - [info] 192.168.0.60: Resetting slave info succeeded.
  206. Mon Apr 21 20:15:53 2014 - [info] Master failover to 192.168.0.60(192.168.0.60:3306) completed successfully.
  207. Mon Apr 21 20:15:53 2014 - [info] Deleted server1 entry from /etc/masterha/app1.cnf .
  208. Mon Apr 21 20:15:53 2014 - [info]
  209. ----- Failover Report -----
  210. app1: MySQL Master failover 192.168.0.50 to 192.168.0.60 succeeded
  211. Master 192.168.0.50 is down!
  212. Check MHA Manager logs at server01:/var/log/masterha/app1/manager.log for details.
  213. Started automated(non-interactive) failover.
  214. Invalidated master IP address on 192.168.0.50.
  215. The latest slave 192.168.0.60(192.168.0.60:3306) has all relay logs for recovery.
  216. Selected 192.168.0.60 as a new master.
  217. 192.168.0.60: OK: Applying all logs succeeded.
  218. 192.168.0.60: OK: Activated master IP address.
  219. 192.168.0.70: This host has the latest relay log events.
  220. Generating relay diff files from the latest slave succeeded.
  221. 192.168.0.70: OK: Applying all logs succeeded. Slave started, replicating from 192.168.0.60.
  222. 192.168.0.60: Resetting slave info succeeded.
  223. Master failover to 192.168.0.60(192.168.0.60:3306) completed successfully.
  224. [root@192.168.0.20 ~]#

看到最后的Master failover to 192.168.0.60(192.168.0.60:3306) completed successfully.说明备选master现在已经上位了。

从上面的输出可以看出整个MHA的切换过程,共包括以下的步骤:

1.配置文件检查阶段,这个阶段会检查整个集群配置文件配置

2.宕机的master处理,这个阶段包括虚拟ip摘除操作,主机关机操作(这个我这里还没有实现,需要研究)

3.复制dead maste和最新slave相差的relay log,并保存到MHA Manger具体的目录下

4.识别含有最新更新的slave

5.应用从master保存的二进制日志事件(binlog events)

6.提升一个slave为新的master进行复制

7.使其他的slave连接新的master进行复制

最后启动MHA Manger监控,查看集群里面现在谁是master(在切换后监控就停止了。。。还有东西没搞对?)后来在官方网站看到这句话就明白了 。

Running MHA Manager from daemontools

Currently MHA Manager process does not run as a daemon. If failover completed successfully or the master process was killed by accident, the manager stops working. To run as a daemon, daemontool. or any external daemon program can be used. Here is an example to run from daemontools.
  1. [root@192.168.0.20 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
  2. app1 (pid:23971) is running(0:PING_OK), master:192.168.0.60
  3. [root@192.168.0.20 ~]#

二.手动Failover(MHA Manager必须没有运行)

手动failover,这种场景意味着在业务上没有启用MHA自动切换功能,当主服务器故障时,人工手动调用MHA来进行故障切换操作,具体命令如下:

注意:如果,MHA manager检测到没有dead的server,将报错,并结束failover: 

  1. Mon Apr 21 21:23:33 2014 - [info] Dead Servers:
  2. Mon Apr 21 21:23:33 2014 - [error][/usr/local/share/perl5/MHA/MasterFailover.pm, ln181] None of server is dead. Stop failover.
  3. Mon Apr 21 21:23:33 2014 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln178] Got ERROR: at /usr/local/bin/masterha_master_switch line 53

进行手动切换命令如下: 

[root@192.168.0.20 ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.0.50 --dead_master_port=3306 --new_master_host=192.168.0.60 --new_master_port=3306 --ignore_last_failover

输出的信息会询问你是否进行切换: 

  1. Mon Apr 21 21:28:00 2014 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  2. Mon Apr 21 21:28:00 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
  3. Mon Apr 21 21:28:00 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf..
  4. Mon Apr 21 21:28:00 2014 - [info] MHA::MasterFailover version 0.53.
  5. Mon Apr 21 21:28:00 2014 - [info] Starting master failover.
  6. Mon Apr 21 21:28:00 2014 - [info]
  7. Mon Apr 21 21:28:00 2014 - [info] * Phase 1: Configuration Check Phase..
  8. Mon Apr 21 21:28:00 2014 - [info]
  9. Mon Apr 21 21:28:00 2014 - [info] Dead Servers:
  10. Mon Apr 21 21:28:00 2014 - [info] 192.168.0.50(192.168.0.50:3306)
  11. Mon Apr 21 21:28:00 2014 - [info] Checking master reachability via mysql(double check)..
  12. Mon Apr 21 21:28:00 2014 - [info] ok.
  13. Mon Apr 21 21:28:00 2014 - [info] Alive Servers:
  14. Mon Apr 21 21:28:00 2014 - [info] 192.168.0.60(192.168.0.60:3306)
  15. Mon Apr 21 21:28:00 2014 - [info] 192.168.0.70(192.168.0.70:3306)
  16. Mon Apr 21 21:28:00 2014 - [info] Alive Slaves:
  17. Mon Apr 21 21:28:00 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  18. Mon Apr 21 21:28:00 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  19. Mon Apr 21 21:28:00 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  20. Mon Apr 21 21:28:00 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  21. Mon Apr 21 21:28:00 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  22. Master 192.168.0.50 is dead. Proceed? (yes/NO): yes
  23. Mon Apr 21 21:36:01 2014 - [info] ** Phase 1: Configuration Check Phase completed.
  24. Mon Apr 21 21:36:01 2014 - [info]
  25. Mon Apr 21 21:36:01 2014 - [info] * Phase 2: Dead Master Shutdown Phase..
  26. Mon Apr 21 21:36:01 2014 - [info]
  27. Mon Apr 21 21:36:01 2014 - [info] HealthCheck: SSH to 192.168.0.50 is reachable.
  28. Mon Apr 21 21:36:01 2014 - [info] Forcing shutdown so that applications never connect to the current master..
  29. Mon Apr 21 21:36:01 2014 - [info] Executing master IP deactivatation script:
  30. Mon Apr 21 21:36:01 2014 - [info] /usr/local/bin/master_ip_failover --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --command=stopssh --ssh_user=root
  31. IN SCRIPT TEST====/sbin/ifconfig eth1:1 down==/sbin/ifconfig eth1:1 192.168.0.88/24===
  32. Disabling the VIP on old master: 192.168.0.50
  33. Mon Apr 21 21:36:02 2014 - [info] done.
  34. Mon Apr 21 21:36:02 2014 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
  35. Mon Apr 21 21:36:02 2014 - [info] * Phase 2: Dead Master Shutdown Phase completed.
  36. Mon Apr 21 21:36:02 2014 - [info]
  37. Mon Apr 21 21:36:02 2014 - [info] * Phase 3: Master Recovery Phase..
  38. Mon Apr 21 21:36:02 2014 - [info]
  39. Mon Apr 21 21:36:02 2014 - [info] * Phase 3.1: Getting Latest Slaves Phase..
  40. Mon Apr 21 21:36:02 2014 - [info]
  41. Mon Apr 21 21:36:02 2014 - [info] The latest binary log file/position on all slaves is mysql-bin.000020:112
  42. Mon Apr 21 21:36:02 2014 - [info] Latest slaves (Slaves that received relay log files to the latest):
  43. Mon Apr 21 21:36:02 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  44. Mon Apr 21 21:36:02 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  45. Mon Apr 21 21:36:02 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  46. Mon Apr 21 21:36:02 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  47. Mon Apr 21 21:36:02 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  48. Mon Apr 21 21:36:02 2014 - [info] The oldest binary log file/position on all slaves is mysql-bin.000020:112
  49. Mon Apr 21 21:36:02 2014 - [info] Oldest slaves:
  50. Mon Apr 21 21:36:02 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  51. Mon Apr 21 21:36:02 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  52. Mon Apr 21 21:36:02 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  53. Mon Apr 21 21:36:02 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  54. Mon Apr 21 21:36:02 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  55. Mon Apr 21 21:36:02 2014 - [info]
  56. Mon Apr 21 21:36:02 2014 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
  57. Mon Apr 21 21:36:02 2014 - [info]
  58. Mon Apr 21 21:36:02 2014 - [info] Fetching dead master's binary logs..
  59. Mon Apr 21 21:36:02 2014 - [info] Executing command on the dead master 192.168.0.50(192.168.0.50:3306): save_binary_logs --command=save --start_file=mysql-bin.000020 --start_pos=112 --binlog_dir=/data/mysql --output_file=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53
  60. Creating /tmp if not exists.. ok.
  61. Concat binary/relay logs from mysql-bin.000020 pos 112 to mysql-bin.000020 EOF into /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog ..
  62. Dumping binlog format description event, from position 0 to 112.. ok.
  63. Dumping effective binlog data from /data/mysql/mysql-bin.000020 position 112 to tail(131).. ok.
  64. Concat succeeded.
  65. saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog 100% 131 0.1KB/s 00:00
  66. Mon Apr 21 21:36:02 2014 - [info] scp from root@192.168.0.50:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog to local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog succeeded.
  67. Mon Apr 21 21:36:02 2014 - [info] HealthCheck: SSH to 192.168.0.60 is reachable.
  68. Mon Apr 21 21:36:03 2014 - [info] HealthCheck: SSH to 192.168.0.70 is reachable.
  69. Mon Apr 21 21:36:03 2014 - [info]
  70. Mon Apr 21 21:36:03 2014 - [info] * Phase 3.3: Determining New Master Phase..
  71. Mon Apr 21 21:36:03 2014 - [info]
  72. Mon Apr 21 21:36:03 2014 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
  73. Mon Apr 21 21:36:03 2014 - [info] All slaves received relay logs to the same position. No need to resync each other.
  74. Mon Apr 21 21:36:03 2014 - [info] 192.168.0.60 can be new master.
  75. Mon Apr 21 21:36:03 2014 - [info] New master is 192.168.0.60(192.168.0.60:3306)
  76. Mon Apr 21 21:36:03 2014 - [info] Starting master failover..
  77. Mon Apr 21 21:36:03 2014 - [info]
  78. From:
  79. 192.168.0.50 (current master)
  80. +--192.168.0.60
  81. +--192.168.0.70
  82. To:
  83. 192.168.0.60 (new master)
  84. +--192.168.0.70
  85. Starting master switch from 192.168.0.50(192.168.0.50:3306) to 192.168.0.60(192.168.0.60:3306)? (yes/NO): yes
  86. Mon Apr 21 21:36:06 2014 - [info] New master decided manually is 192.168.0.60(192.168.0.60:3306)
  87. Mon Apr 21 21:36:06 2014 - [info]
  88. Mon Apr 21 21:36:06 2014 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
  89. Mon Apr 21 21:36:06 2014 - [info]
  90. Mon Apr 21 21:36:06 2014 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
  91. Mon Apr 21 21:36:06 2014 - [info] Sending binlog..
  92. saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog 100% 131 0.1KB/s 00:00
  93. Mon Apr 21 21:36:07 2014 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog to root@192.168.0.60:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog succeeded.
  94. Mon Apr 21 21:36:07 2014 - [info]
  95. Mon Apr 21 21:36:07 2014 - [info] * Phase 3.4: Master Log Apply Phase..
  96. Mon Apr 21 21:36:07 2014 - [info]
  97. Mon Apr 21 21:36:07 2014 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
  98. Mon Apr 21 21:36:07 2014 - [info] Starting recovery on 192.168.0.60(192.168.0.60:3306)..
  99. Mon Apr 21 21:36:07 2014 - [info] Generating diffs succeeded.
  100. Mon Apr 21 21:36:07 2014 - [info] Waiting until all relay logs are applied.
  101. Mon Apr 21 21:36:07 2014 - [info] done.
  102. Mon Apr 21 21:36:07 2014 - [info] Getting slave status..
  103. Mon Apr 21 21:36:07 2014 - [info] This slave(192.168.0.60)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000020:112). No need to recover from Exec_Master_Log_Pos.
  104. Mon Apr 21 21:36:07 2014 - [info] Connecting to the target slave host 192.168.0.60, running recover script..
  105. Mon Apr 21 21:36:07 2014 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user=root --slave_host=192.168.0.60 --slave_ip=192.168.0.60 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog --workdir=/tmp --target_version=5.5.19-ndb-7.2.4-gpl-log --timestamp=20140421212800 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53 --slave_pass=xxx
  106. Mon Apr 21 21:36:07 2014 - [info]
  107. Applying differential binary/relay log files /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog on 192.168.0.60:3306. This may take long time...
  108. Applying log files succeeded.
  109. Mon Apr 21 21:36:07 2014 - [info] All relay logs were successfully applied.
  110. Mon Apr 21 21:36:07 2014 - [info] Getting new master's binlog name and position..
  111. Mon Apr 21 21:36:07 2014 - [info] mysql-bin.000022:506716
  112. Mon Apr 21 21:36:07 2014 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.60', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000022', MASTER_LOG_POS=506716, MASTER_USER='repl', MASTER_PASSWORD='xxx';
  113. Mon Apr 21 21:36:07 2014 - [info] Executing master IP activate script:
  114. Mon Apr 21 21:36:07 2014 - [info] /usr/local/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --new_master_host=192.168.0.60 --new_master_ip=192.168.0.60 --new_master_port=3306
  115. IN SCRIPT TEST====/sbin/ifconfig eth1:1 down==/sbin/ifconfig eth1:1 192.168.0.88/24===
  116. Enabling the VIP - 192.168.0.88/24 on the new master - 192.168.0.60
  117. Mon Apr 21 21:36:08 2014 - [info] OK.
  118. Mon Apr 21 21:36:08 2014 - [info] Setting read_only=0 on 192.168.0.60(192.168.0.60:3306)..
  119. Mon Apr 21 21:36:08 2014 - [info] ok.
  120. Mon Apr 21 21:36:08 2014 - [info] ** Finished master recovery successfully.
  121. Mon Apr 21 21:36:08 2014 - [info] * Phase 3: Master Recovery Phase completed.
  122. Mon Apr 21 21:36:08 2014 - [info]
  123. Mon Apr 21 21:36:08 2014 - [info] * Phase 4: Slaves Recovery Phase..
  124. Mon Apr 21 21:36:08 2014 - [info]
  125. Mon Apr 21 21:36:08 2014 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
  126. Mon Apr 21 21:36:08 2014 - [info]
  127. Mon Apr 21 21:36:08 2014 - [info] -- Slave diff file generation on host 192.168.0.70(192.168.0.70:3306) started, pid: 33518. Check tmp log /var/log/masterha/app1.log/192.168.0.70_3306_20140421212800.log if it takes time..
  128. Mon Apr 21 21:36:08 2014 - [info]
  129. Mon Apr 21 21:36:08 2014 - [info] Log messages from 192.168.0.70 ...
  130. Mon Apr 21 21:36:08 2014 - [info]
  131. Mon Apr 21 21:36:08 2014 - [info] This server has all relay logs. No need to generate diff files from the latest slave.
  132. Mon Apr 21 21:36:08 2014 - [info] End of log messages from 192.168.0.70.
  133. Mon Apr 21 21:36:08 2014 - [info] -- 192.168.0.70(192.168.0.70:3306) has the latest relay log events.
  134. Mon Apr 21 21:36:08 2014 - [info] Generating relay diff files from the latest slave succeeded.
  135. Mon Apr 21 21:36:08 2014 - [info]
  136. Mon Apr 21 21:36:08 2014 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
  137. Mon Apr 21 21:36:08 2014 - [info]
  138. Mon Apr 21 21:36:08 2014 - [info] -- Slave recovery on host 192.168.0.70(192.168.0.70:3306) started, pid: 33520. Check tmp log /var/log/masterha/app1.log/192.168.0.70_3306_20140421212800.log if it takes time..
  139. saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog 100% 131 0.1KB/s 00:00
  140. Mon Apr 21 21:36:09 2014 - [info]
  141. Mon Apr 21 21:36:09 2014 - [info] Log messages from 192.168.0.70 ...
  142. Mon Apr 21 21:36:09 2014 - [info]
  143. Mon Apr 21 21:36:08 2014 - [info] Sending binlog..
  144. Mon Apr 21 21:36:08 2014 - [info] scp from local:/var/log/masterha/app1.log/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog to root@192.168.0.70:/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog succeeded.
  145. Mon Apr 21 21:36:08 2014 - [info] Starting recovery on 192.168.0.70(192.168.0.70:3306)..
  146. Mon Apr 21 21:36:08 2014 - [info] Generating diffs succeeded.
  147. Mon Apr 21 21:36:08 2014 - [info] Waiting until all relay logs are applied.
  148. Mon Apr 21 21:36:08 2014 - [info] done.
  149. Mon Apr 21 21:36:08 2014 - [info] Getting slave status..
  150. Mon Apr 21 21:36:08 2014 - [info] This slave(192.168.0.70)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000020:112). No need to recover from Exec_Master_Log_Pos.
  151. Mon Apr 21 21:36:08 2014 - [info] Connecting to the target slave host 192.168.0.70, running recover script..
  152. Mon Apr 21 21:36:08 2014 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user=root --slave_host=192.168.0.70 --slave_ip=192.168.0.70 --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog --workdir=/tmp --target_version=5.5.19-ndb-7.2.4-gpl-log --timestamp=20140421212800 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.53 --slave_pass=xxx
  153. Mon Apr 21 21:36:09 2014 - [info]
  154. Applying differential binary/relay log files /tmp/saved_master_binlog_from_192.168.0.50_3306_20140421212800.binlog on 192.168.0.70:3306. This may take long time...
  155. Applying log files succeeded.
  156. Mon Apr 21 21:36:09 2014 - [info] All relay logs were successfully applied.
  157. Mon Apr 21 21:36:09 2014 - [info] Resetting slave 192.168.0.70(192.168.0.70:3306) and starting replication from the new master 192.168.0.60(192.168.0.60:3306)..
  158. Mon Apr 21 21:36:09 2014 - [info] Executed CHANGE MASTER.
  159. Mon Apr 21 21:36:09 2014 - [info] Slave started.
  160. Mon Apr 21 21:36:09 2014 - [info] End of log messages from 192.168.0.70.
  161. Mon Apr 21 21:36:09 2014 - [info] -- Slave recovery on host 192.168.0.70(192.168.0.70:3306) succeeded.
  162. Mon Apr 21 21:36:09 2014 - [info] All new slave servers recovered successfully.
  163. Mon Apr 21 21:36:09 2014 - [info]
  164. Mon Apr 21 21:36:09 2014 - [info] * Phase 5: New master cleanup phease..
  165. Mon Apr 21 21:36:09 2014 - [info]
  166. Mon Apr 21 21:36:09 2014 - [info] Resetting slave info on the new master..
  167. Mon Apr 21 21:36:09 2014 - [info] 192.168.0.60: Resetting slave info succeeded.
  168. Mon Apr 21 21:36:09 2014 - [info] Master failover to 192.168.0.60(192.168.0.60:3306) completed successfully.
  169. Mon Apr 21 21:36:09 2014 - [info]
  170. ----- Failover Report -----
  171. app1: MySQL Master failover 192.168.0.50 to 192.168.0.60 succeeded
  172. Master 192.168.0.50 is down!
  173. Check MHA Manager logs at server01 for details.
  174. Started manual(interactive) failover.
  175. Invalidated master IP address on 192.168.0.50.
  176. The latest slave 192.168.0.60(192.168.0.60:3306) has all relay logs for recovery.
  177. Selected 192.168.0.60 as a new master.
  178. 192.168.0.60: OK: Applying all logs succeeded.
  179. 192.168.0.60: OK: Activated master IP address.
  180. 192.168.0.70: This host has the latest relay log events.
  181. Generating relay diff files from the latest slave succeeded.
  182. 192.168.0.70: OK: Applying all logs succeeded. Slave started, replicating from 192.168.0.60.
  183. 192.168.0.60: Resetting slave info succeeded.
  184. Master failover to 192.168.0.60(192.168.0.60:3306) completed successfully.

上述模拟了master宕机的情况下手动把192.168.0.60提升为主库的操作过程。

三.在线进行切换

 在许多情况下, 需要将现有的主服务器迁移到另外一台服务器上。 比如主服务器硬件故障,RAID 控制卡需要重建,将主服务器移到性能更好的服务器上等等。维护主服务器引起性能下降, 导致停机时间至少无法写入数据。 另外, 阻塞或杀掉当前运行的会话会导致主主之间数据不一致的问题发生。 MHA 提供快速切换和优雅的阻塞写入,这个切换过程只需要 0.5-2s 的时间,这段时间内数据是无法写入的。在很多情况下,0.5-2s 的阻塞写入是可以接受的。因此切换主服务器不需要计划分配维护时间窗口。

MHA在线切换的大概过程:
1.检测复制设置和确定当前主服务器
2.确定新的主服务器
3.阻塞写入到当前主服务器
4.等待所有从服务器赶上复制
5.授予写入到新的主服务器
6.重新设置从服务器 

注意,在线切换的时候应用架构需要考虑以下两个问题:

1.自动识别master和slave的问题(master的机器可能会切换),如果采用了vip的方式,基本可以解决这个问题。

2.负载均衡的问题(可以定义大概的读写比例,每台机器可承担的负载比例,当有机器离开集群时,需要考虑这个问题)

为了保证数据完全一致性,在最快的时间内完成切换,MHA的在线切换必须满足以下条件才会切换成功,否则会切换失败。

1.所有slave的IO线程都在运行

2.所有slave的SQL线程都在运行

3.所有的show slave status的输出中Seconds_Behind_Master参数小于或者等于running_updates_limit秒,如果在切换过程中不指定running_updates_limit,那么默认情况下running_updates_limit为1秒。

4.在master端,通过show processlist输出,没有一个更新花费的时间大于running_updates_limit秒。

在线切换步骤如下:

首先,停掉MHA监控:

[root@192.168.0.20 ~]# masterha_stop --conf=/etc/masterha/app1.cnf

其次,进行在线切换操作(模拟在线切换主库操作,原主库192.168.0.50变为slave,192.168.0.60提升为新的主库) 

[root@192.168.0.20 ~]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.0.60 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000

最后查看日志,了解切换过程,输出信息如下: 

  1. [root@192.168.0.20 ~]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.0.60 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
  2. Wed Apr 23 00:27:39 2014 - [info] MHA::MasterRotate version 0.53.
  3. Wed Apr 23 00:27:39 2014 - [info] Starting online master switch..
  4. Wed Apr 23 00:27:39 2014 - [info]
  5. Wed Apr 23 00:27:39 2014 - [info] * Phase 1: Configuration Check Phase..
  6. Wed Apr 23 00:27:39 2014 - [info]
  7. Wed Apr 23 00:27:39 2014 - [info] Reading default configuratoins from /etc/masterha_default.cnf..
  8. Wed Apr 23 00:27:39 2014 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
  9. Wed Apr 23 00:27:39 2014 - [info] Reading server configurations from /etc/masterha/app1.cnf..
  10. Wed Apr 23 00:27:39 2014 - [info] Multi-master configuration is detected. Current primary(writable) master is 192.168.0.50(192.168.0.50:3306)
  11. Wed Apr 23 00:27:39 2014 - [info] Master configurations are as below:
  12. Master 192.168.0.60(192.168.0.60:3306), replicating from 192.168.0.50(192.168.0.50:3306), read-only
  13. Master 192.168.0.50(192.168.0.50:3306), replicating from 192.168.0.60(192.168.0.60:3306)
  14. Wed Apr 23 00:27:39 2014 - [info] Current Alive Master: 192.168.0.50(192.168.0.50:3306)
  15. Wed Apr 23 00:27:39 2014 - [info] Alive Slaves:
  16. Wed Apr 23 00:27:39 2014 - [info] 192.168.0.60(192.168.0.60:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  17. Wed Apr 23 00:27:39 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  18. Wed Apr 23 00:27:39 2014 - [info] Primary candidate for the new Master (candidate_master is set)
  19. Wed Apr 23 00:27:39 2014 - [info] 192.168.0.70(192.168.0.70:3306) Version=5.5.19-ndb-7.2.4-gpl-log (oldest major version between slaves) log-bin:enabled
  20. Wed Apr 23 00:27:39 2014 - [info] Replicating from 192.168.0.50(192.168.0.50:3306)
  21. It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.0.50(192.168.0.50:3306)? (YES/no): yes
  22. Wed Apr 23 00:27:40 2014 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
  23. Wed Apr 23 00:27:40 2014 - [info] ok.
  24. Wed Apr 23 00:27:40 2014 - [info] Checking MHA is not monitoring or doing failover..
  25. Wed Apr 23 00:27:40 2014 - [info] Checking replication health on 192.168.0.60..
  26. Wed Apr 23 00:27:40 2014 - [info] ok.
  27. Wed Apr 23 00:27:40 2014 - [info] Checking replication health on 192.168.0.70..
  28. Wed Apr 23 00:27:40 2014 - [info] ok.
  29. Wed Apr 23 00:27:40 2014 - [info] 192.168.0.60 can be new master.
  30. Wed Apr 23 00:27:40 2014 - [info]
  31. From:
  32. 192.168.0.50 (current master)
  33. +--192.168.0.60
  34. +--192.168.0.70
  35. To:
  36. 192.168.0.60 (new master)
  37. +--192.168.0.70
  38. +--192.168.0.50
  39. Starting master switch from 192.168.0.50(192.168.0.50:3306) to 192.168.0.60(192.168.0.60:3306)? (yes/NO): yes
  40. Wed Apr 23 00:27:41 2014 - [info] Checking whether 192.168.0.60(192.168.0.60:3306) is ok for the new master..
  41. Wed Apr 23 00:27:41 2014 - [info] ok.
  42. Wed Apr 23 00:27:41 2014 - [info] ** Phase 1: Configuration Check Phase completed.
  43. Wed Apr 23 00:27:41 2014 - [info]
  44. Wed Apr 23 00:27:41 2014 - [info] * Phase 2: Rejecting updates Phase..
  45. Wed Apr 23 00:27:41 2014 - [info]
  46. Wed Apr 23 00:27:41 2014 - [info] Executing master ip online change script to disable write on the current master:
  47. Wed Apr 23 00:27:41 2014 - [info] /usr/local/bin/master_ip_online_change.pl --command=stop --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --new_master_host=192.168.0.60 --new_master_ip=192.168.0.60 --new_master_port=3306
  48. Wed Apr 23 00:27:41 2014 714804 Set read_only on the new master.. ok.
  49. Wed Apr 23 00:27:41 2014 719969 Set read_only=1 on the orig master.. ok.
  50. Disabling the VIP on old master: 192.168.0.50
  51. reverse mapping checking getaddrinfo for bogon [192.168.0.50] failed - POSSIBLE BREAK-IN ATTEMPT!
  52. Wed Apr 23 00:27:51 2014 963762 Killing all application threads..
  53. Wed Apr 23 00:27:51 2014 963869 done.
  54. Wed Apr 23 00:27:51 2014 - [info] ok.
  55. Wed Apr 23 00:27:51 2014 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
  56. Wed Apr 23 00:27:51 2014 - [info] Executing FLUSH TABLES WITH READ LOCK..
  57. Wed Apr 23 00:27:51 2014 - [info] ok.
  58. Wed Apr 23 00:27:51 2014 - [info] Orig master binlog:pos is mysql-bin.000028:112.
  59. Wed Apr 23 00:27:51 2014 - [info] Waiting to execute all relay logs on 192.168.0.60(192.168.0.60:3306)..
  60. Wed Apr 23 00:27:51 2014 - [info] master_pos_wait(mysql-bin.000028:112) completed on 192.168.0.60(192.168.0.60:3306). Executed 0 events.
  61. Wed Apr 23 00:27:51 2014 - [info] done.
  62. Wed Apr 23 00:27:51 2014 - [info] Getting new master's binlog name and position..
  63. Wed Apr 23 00:27:51 2014 - [info] mysql-bin.000023:1550
  64. Wed Apr 23 00:27:51 2014 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.60', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000023', MASTER_LOG_POS=1550, MASTER_USER='repl', MASTER_PASSWORD='xxx';
  65. Wed Apr 23 00:27:51 2014 - [info] Executing master ip online change script to allow write on the new master:
  66. Wed Apr 23 00:27:51 2014 - [info] /usr/local/bin/master_ip_online_change.pl --command=start --orig_master_host=192.168.0.50 --orig_master_ip=192.168.0.50 --orig_master_port=3306 --new_master_host=192.168.0.60 --new_master_ip=192.168.0.60 --new_master_port=3306
  67. Wed Apr 23 00:27:52 2014 077334 Set read_only=0 on the new master.
  68. Enabling the VIP - 192.168.0.88/24 on the new master - 192.168.0.60
  69. reverse mapping checking getaddrinfo for bogon [192.168.0.60] failed - POSSIBLE BREAK-IN ATTEMPT!
  70. Wed Apr 23 00:28:02 2014 - [info] ok.
  71. Wed Apr 23 00:28:02 2014 - [info]
  72. Wed Apr 23 00:28:02 2014 - [info] * Switching slaves in parallel..
  73. Wed Apr 23 00:28:02 2014 - [info]
  74. Wed Apr 23 00:28:02 2014 - [info] -- Slave switch on host 192.168.0.70(192.168.0.70:3306) started, pid: 3036
  75. Wed Apr 23 00:28:02 2014 - [info]
  76. Wed Apr 23 00:28:02 2014 - [info] Log messages from 192.168.0.70 ...
  77. Wed Apr 23 00:28:02 2014 - [info]
  78. Wed Apr 23 00:28:02 2014 - [info] Waiting to execute all relay logs on 192.168.0.70(192.168.0.70:3306)..
  79. Wed Apr 23 00:28:02 2014 - [info] master_pos_wait(mysql-bin.000028:112) completed on 192.168.0.70(192.168.0.70:3306). Executed 0 events.
  80. Wed Apr 23 00:28:02 2014 - [info] done.
  81. Wed Apr 23 00:28:02 2014 - [info] Resetting slave 192.168.0.70(192.168.0.70:3306) and starting replication from the new master 192.168.0.60(192.168.0.60:3306)..
  82. Wed Apr 23 00:28:02 2014 - [info] Executed CHANGE MASTER.
  83. Wed Apr 23 00:28:02 2014 - [info] Slave started.
  84. Wed Apr 23 00:28:02 2014 - [info] End of log messages from 192.168.0.70 ...
  85. Wed Apr 23 00:28:02 2014 - [info]
  86. Wed Apr 23 00:28:02 2014 - [info] -- Slave switch on host 192.168.0.70(192.168.0.70:3306) succeeded.
  87. Wed Apr 23 00:28:02 2014 - [info] Unlocking all tables on the orig master:
  88. Wed Apr 23 00:28:02 2014 - [info] Executing UNLOCK TABLES..
  89. Wed Apr 23 00:28:02 2014 - [info] ok.
  90. Wed Apr 23 00:28:02 2014 - [info] Starting orig master as a new slave..
  91. Wed Apr 23 00:28:02 2014 - [info] Resetting slave 192.168.0.50(192.168.0.50:3306) and starting replication from the new master 192.168.0.60(192.168.0.60:3306)..
  92. Wed Apr 23 00:28:02 2014 - [info] Executed CHANGE MASTER.
  93. Wed Apr 23 00:28:02 2014 - [info] Slave started.
  94. Wed Apr 23 00:28:02 2014 - [info] All new slave servers switched successfully.
  95. Wed Apr 23 00:28:02 2014 - [info]
  96. Wed Apr 23 00:28:02 2014 - [info] * Phase 5: New master cleanup phease..
  97. Wed Apr 23 00:28:02 2014 - [info]
  98. Wed Apr 23 00:28:02 2014 - [info] 192.168.0.60: Resetting slave info succeeded.
  99. Wed Apr 23 00:28:02 2014 - [info] Switching master to 192.168.0.60(192.168.0.60:3306) completed successfully.

其中参数的意思:

--orig_master_is_new_slave 切换时加上此参数是将原 master 变为 slave 节点,如果不加此参数,原来的 master 将不启动

--running_updates_limit=10000,故障切换时,候选master 如果有延迟的话, mha 切换不能成功,加上此参数表示延迟在此时间范围内都可切换(单位为s),但是切换的时间长短是由recover 时relay 日志的大小决定 

注意:由于在线进行切换需要调用到master_ip_online_change这个脚本,但是由于该脚本不完整,需要自己进行相应的修改,我google到后发现还是有问题,脚本中new_master_password这个变量获取不到,导致在线切换失败,所以进行了相关的硬编码,直接把mysql的root用户密码赋值给变量new_master_password,如果有哪位大牛知道原因,请指点指点。这个脚本还可以管理vip。下面贴出脚本:

  1. #!/usr/bin/env perl
  2. # Copyright (C) 2011 DeNA Co.,Ltd.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ## Note: This is a sample script and is not complete. Modify the script based on your environment.
  19. use strict;
  20. use warnings FATAL => 'all';
  21. use Getopt::Long;
  22. use MHA::DBHelper;
  23. use MHA::NodeUtil;
  24. use Time::HiRes qw( sleep gettimeofday tv_interval );
  25. use Data::Dumper;
  26. my $_tstart;
  27. my $_running_interval = 0.1;
  28. my (
  29. $command, $orig_master_host, $orig_master_ip,
  30. $orig_master_port, $orig_master_user,
  31. $new_master_host, $new_master_ip, $new_master_port,
  32. $new_master_user,
  33. );
  34. my $vip = '192.168.0.88/24'; # Virtual IP
  35. my $key = "1";
  36. my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip";
  37. my $ssh_stop_vip = "/sbin/ifconfig eth1:$key down";
  38. my $ssh_user = "root";
  39. my $new_master_password='123456';
  40. my $orig_master_password='123456';
  41. GetOptions(
  42. 'command=s' => \$command,
  43. #'ssh_user=s' => \$ssh_user,
  44. 'orig_master_host=s' => \$orig_master_host,
  45. 'orig_master_ip=s' => \$orig_master_ip,
  46. 'orig_master_port=i' => \$orig_master_port,
  47. 'orig_master_user=s' => \$orig_master_user,
  48. #'orig_master_password=s' => \$orig_master_password,
  49. 'new_master_host=s' => \$new_master_host,
  50. 'new_master_ip=s' => \$new_master_ip,
  51. 'new_master_port=i' => \$new_master_port,
  52. 'new_master_user=s' => \$new_master_user,
  53. #'new_master_password=s' => \$new_master_password,
  54. );
  55. exit &main();
  56. sub current_time_us {
  57. my ( $sec, $microsec ) = gettimeofday();
  58. my $curdate = localtime($sec);
  59. return $curdate . " " . sprintf( "%06d", $microsec );
  60. }
  61. sub sleep_until {
  62. my $elapsed = tv_interval($_tstart);
  63. if ( $_running_interval > $elapsed ) {
  64. sleep( $_running_interval - $elapsed );
  65. }
  66. }
  67. sub get_threads_util {
  68. my $dbh = shift;
  69. my $my_connection_id = shift;
  70. my $running_time_threshold = shift;
  71. my $type = shift;
  72. $running_time_threshold = 0 unless ($running_time_threshold);
  73. $type = 0 unless ($type);
  74. my @threads;
  75. my $sth = $dbh->prepare("SHOW PROCESSLIST");
  76. $sth->execute();
  77. while ( my $ref = $sth->fetchrow_hashref() ) {
  78. my $id = $ref->{Id};
  79. my $user = $ref->{User};
  80. my $host = $ref->{Host};
  81. my $command = $ref->{Command};
  82. my $state = $ref->{State};
  83. my $query_time = $ref->{Time};
  84. my $info = $ref->{Info};
  85. $info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
  86. next if ( $my_connection_id == $id );
  87. next if ( defined($query_time) && $query_time < $running_time_threshold );
  88. next if ( defined($command) && $command eq "Binlog Dump" );
  89. next if ( defined($user) && $user eq "system user" );
  90. next
  91. if ( defined($command)
  92. && $command eq "Sleep"
  93. && defined($query_time)
  94. && $query_time >= 1 );
  95. if ( $type >= 1 ) {
  96. next if ( defined($command) && $command eq "Sleep" );
  97. next if ( defined($command) && $command eq "Connect" );
  98. }
  99. if ( $type >= 2 ) {
  100. next if ( defined($info) && $info =~ m/^select/i );
  101. next if ( defined($info) && $info =~ m/^show/i );
  102. }
  103. push @threads, $ref;
  104. }
  105. return @threads;
  106. }
  107. sub main {
  108. if ( $command eq "stop" ) {
  109. ## Gracefully killing connections on the current master
  110. # 1. Set read_only= 1 on the new master
  111. # 2. DROP USER so that no app user can establish new connections
  112. # 3. Set read_only= 1 on the current master
  113. # 4. Kill current queries
  114. # * Any database access failure will result in script die.
  115. my $exit_code = 1;
  116. eval {
  117. ## Setting read_only=1 on the new master (to avoid accident)
  118. my $new_master_handler = new MHA::DBHelper();
  119. # args: hostname, port, user, password, raise_error(die_on_error)_or_not
  120. $new_master_handler->connect( $new_master_ip, $new_master_port,
  121. $new_master_user, $new_master_password, 1 );
  122. print current_time_us() . " Set read_only on the new master.. ";
  123. $new_master_handler->enable_read_only();
  124. if ( $new_master_handler->is_read_only() ) {
  125. print "ok.\n";
  126. }
  127. else {
  128. die "Failed!\n";
  129. }
  130. $new_master_handler->disconnect();
  131. # Connecting to the orig master, die if any database error happens
  132. my $orig_master_handler = new MHA::DBHelper();
  133. $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
  134. $orig_master_user, $orig_master_password, 1 );
  135. ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
  136. #$orig_master_handler->disable_log_bin_local();
  137. #print current_time_us() . " Drpping app user on the orig master..\n";
  138. #FIXME_xxx_drop_app_user($orig_master_handler);
  139. ## Waiting for N * 100 milliseconds so that current connections can exit
  140. my $time_until_read_only = 15;
  141. $_tstart = [gettimeofday];
  142. my @threads = get_threads_util( $orig_master_handler->{dbh},
  143. $orig_master_handler->{connection_id} );
  144. while ( $time_until_read_only > 0 && $#threads >= 0 ) {
  145. if ( $time_until_read_only % 5 == 0 ) {
  146. printf
  147. "%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
  148. current_time_us(), $#threads + 1, $time_until_read_only * 100;
  149. if ( $#threads < 5 ) {
  150. print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
  151. foreach (@threads);
  152. }
  153. }
  154. sleep_until();
  155. $_tstart = [gettimeofday];
  156. $time_until_read_only--;
  157. @threads = get_threads_util( $orig_master_handler->{dbh},
  158. $orig_master_handler->{connection_id} );
  159. }
  160. ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
  161. print current_time_us() . " Set read_only=1 on the orig master.. ";
  162. $orig_master_handler->enable_read_only();
  163. if ( $orig_master_handler->is_read_only() ) {
  164. print "ok.\n";
  165. }
  166. else {
  167. die "Failed!\n";
  168. }
  169. ## Waiting for M * 100 milliseconds so that current update queries can complete
  170. my $time_until_kill_threads = 5;
  171. @threads = get_threads_util( $orig_master_handler->{dbh},
  172. $orig_master_handler->{connection_id} );
  173. while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
  174. if ( $time_until_kill_threads % 5 == 0 ) {
  175. printf
  176. "%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
  177. current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
  178. if ( $#threads < 5 ) {
  179. print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
  180. foreach (@threads);
  181. }
  182. }
  183. sleep_until();
  184. $_tstart = [gettimeofday];
  185. $time_until_kill_threads--;
  186. @threads = get_threads_util( $orig_master_handler->{dbh},
  187. $orig_master_handler->{connection_id} );
  188. }
  189. print "Disabling the VIP on old master: $orig_master_host \n";
  190. &stop_vip();
  191. ## Terminating all threads
  192. print current_time_us() . " Killing all application threads..\n";
  193. $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
  194. print current_time_us() . " done.\n";
  195. #$orig_master_handler->enable_log_bin_local();
  196. $orig_master_handler->disconnect();
  197. ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
  198. $exit_code = 0;
  199. };
  200. if ($@) {
  201. warn "Got Error: $@\n";
  202. exit $exit_code;
  203. }
  204. exit $exit_code;
  205. }
  206. elsif ( $command eq "start" ) {
  207. ## Activating master ip on the new master
  208. # 1. Create app user with write privileges
  209. # 2. Moving backup script if needed
  210. # 3. Register new master's ip to the catalog database
  211. # We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
  212. # If exit code is 0 or 10, MHA does not abort
  213. my $exit_code = 10;
  214. eval {
  215. my $new_master_handler = new MHA::DBHelper();
  216. # args: hostname, port, user, password, raise_error_or_not
  217. $new_master_handler->connect( $new_master_ip, $new_master_port,
  218. $new_master_user, $new_master_password, 1 );
  219. ## Set read_only=0 on the new master
  220. #$new_master_handler->disable_log_bin_local();
  221. print current_time_us() . " Set read_only=0 on the new master.\n";
  222. $new_master_handler->disable_read_only();
  223. ## Creating an app user on the new master
  224. #print current_time_us() . " Creating app user on the new master..\n";
  225. #FIXME_xxx_create_app_user($new_master_handler);
  226. #$new_master_handler->enable_log_bin_local();
  227. $new_master_handler->disconnect();
  228. ## Update master ip on the catalog database, etc
  229. print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  230. &start_vip();
  231. $exit_code = 0;
  232. };
  233. if ($@) {
  234. warn "Got Error: $@\n";
  235. exit $exit_code;
  236. }
  237. exit $exit_code;
  238. }
  239. elsif ( $command eq "status" ) {
  240. # do nothing
  241. exit 0;
  242. }
  243. else {
  244. &usage();
  245. exit 1;
  246. }
  247. }
  248. # A simple system call that enable the VIP on the new master
  249. sub start_vip() {
  250. `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  251. }
  252. # A simple system call that disable the VIP on the old_master
  253. sub stop_vip() {
  254. `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  255. }
  256. sub usage {
  257. print
  258. "Usage: master_ip_online_change --command=start|stop|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
  259. die;
  260. }

四.修复宕机的Master 

通常情况下自动切换以后,原master可能已经废弃掉,待原master主机修复后,如果数据完整的情况下,可能想把原来master重新作为新主库的slave,这时我们可以借助当时自动切换时刻的MHA日志来完成对原master的修复。下面是提取相关日志的命令:

  1. [root@192.168.0.20 app1]# grep -i "All other slaves should start" manager.log
  2. Mon Apr 21 22:28:33 2014 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.0.60', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000022', MASTER_LOG_POS=506716, MASTER_USER='repl', MASTER_PASSWORD='xxx';
  3. [root@192.168.0.20 app1]#

获取上述信息以后,就可以直接在修复后的master上执行change master to相关操作,重新作为从库了。

最后补充一下邮件发送脚本send_report ,这个脚本在询问一位朋友后可以使用,如下:

  1. #!/usr/bin/perl
  2. # Copyright (C) 2011 DeNA Co.,Ltd.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ## Note: This is a sample script and is not complete. Modify the script based on your environment.
  19. use strict;
  20. use warnings FATAL => 'all';
  21. use Mail::Sender;
  22. use Getopt::Long;
  23. #new_master_host and new_slave_hosts are set only when recovering master succeeded
  24. my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
  25. my $smtp='smtp.163.com';
  26. my $mail_from='xxxx';
  27. my $mail_user='xxxxx';
  28. my $mail_pass='xxxxx';
  29. my $mail_to=['xxxx','xxxx'];
  30. GetOptions(
  31. 'orig_master_host=s' => \$dead_master_host,
  32. 'new_master_host=s' => \$new_master_host,
  33. 'new_slave_hosts=s' => \$new_slave_hosts,
  34. 'subject=s' => \$subject,
  35. 'body=s' => \$body,
  36. );
  37. mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
  38. sub mailToContacts {
  39. my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
  40. open my $DEBUG, "> /tmp/monitormail.log"
  41. or die "Can't open the debug file:$!\n";
  42. my $sender = new Mail::Sender {
  43. ctype => 'text/plain; charset=utf-8',
  44. encoding => 'utf-8',
  45. smtp => $smtp,
  46. from => $mail_from,
  47. auth => 'LOGIN',
  48. TLS_allowed => '0',
  49. authid => $user,
  50. authpwd => $passwd,
  51. to => $mail_to,
  52. subject => $subject,
  53. debug => $DEBUG
  54. };
  55. $sender->MailMsg(
  56. { msg => $msg,
  57. debug => $DEBUG
  58. }
  59. ) or print $Mail::Sender::Error;
  60. return 1;
  61. }
  62. # Do whatever you want here
  63. exit 0;

最后切换以后发送告警的邮件示例,注意,这个是我后续的测试,和上面环境出现的ip不一致不要在意。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/294783
推荐阅读
相关标签
  

闽ICP备14008679号