码迷,mamicode.com
首页 > 数据库 > 详细

mysql高可用之mha(补充1)

时间:2015-08-12 17:00:03      阅读:858      评论:0      收藏:0      [点我收藏+]

标签:mysql mha   masterha_master_swit   master_ip_faliover   master_ip_online_cha   masterha_conf_host   

1.masterha_conf_host自动添加/删除数据库

在某些情况下我们需要在配置文件中添加mysql服务器实现对现有数据的扩展或者有故障的master已经修复好需要在现有环境中当做slave继续使用,那么我们就可以使用masterha_conf_host来实现。

在前面我们讲到rd-mysql-test1的master有问题导致启动failover,现在rd-mysql-test2成为新的master,现在rd-mysql-test1已经修复,我们将其作为slave继续使用:

现在的配置文件为:

[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/data/mysql
password=123456
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=rep
ssh_port=1022
ssh_user=root
user=mha

[server2]
candidate_master=1
check_repl_delay=0
hostname=10.10.10.57
port=3306

[server3]
hostname=10.10.10.58
port=3306
我们用masterha_conf_host来添加

[root@rd-mysql-test4 mha]# masterha_conf_host --command=add --conf=/etc/mha/app1.cnf --block=server1 --hostname=10.10.10.56 --params="no_master=1;ignore_fail=1"
Wrote server1 entry to /etc/mha/app1.cnf .
[root@rd-mysql-test4 mha]# cat app1.cnf 
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/data/mysql
password=123456
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=rep
ssh_port=1022
ssh_user=root
user=mha

[server1]
hostname=10.10.10.56
ignore_fail=1
no_master=1

[server2]
candidate_master=1
check_repl_delay=0
hostname=10.10.10.57
port=3306

[server3]
hostname=10.10.10.58
port=3306
看到了吗?server1的模块已经添加到配置文件了,然后需要配置主从:

mysql> change master to master_host='10.10.10.57',master_port=3306,master_user='rep',master_password='123456',master_log_file='mysql-bin.000010',master_log_pos=120;
mysql> start slave;
[root@rd-mysql-test4 app1]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
ok,就这样我们我们添加了一台salve到当前架构了。

2.masterha_master_switch(关闭mha监控)

masterha_manager既可以用作监控也能够实现failover;而masterha_master_switch不能够用来监控,但是可以用来实现master的failover,也可以用来实现在线master切换。

参数:--master_state=dead,强制性使用参数,主要是dead和alive;如果设置为alive,则说明进行的在线master切换,但是master必须是alive的。

--dead_master_host=(hostname),强制性使用参数,--dead_master_ip,--dead_master_port也可以使用

--new_master_host=(hostname),可选参数,如果设置了则会按照设置的参数将其作为新master,如果没有设置,则会和按照masterha_manager的自动failover的选举方式一样(检查candidate_master参数)进行选择。

--interactive=(0|1),互动模式为1(默认),0为不互动

--ssh_reachable=(0|1|2),检查master的ssh是否正常,0为不同,1为通,2为未知。默认为2,会检查确认ssh的状态并更为0或1。如果master通过ssh可以登录,并且master_ip_failover_script或shutdown_script设置的话,则会执行stopssh命令;反之masterha_master_switch将会执行stop命令。另外,有故障的master如果ssh通的话,failover script还会copy二进制日志的。

--skip_change_master通过设置此参数,mha在应用为apply different relay logs后,会跳过执行change master和start slave,因此不会产生新的master,以便我们进行二次检查。

--skip_disable_read_only通过设置此参数,mha将不会在新的master执行set global read_only=0

--last_failover_minute,--ignore_last_failover,--wait_on_failover_error和masterha_manager中参数命令一样。

--remove_dead_master_conf执行完成后会清除配置文件中相应的模块。

(1)手动failover

互动模式(默认)

masterha_master_switch --conf=/etc/mha/app1.conf --master_state=dead --dead_master_host=10.10.10.56 --new_master_host=10.10.10.57 --interactive=1

non-interactive模式

masterha_master_switch --conf=/etc/mha/app1.conf --master_state=dead --dead_master_host=10.10.10.56 --new_master_host=10.10.10.57 --interactive=0

这种模式和masterha_manager效果是一样的,这适用于已经确定master已经dead,你想尽快完成failover。

(2)在线故障迁移

在某些情况下你想进行在线迁移,及时现在的master仍然正常运行。最典型的例子是你想更换硬件或升级master的硬件,例如更换raid卡,你就必须停机。在这些情况下,我们就需要将master在线迁移到其他服务器上。

在线切换开始前需要满足以下条件:

1.所有slave上的IO threads必须running

2.所有slave上的SQL threads必须running

3.所有slaves上的Seconds_Behind_Master(show slave status输出的)必须小于等于设置的--running_updates_limit秒数,默认--running_updates_limit=1

4.master上show processlist输出的内容,没有一个更新花费的时间大于设置的--running_updates_limit的秒数

参数如下:

--new_master_host=(hostname)

--orig_master_is_new_slave 在master switch完成后,老master将会作为一个新的salve继续运行。默认情况下,这个参数禁用。如果你使用这个参数,需要在配置文件中设置repl_password,因为它不知道新的master的复制密码。

--running_updates_limit=(seconds) 如果现在的master执行写操作的执行时间大于这个参数,或者任何一台slave的Seconds_Behind_Master大于这个参数,那么master switch将自动放弃。默认参数为1s

--remove_orig_master_conf 当master switch成功完成后,mha manager将自动将原master的区块从配置文件中删除,默认不删。

--skip_lock_all_tables 在master switch过程中,mha将会在原master上运行flush tables with read lock来保证数据不会更新。但是flush tables with read lock的非常耗时,因为需要等所有的读写操作文成后才会锁表,因此如果你确定原master上的数据不会更新的话,你可以避免使用这个参数。

开始在线迁移:

[root@rd-mysql-test4 mha]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --new_master_host=10.10.10.58 --orig_master_is_new_slave --running_updates_limit=1000000
Tue Aug 11 14:52:51 2015 - [info] MHA::MasterRotate version 0.56.
Tue Aug 11 14:52:51 2015 - [info] Starting online master switch..
Tue Aug 11 14:52:51 2015 - [info] 
Tue Aug 11 14:52:51 2015 - [info] * Phase 1: Configuration Check Phase..
Tue Aug 11 14:52:51 2015 - [info] 
Tue Aug 11 14:52:51 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 11 14:52:51 2015 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Aug 11 14:52:51 2015 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Aug 11 14:52:51 2015 - [info] GTID failover mode = 0
Tue Aug 11 14:52:51 2015 - [info] Current Alive Master: 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 14:52:51 2015 - [info] Alive Slaves:
Tue Aug 11 14:52:51 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 14:52:51 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 14:52:51 2015 - [info]     Not candidate for the new Master (no_master is set)
Tue Aug 11 14:52:51 2015 - [info]   10.10.10.58(10.10.10.58:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 14:52:51 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 10.10.10.57(10.10.10.57:3306)? (YES/no): yes
Tue Aug 11 14:52:54 2015 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Tue Aug 11 14:52:54 2015 - [info]  ok.
Tue Aug 11 14:52:54 2015 - [info] Checking MHA is not monitoring or doing failover..
Tue Aug 11 14:52:54 2015 - [info] Checking replication health on 10.10.10.56..
Tue Aug 11 14:52:54 2015 - [info]  ok.
Tue Aug 11 14:52:54 2015 - [info] Checking replication health on 10.10.10.58..
Tue Aug 11 14:52:54 2015 - [info]  ok.
Tue Aug 11 14:52:54 2015 - [info] 10.10.10.58 can be new master.
Tue Aug 11 14:52:54 2015 - [info] 
From:
10.10.10.57(10.10.10.57:3306) (current master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.58(10.10.10.58:3306)

To:
10.10.10.58(10.10.10.58:3306) (new master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.57(10.10.10.57:3306)

Starting master switch from 10.10.10.57(10.10.10.57:3306) to 10.10.10.58(10.10.10.58:3306)? (yes/NO): yes
Tue Aug 11 14:52:58 2015 - [info] Checking whether 10.10.10.58(10.10.10.58:3306) is ok for the new master..
Tue Aug 11 14:52:58 2015 - [info]  ok.
Tue Aug 11 14:52:58 2015 - [info] 10.10.10.57(10.10.10.57:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Tue Aug 11 14:52:58 2015 - [info] 10.10.10.57(10.10.10.57:3306): Resetting slave pointing to the dummy host.
Tue Aug 11 14:52:58 2015 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Aug 11 14:52:58 2015 - [info] 
Tue Aug 11 14:52:58 2015 - [info] * Phase 2: Rejecting updates Phase..
Tue Aug 11 14:52:58 2015 - [info] 
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): no
Tue Aug 11 14:53:03 2015 - [info] Not typed yes. Stopping. at /usr/local/share/perl5/MHA/MasterRotate.pm line 320, <STDIN> line 3.
报错master_ip_online_change_script is not defined,在迁移过程中需要用到master_ip_online_change脚本,因此我们需要在配置文件中加入master_ip_online_change_script=/usr/local/bin/master_ip_online_change。引入此脚本的目的在于用一种更好的方式锁表:a.通过drop user app_user来阻塞数据库的连接;b.等待1~2秒钟以便所有的数据库都disconnect;c.通过set global read_only=1阻塞所有的更新除了super;d.等待一段时间;e.通过flush tables with read lock阻塞所有的更新。

再次执行:

[root@rd-mysql-test4 mha]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --new_master_host=10.10.10.58 --orig_master_is_new_slave --running_updates_limit=1000000
Tue Aug 11 14:53:08 2015 - [info] MHA::MasterRotate version 0.56.
Tue Aug 11 14:53:08 2015 - [info] Starting online master switch..
Tue Aug 11 14:53:08 2015 - [info] 
Tue Aug 11 14:53:08 2015 - [info] * Phase 1: Configuration Check Phase..
Tue Aug 11 14:53:08 2015 - [info] 
Tue Aug 11 14:53:08 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 11 14:53:08 2015 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Aug 11 14:53:08 2015 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Aug 11 14:53:08 2015 - [info] GTID failover mode = 0
Tue Aug 11 14:53:08 2015 - [info] Current Alive Master: 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 14:53:08 2015 - [info] Alive Slaves:
Tue Aug 11 14:53:08 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 14:53:08 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 14:53:08 2015 - [info]     Not candidate for the new Master (no_master is set)
Tue Aug 11 14:53:08 2015 - [info]   10.10.10.58(10.10.10.58:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 14:53:08 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 10.10.10.57(10.10.10.57:3306)? (YES/no): yes
Tue Aug 11 14:53:09 2015 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Tue Aug 11 14:53:09 2015 - [info]  ok.
Tue Aug 11 14:53:09 2015 - [info] Checking MHA is not monitoring or doing failover..
Tue Aug 11 14:53:09 2015 - [info] Checking replication health on 10.10.10.56..
Tue Aug 11 14:53:09 2015 - [info]  ok.
Tue Aug 11 14:53:09 2015 - [info] Checking replication health on 10.10.10.58..
Tue Aug 11 14:53:09 2015 - [info]  ok.
Tue Aug 11 14:53:09 2015 - [info] 10.10.10.58 can be new master.
Tue Aug 11 14:53:09 2015 - [info] 
From:
10.10.10.57(10.10.10.57:3306) (current master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.58(10.10.10.58:3306)

To:
10.10.10.58(10.10.10.58:3306) (new master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.57(10.10.10.57:3306)

Starting master switch from 10.10.10.57(10.10.10.57:3306) to 10.10.10.58(10.10.10.58:3306)? (yes/NO): yes
Tue Aug 11 14:53:11 2015 - [info] Checking whether 10.10.10.58(10.10.10.58:3306) is ok for the new master..
Tue Aug 11 14:53:11 2015 - [info]  ok.
Tue Aug 11 14:53:11 2015 - [info] 10.10.10.57(10.10.10.57:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Tue Aug 11 14:53:11 2015 - [info] 10.10.10.57(10.10.10.57:3306): Resetting slave pointing to the dummy host.
Tue Aug 11 14:53:11 2015 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Aug 11 14:53:11 2015 - [info] 
Tue Aug 11 14:53:11 2015 - [info] * Phase 2: Rejecting updates Phase..
Tue Aug 11 14:53:11 2015 - [info] 
Tue Aug 11 14:53:11 2015 - [info] Executing master ip online change script to disable write on the current master:
Tue Aug 11 14:53:11 2015 - [info]   /usr/local/bin/master_ip_online_change --command=stop --orig_master_host=10.10.10.57 --orig_master_ip=10.10.10.57 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='123456' --new_master_host=10.10.10.58 --new_master_ip=10.10.10.58 --new_master_port=3306 --new_master_user='mha' --new_master_password='123456' --orig_master_ssh_user=root --new_master_ssh_user=root  --orig_master_ssh_port=1022  --new_master_ssh_port=1022 --orig_master_is_new_slave
Unknown option: orig_master_ssh_port
Unknown option: new_master_ssh_port
Tue Aug 11 14:53:11 2015 290906 Set read_only on the new master.. ok.
Tue Aug 11 14:53:11 2015 296385 Drpping app user on the orig master..
Got Error: Undefined subroutine &main::FIXME_xxx_drop_app_user called at /usr/local/bin/master_ip_online_change line 152.

Tue Aug 11 14:53:11 2015 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/local/bin/masterha_master_switch line 53
再次报错“Got Error: Undefined subroutine &main::FIXME_xxx_drop_app_user called at /usr/local/bin/master_ip_online_change line 152”

这是由于无法找到对FIXME_xxx_drop_app_user定义,由于perl不熟,我暂时注释掉相关drop user的行或FIXME_xxx等,不会影响其他过程。

其中需要注释掉的语句为:

FIXME_xxx_drop_app_user($orig_master_handler);

FIXME_xxx_create_app_user($new_master_handler);

再次执行:

[root@rd-mysql-test4 mha]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --new_master_host=10.10.10.58 --orig_master_is_new_slave --running_updates_limit=1000000
Tue Aug 11 15:08:11 2015 - [info] MHA::MasterRotate version 0.56.
Tue Aug 11 15:08:11 2015 - [info] Starting online master switch..
Tue Aug 11 15:08:11 2015 - [info] 
Tue Aug 11 15:08:11 2015 - [info] * Phase 1: Configuration Check Phase..
Tue Aug 11 15:08:11 2015 - [info] 
Tue Aug 11 15:08:11 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Aug 11 15:08:11 2015 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Aug 11 15:08:11 2015 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Aug 11 15:08:12 2015 - [info] GTID failover mode = 0
Tue Aug 11 15:08:12 2015 - [info] Current Alive Master: 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 15:08:12 2015 - [info] Alive Slaves:
Tue Aug 11 15:08:12 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 15:08:12 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)
Tue Aug 11 15:08:12 2015 - [info]     Not candidate for the new Master (no_master is set)
Tue Aug 11 15:08:12 2015 - [info]   10.10.10.58(10.10.10.58:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Tue Aug 11 15:08:12 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 10.10.10.57(10.10.10.57:3306)? (YES/no): yes
Tue Aug 11 15:08:13 2015 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Tue Aug 11 15:08:13 2015 - [info]  ok.
Tue Aug 11 15:08:13 2015 - [info] Checking MHA is not monitoring or doing failover..
Tue Aug 11 15:08:13 2015 - [info] Checking replication health on 10.10.10.56..
Tue Aug 11 15:08:13 2015 - [info]  ok.
Tue Aug 11 15:08:13 2015 - [info] Checking replication health on 10.10.10.58..
Tue Aug 11 15:08:13 2015 - [info]  ok.
Tue Aug 11 15:08:13 2015 - [info] 10.10.10.58 can be new master.
Tue Aug 11 15:08:13 2015 - [info] 
From:
10.10.10.57(10.10.10.57:3306) (current master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.58(10.10.10.58:3306)

To:
10.10.10.58(10.10.10.58:3306) (new master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.57(10.10.10.57:3306)

Starting master switch from 10.10.10.57(10.10.10.57:3306) to 10.10.10.58(10.10.10.58:3306)? (yes/NO): yes
Tue Aug 11 15:08:14 2015 - [info] Checking whether 10.10.10.58(10.10.10.58:3306) is ok for the new master..
Tue Aug 11 15:08:14 2015 - [info]  ok.
Tue Aug 11 15:08:14 2015 - [info] 10.10.10.57(10.10.10.57:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Tue Aug 11 15:08:14 2015 - [info] 10.10.10.57(10.10.10.57:3306): Resetting slave pointing to the dummy host.
Tue Aug 11 15:08:14 2015 - [info] ** Phase 1: Configuration Check Phase completed.
Tue Aug 11 15:08:14 2015 - [info] 
Tue Aug 11 15:08:14 2015 - [info] * Phase 2: Rejecting updates Phase..
Tue Aug 11 15:08:14 2015 - [info] 
Tue Aug 11 15:08:14 2015 - [info] Executing master ip online change script to disable write on the current master:
Tue Aug 11 15:08:14 2015 - [info]   /usr/local/bin/master_ip_online_change --command=stop --orig_master_host=10.10.10.57 --orig_master_ip=10.10.10.57 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='123456' --new_master_host=10.10.10.58 --new_master_ip=10.10.10.58 --new_master_port=3306 --new_master_user='mha' --new_master_password='123456' --orig_master_ssh_user=root --new_master_ssh_user=root  --orig_master_ssh_port=1022  --new_master_ssh_port=1022 --orig_master_is_new_slave
Unknown option: orig_master_ssh_port
Unknown option: new_master_ssh_port
Tue Aug 11 15:08:14 2015 969366 Set read_only on the new master.. ok.
Tue Aug 11 15:08:14 2015 974088 Drpping app user on the orig master..
Tue Aug 11 15:08:14 2015 974826 Waiting all running 1 threads are disconnected.. (max 1500 milliseconds)
{'Time' => '332257','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Tue Aug 11 15:08:15 2015 475963 Waiting all running 1 threads are disconnected.. (max 1000 milliseconds)
{'Time' => '332258','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Tue Aug 11 15:08:15 2015 976758 Waiting all running 1 threads are disconnected.. (max 500 milliseconds)
{'Time' => '332258','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Tue Aug 11 15:08:16 2015 478138 Set read_only=1 on the orig master.. ok.
Tue Aug 11 15:08:16 2015 480285 Waiting all running 1 queries are disconnected.. (max 500 milliseconds)
{'Time' => '332259','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Tue Aug 11 15:08:16 2015 979233 Killing all application threads..
Tue Aug 11 15:08:16 2015 979903 done.
Tue Aug 11 15:08:16 2015 - [info]  ok.
Tue Aug 11 15:08:16 2015 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Tue Aug 11 15:08:16 2015 - [info] Executing FLUSH TABLES WITH READ LOCK..
Tue Aug 11 15:08:16 2015 - [info]  ok.
Tue Aug 11 15:08:16 2015 - [info] Orig master binlog:pos is mysql-bin.000010:120.
Tue Aug 11 15:08:16 2015 - [info]  Waiting to execute all relay logs on 10.10.10.58(10.10.10.58:3306)..
Tue Aug 11 15:08:16 2015 - [info]  master_pos_wait(mysql-bin.000010:120) completed on 10.10.10.58(10.10.10.58:3306). Executed 0 events.
Tue Aug 11 15:08:16 2015 - [info]   done.
Tue Aug 11 15:08:16 2015 - [info] Getting new master's binlog name and position..
Tue Aug 11 15:08:16 2015 - [info]  mysql-bin.000008:120
Tue Aug 11 15:08:16 2015 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.10.10.58', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx';
Tue Aug 11 15:08:16 2015 - [info] Executing master ip online change script to allow write on the new master:
Tue Aug 11 15:08:16 2015 - [info]   /usr/local/bin/master_ip_online_change --command=start --orig_master_host=10.10.10.57 --orig_master_ip=10.10.10.57 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='123456' --new_master_host=10.10.10.58 --new_master_ip=10.10.10.58 --new_master_port=3306 --new_master_user='mha' --new_master_password='123456' --orig_master_ssh_user=root --new_master_ssh_user=root  --orig_master_ssh_port=1022  --new_master_ssh_port=1022 --orig_master_is_new_slave
Unknown option: orig_master_ssh_port
Unknown option: new_master_ssh_port
Tue Aug 11 15:08:17 2015 164390 Set read_only=0 on the new master.
Tue Aug 11 15:08:17 2015 165561 Creating app user on the new master..
Got Error: Undefined subroutine &main::FIXME_xxx_create_app_user called at /usr/local/bin/master_ip_online_change line 246.

Tue Aug 11 15:08:17 2015 - [warning] Proceeding.
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info] * Switching slaves in parallel..
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info] -- Slave switch on host 10.10.10.56(10.10.10.56:3306) started, pid: 9445
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info] Log messages from 10.10.10.56 ...
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info]  Waiting to execute all relay logs on 10.10.10.56(10.10.10.56:3306)..
Tue Aug 11 15:08:17 2015 - [info]  master_pos_wait(mysql-bin.000010:120) completed on 10.10.10.56(10.10.10.56:3306). Executed 0 events.
Tue Aug 11 15:08:17 2015 - [info]   done.
Tue Aug 11 15:08:17 2015 - [info]  Resetting slave 10.10.10.56(10.10.10.56:3306) and starting replication from the new master 10.10.10.58(10.10.10.58:3306)..
Tue Aug 11 15:08:17 2015 - [info]  Executed CHANGE MASTER.
Tue Aug 11 15:08:17 2015 - [info]  Slave started.
Tue Aug 11 15:08:17 2015 - [info] End of log messages from 10.10.10.56 ...
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info] -- Slave switch on host 10.10.10.56(10.10.10.56:3306) succeeded.
Tue Aug 11 15:08:17 2015 - [info] Unlocking all tables on the orig master:
Tue Aug 11 15:08:17 2015 - [info] Executing UNLOCK TABLES..
Tue Aug 11 15:08:17 2015 - [info]  ok.
Tue Aug 11 15:08:17 2015 - [info] Starting orig master as a new slave..
Tue Aug 11 15:08:17 2015 - [info]  Resetting slave 10.10.10.57(10.10.10.57:3306) and starting replication from the new master 10.10.10.58(10.10.10.58:3306)..
Tue Aug 11 15:08:17 2015 - [info]  Executed CHANGE MASTER.
Tue Aug 11 15:08:17 2015 - [info]  Slave started.
Tue Aug 11 15:08:17 2015 - [info] All new slave servers switched successfully.
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info] * Phase 5: New master cleanup phase..
Tue Aug 11 15:08:17 2015 - [info] 
Tue Aug 11 15:08:17 2015 - [info]  10.10.10.58: Resetting slave info succeeded.
Tue Aug 11 15:08:17 2015 - [info] Switching master to 10.10.10.58(10.10.10.58:3306) completed successfully.
ok,在线迁移成功了,我们可以登录mysql查看。

注意:1.orig_master_ssh_port和new_master_ssh_port在master_ip_online_change中不支持,此处是从配置中读取的。

2.在执行过程中若报一下错误:

Wed Aug 12 14:05:12 2015 - [error][/usr/local/share/perl5/MHA/MasterRotate.pm, ln262] We should not start online master switch when one of connections are running long queries on the new master(10.10.10.56(10.10.10.56:3306)). Currently 1 thread(s) are running.
Details:
{'Time' => '93075','Command' => 'Daemon','db' => undef,'Id' => '29','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Wed Aug 12 14:05:12 2015 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/local/bin/masterha_master_switch line 53
这是由于new master上的event_scheduler导致,我们可以临时关闭下event_scheduler通过set global event_scheduler=0;待迁移完成后再开启。

从以上看在线迁移主要经过以下过程:

a.配置检查阶段,选出并确定新master

b.拒绝更新阶段,阻塞原master使其不能进行写 ;在新master上执行最新relay logs;其他的salve从新的master进行复制;对新master配置可写

c.将原master转换成新slave

3.master_ip_failover

在普通的HA环境中,我们可以在master上配置虚拟ip(VIP),当master故障时,高可用软件如keepalived等可以将VIP漂移到备用机上。

mha manager会调用master_ip_failover_script三次,第一次是在进入监控master前,检查repl时使用;第二次是在调用shutdown_script前使用;第三次是在将relay log应用到新master后使用。

注:参数不用在配置文件中配置。

(1)首先我们需要根据实际情况更改master_ip_failover脚本,修改的地方已经用###标注处

use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);

###################################################################
my $vip = '10.10.10.60';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key $vip down";
####################################################################
GetOptions(
  'command=s'             => \$command,
  'ssh_user=s'            => \$ssh_user,
  'orig_master_host=s'    => \$orig_master_host,
  'orig_master_ip=s'      => \$orig_master_ip,
  'orig_master_port=i'    => \$orig_master_port,
  'new_master_host=s'     => \$new_master_host,
  'new_master_ip=s'       => \$new_master_ip,
  'new_master_port=i'     => \$new_master_port,
  'new_master_user=s'     => \$new_master_user,
  'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
###########################################################################
  print "\n\n script test ++++++ $ssh_stop_vip+++$ssh_start_vip+++ \n\n";
###########################################################################
  if ( $command eq "stop" || $command eq "stopssh" ) {
    # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
    # If you manage master ip address at global catalog database,
    # invalidate orig_master_ip here.
    my $exit_code = 1;
    eval {
##############################################################################
      print "disable the VIP on old master: $orig_master_host \n";
      &stop_vip();
##############################################################################	  
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    # all arguments are passed.
    # If you manage master ip address at global catalog database,
    # activate new_master_ip here.
    # You can also grant write access (create user, set read_only=0, etc) here.
    my $exit_code = 10;
    eval {
#####################################################################################
      print "enable the VIP: $vip on the new master: $new_master_host \n";
      &start_vip();
#######################################################################################
      my $new_master_handler = new MHA::DBHelper();
      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();
      ## Creating an app user on the new master
     # print "Creating app user on the new master..\n";
     # FIXME_xxx_create_user( $new_master_handler->{dbh} );
     # $new_master_handler->enable_log_bin_local();
     # $new_master_handler->disconnect();
     # ## Update master ip on the catalog database, etc
     # FIXME_xxx;
      $exit_code = 0;
    };
    if ($@) {
      warn $@;
      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {
    print "check the status of the script .. OK \n";
    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}
##############################################################################
sub start_vip() {
	`ssh  $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
	return 0 unless ($ssh_user);
	`ssh  $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
##############################################################################
sub usage {
  print
"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";
}
修改的主要内容是当master故障时,会触发脚本将master的vip停掉,在选出的新master上启动vip,并将read_only置为0使其可写。

我们通过停掉10.10.10.58上的mysql进程,failover过程请看/var/log/masterha/manager.log

Wed Aug 12 11:00:25 2015 - [warning] shutdown_script is not defined.
Wed Aug 12 11:00:25 2015 - [info] Set master ping interval 1 seconds.
Wed Aug 12 11:00:25 2015 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Wed Aug 12 11:00:25 2015 - [info] Starting ping health check on 10.10.10.58(10.10.10.58:3306)..
Wed Aug 12 11:00:25 2015 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
Wed Aug 12 11:01:00 2015 - [warning] Got error on MySQL select ping: 2006 (MySQL server has gone away)
Wed Aug 12 11:01:01 2015 - [info] HealthCheck: SSH to 10.10.10.58 is reachable.
Wed Aug 12 11:01:01 2015 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Aug 12 11:01:01 2015 - [warning] Connection failed 2 time(s)..
Wed Aug 12 11:01:02 2015 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Aug 12 11:01:02 2015 - [warning] Connection failed 3 time(s)..
Wed Aug 12 11:01:03 2015 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Aug 12 11:01:03 2015 - [warning] Connection failed 4 time(s)..
Wed Aug 12 11:01:03 2015 - [warning] Master is not reachable from health checker!
Wed Aug 12 11:01:03 2015 - [warning] Master 10.10.10.58(10.10.10.58:3306) is not reachable!
Wed Aug 12 11:01:03 2015 - [warning] SSH is reachable.
Wed Aug 12 11:01:03 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 12 11:01:03 2015 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Wed Aug 12 11:01:03 2015 - [info] Reading server configuration from /etc/mha/app1.cnf..
Wed Aug 12 11:01:03 2015 - [info] GTID failover mode = 0
Wed Aug 12 11:01:03 2015 - [info] Dead Servers:
Wed Aug 12 11:01:03 2015 - [info]   10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:03 2015 - [info] Alive Servers:
Wed Aug 12 11:01:03 2015 - [info]   10.10.10.56(10.10.10.56:3306)
Wed Aug 12 11:01:03 2015 - [info]   10.10.10.57(10.10.10.57:3306)
Wed Aug 12 11:01:03 2015 - [info] Alive Slaves:
Wed Aug 12 11:01:03 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:03 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:03 2015 - [info]     Not candidate for the new Master (no_master is set)
Wed Aug 12 11:01:03 2015 - [info]   10.10.10.57(10.10.10.57:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:03 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:03 2015 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 12 11:01:03 2015 - [info] Checking slave configurations..
Wed Aug 12 11:01:03 2015 - [warning]  relay_log_purge=0 is not set on slave 10.10.10.56(10.10.10.56:3306).
Wed Aug 12 11:01:03 2015 - [info] Checking replication filtering settings..
Wed Aug 12 11:01:03 2015 - [info]  Replication filtering check ok.
Wed Aug 12 11:01:03 2015 - [info] Master is down!
Wed Aug 12 11:01:03 2015 - [info] Terminating monitoring script.
Wed Aug 12 11:01:03 2015 - [info] Got exit code 20 (Master dead).
Wed Aug 12 11:01:03 2015 - [info] MHA::MasterFailover version 0.56.
Wed Aug 12 11:01:03 2015 - [info] Starting master failover.
Wed Aug 12 11:01:03 2015 - [info]
Wed Aug 12 11:01:03 2015 - [info] * Phase 1: Configuration Check Phase..
Wed Aug 12 11:01:03 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] GTID failover mode = 0
Wed Aug 12 11:01:04 2015 - [info] Dead Servers:
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info] Checking master reachability via MySQL(double check)...
Wed Aug 12 11:01:04 2015 - [info]  ok.
Wed Aug 12 11:01:04 2015 - [info] Alive Servers:
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.56(10.10.10.56:3306)
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.57(10.10.10.57:3306)
Wed Aug 12 11:01:04 2015 - [info] Alive Slaves:
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info]     Not candidate for the new Master (no_master is set)
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.57(10.10.10.57:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info] Starting Non-GTID based failover.
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] ** Phase 1: Configuration Check Phase completed.
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] * Phase 2: Dead Master Shutdown Phase..
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] Forcing shutdown so that applications never connect to the current master..
Wed Aug 12 11:01:04 2015 - [info] Executing master IP deactivation script:
Wed Aug 12 11:01:04 2015 - [info]   /usr/local/bin/master_ip_failover --orig_master_host=10.10.10.58 --orig_master_ip=10.10.10.58 --orig_master_port=3306 --command=stopssh --ssh_user=root


 script test ++++++ /sbin/ifconfig eth0:1 10.10.10.60 down+++/sbin/ifconfig eth0:1 10.10.10.60+++

disable the VIP on old master: 10.10.10.58
Wed Aug 12 11:01:04 2015 - [info]  done.
Wed Aug 12 11:01:04 2015 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Wed Aug 12 11:01:04 2015 - [info] * Phase 3: Master Recovery Phase..
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] The latest binary log file/position on all slaves is mysql-bin.000008:120
Wed Aug 12 11:01:04 2015 - [info] Latest slaves (Slaves that received relay log files to the latest):
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info]     Not candidate for the new Master (no_master is set)
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.57(10.10.10.57:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 12 11:01:04 2015 - [info] The oldest binary log file/position on all slaves is mysql-bin.000008:120
Wed Aug 12 11:01:04 2015 - [info] Oldest slaves:
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info]     Not candidate for the new Master (no_master is set)
Wed Aug 12 11:01:04 2015 - [info]   10.10.10.57(10.10.10.57:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:04 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:04 2015 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Wed Aug 12 11:01:04 2015 - [info]
Wed Aug 12 11:01:04 2015 - [info] Fetching dead master's binary logs..
  Creating /tmp if not exists..    ok.
 Concat binary/relay logs from mysql-bin.000008 pos 120 to mysql-bin.000011 EOF into /tmp/saved_master_binlog_from_10.10.10.58_3306_20150812110103.binlog ..
 Binlog Checksum enabled
  Dumping binlog format description event, from position 0 to 120.. ok.
  Dumping effective binlog data from /data/mysql/mysql-bin.000008 position 120 to tail(143).. ok.
  Dumping binlog head events (rotate events), skipping format description events from /data/mysql/mysql-bin.000009..  Binlog Checksum enabled
dumped up to pos 120. ok.
dumped up to pos 120. ok.
  Dumping effective binlog data from /data/mysql/mysql-bin.000010 position 120 to tail(143).. ok.
  Dumping binlog head events (rotate events), skipping format description events from /data/mysql/mysql-bin.000011..  Binlog Checksum enabled
dumped up to pos 120. ok.
  Dumping effective binlog data from /data/mysql/mysql-bin.000011 position 120 to tail(143).. ok.
 Binlog Checksum enabled
 Concat succeeded.
Wed Aug 12 11:01:05 2015 - [info] HealthCheck: SSH to 10.10.10.56 is reachable.
Wed Aug 12 11:01:05 2015 - [info] HealthCheck: SSH to 10.10.10.57 is reachable.
Wed Aug 12 11:01:06 2015 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Wed Aug 12 11:01:06 2015 - [info] All slaves received relay logs to the same position. No need to resync each other.
Wed Aug 12 11:01:06 2015 - [info] Searching new master from slaves..
Wed Aug 12 11:01:06 2015 - [info]  Candidate masters from the configuration file:
Wed Aug 12 11:01:06 2015 - [info]   10.10.10.57(10.10.10.57:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 11:01:06 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:06 2015 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Aug 12 11:01:06 2015 - [info]     Replicating from 10.10.10.58(10.10.10.58:3306)
Wed Aug 12 11:01:06 2015 - [info] New master is 10.10.10.57(10.10.10.57:3306)
Wed Aug 12 11:01:06 2015 - [info] Starting master failover..
Wed Aug 12 11:01:06 2015 - [info]
From:
10.10.10.58(10.10.10.58:3306) (current master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.57(10.10.10.57:3306)

To:
10.10.10.57(10.10.10.57:3306) (new master)
 +--10.10.10.56(10.10.10.56:3306)
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] * Phase 3.3: New Master Diff Log Generation Phase..
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] * Phase 3.4: Master Log Apply Phase..
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] *NOTICE: If any error happens from this phase, manual recovery is needed.
Wed Aug 12 11:01:06 2015 - [info] Starting recovery on 10.10.10.57(10.10.10.57:3306)..
Wed Aug 12 11:01:06 2015 - [info]  Generating diffs succeeded.
Wed Aug 12 11:01:06 2015 - [info] Waiting until all relay logs are applied.
Wed Aug 12 11:01:06 2015 - [info]  done.
Wed Aug 12 11:01:06 2015 - [info] Getting slave status..
Wed Aug 12 11:01:06 2015 - [info] This slave(10.10.10.57)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000008:120). No need to recover from Exec_Master_Log_Pos.
Wed Aug 12 11:01:06 2015 - [info] Connecting to the target slave host 10.10.10.57, running recover script..
Wed Aug 12 11:01:06 2015 - [info]
MySQL client version is 5.6.25. Using --binary-mode.
Applying differential binary/relay log files /tmp/saved_master_binlog_from_10.10.10.58_3306_20150812110103.binlog on 10.10.10.57:3306. This may take long time...
Applying log files succeeded.
Wed Aug 12 11:01:06 2015 - [info]  All relay logs were successfully applied.
Wed Aug 12 11:01:06 2015 - [info] Getting new master's binlog name and position..
Wed Aug 12 11:01:06 2015 - [info]  mysql-bin.000010:120


 script test ++++++ /sbin/ifconfig eth0:1 10.10.10.60 down+++/sbin/ifconfig eth0:1 10.10.10.60+++

enable the VIP: 10.10.10.60 on the new master: 10.10.10.57
Set read_only=0 on the new master.
Wed Aug 12 11:01:06 2015 - [info]  OK.
Wed Aug 12 11:01:06 2015 - [info] ** Finished master recovery successfully.
Wed Aug 12 11:01:06 2015 - [info] * Phase 3: Master Recovery Phase completed.
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] * Phase 4: Slaves Recovery Phase..
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] * Phase 4.1: Starting Parallel Slave Diff Log Generation Phase..
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] Log messages from 10.10.10.56 ...
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info]  This server has all relay logs. No need to generate diff files from the latest slave.
Wed Aug 12 11:01:06 2015 - [info] End of log messages from 10.10.10.56.
Wed Aug 12 11:01:06 2015 - [info] -- 10.10.10.56(10.10.10.56:3306) has the latest relay log events.
Wed Aug 12 11:01:06 2015 - [info] Generating relay diff files from the latest slave succeeded.
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] * Phase 4.2: Starting Parallel Slave Log Apply Phase..
Wed Aug 12 11:01:06 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] -- Slave recovery on host 10.10.10.56(10.10.10.56:3306) started, pid: 11795. Check tmp log /var/log/masterha/app1/10.10.10.56_3306_20150812110103.log if it takes time..
Wed Aug 12 11:01:07 2015 - [info]
Wed Aug 12 11:01:07 2015 - [info] Log messages from 10.10.10.56 ...
Wed Aug 12 11:01:07 2015 - [info]
Wed Aug 12 11:01:06 2015 - [info] Sending binlog..
Wed Aug 12 11:01:07 2015 - [info] Starting recovery on 10.10.10.56(10.10.10.56:3306)..
Wed Aug 12 11:01:07 2015 - [info]  Generating diffs succeeded.
Wed Aug 12 11:01:07 2015 - [info] Waiting until all relay logs are applied.
Wed Aug 12 11:01:07 2015 - [info]  done.
Wed Aug 12 11:01:07 2015 - [info] Getting slave status..
Wed Aug 12 11:01:07 2015 - [info] This slave(10.10.10.56)'s Exec_Master_Log_Pos equals to Read_Master_Log_Pos(mysql-bin.000008:120). No need to recover from Exec_Master_Log_Pos.
Wed Aug 12 11:01:07 2015 - [info] Connecting to the target slave host 10.10.10.56, running recover script..
Wed Aug 12 11:01:07 2015 - [info] Executing command: apply_diff_relay_logs --command=apply --slave_user='mha' --slave_host=10.10.10.56 --slave_ip=10.10.10.56  --slave_port=3306 --apply_files=/tmp/saved_master_binlog_from_10.10.10.58_3306_20150812110103.binlog --workdir=/tmp --target_version=5.6.26-log --timestamp=20150812110103 --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.56 --slave_pass=xxx
Wed Aug 12 11:01:07 2015 - [info]
MySQL client version is 5.6.26. Using --binary-mode.
Applying differential binary/relay log files /tmp/saved_master_binlog_from_10.10.10.58_3306_20150812110103.binlog on 10.10.10.56:3306. This may take long time...
Applying log files succeeded.
Wed Aug 12 11:01:07 2015 - [info]  All relay logs were successfully applied.
Wed Aug 12 11:01:07 2015 - [info]  Resetting slave 10.10.10.56(10.10.10.56:3306) and starting replication from the new master 10.10.10.57(10.10.10.57:3306)..
Wed Aug 12 11:01:07 2015 - [info]  Executed CHANGE MASTER.
Wed Aug 12 11:01:07 2015 - [info]  Slave started.
Wed Aug 12 11:01:07 2015 - [info] End of log messages from 10.10.10.56.
Wed Aug 12 11:01:07 2015 - [info] -- Slave recovery on host 10.10.10.56(10.10.10.56:3306) succeeded.
Wed Aug 12 11:01:07 2015 - [info] All new slave servers recovered successfully.
Wed Aug 12 11:01:07 2015 - [info]
Wed Aug 12 11:01:07 2015 - [info] * Phase 5: New master cleanup phase..
Wed Aug 12 11:01:07 2015 - [info]
Wed Aug 12 11:01:07 2015 - [info] Resetting slave info on the new master..
Wed Aug 12 11:01:07 2015 - [info]  10.10.10.57: Resetting slave info succeeded.
Wed Aug 12 11:01:07 2015 - [info] Master failover to 10.10.10.57(10.10.10.57:3306) completed successfully.
Wed Aug 12 11:01:07 2015 - [info] Deleted server3 entry from /etc/mha/app1.cnf .
Wed Aug 12 11:01:07 2015 - [info]

----- Failover Report -----

app1: MySQL Master failover 10.10.10.58(10.10.10.58:3306) to 10.10.10.57(10.10.10.57:3306) succeeded

Master 10.10.10.58(10.10.10.58:3306) is down!

Check MHA Manager logs at rd-mysql-test4:/var/log/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 10.10.10.58(10.10.10.58:3306)
The latest slave 10.10.10.56(10.10.10.56:3306) has all relay logs for recovery.
Selected 10.10.10.57(10.10.10.57:3306) as a new master.
10.10.10.57(10.10.10.57:3306): OK: Applying all logs succeeded.
10.10.10.57(10.10.10.57:3306): OK: Activated master IP address.
10.10.10.56(10.10.10.56:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.10.10.56(10.10.10.56:3306): OK: Applying all logs succeeded. Slave started, replicating from 10.10.10.57(10.10.10.57:3306)
10.10.10.57(10.10.10.57:3306): Resetting slave info succeeded.
Master failover to 10.10.10.57(10.10.10.57:3306) completed successfully.

当failover完成后,我们观察VIP已经在新的master上了。

[root@rd-mysql-test2 mha4mysql-node-0.56]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:87:37:af brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.57/16 brd 10.10.255.255 scope global eth0
    inet 10.10.10.60/8 brd 10.255.255.255 scope global eth0:1
    inet6 fe80::250:56ff:fe87:37af/64 scope link 
       valid_lft forever preferred_lft forever
注意:1.可能在生产环境中ssh端口不为22,通过在配置文件配置ssh_port=XXXX,由于版本的问题,某些脚本可能不支持非22端口。

    2.引入vip后,我们需要修改master_ip_online_failover脚本,因为我们在使用masterha_master_switch实现切换时会在配置文件中配置master_ip_online_failover_script

4.master_ip_online_change

前面我们在使用masterha_master_swith实现master切换时会使用master_ip_online_failover进行锁表使老master不再写入数据从而实现在切换过程中数据库的一致性。在上面提到的ha环境中,failover过程中通过master_ip_failover实现vip漂移,但若在在线迁移过程中,就需要用到master_ip_online_change实现vip漂移了,当然还得需要我们修改脚本。

ps:仍然注释掉(FIXME_xxx_drop_app_user($orig_master_handler);FIXME_xxx_create_app_user($new_master_handler);)

修改后的脚本如下(修改处已用###标注):

use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;
my $_tstart;
my $_running_interval = 0.1;
my (
  $command,              $orig_master_is_new_slave, $orig_master_host,
  $orig_master_ip,       $orig_master_port,         $orig_master_user,
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,
  $new_master_ip,        $new_master_port,          $new_master_user,
  $new_master_password,  $new_master_ssh_user,
);

###########################################################################
my $vip = '10.10.10.60';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key $vip down";
###########################################################################

GetOptions(
  'command=s'                => \$command,
  'orig_master_is_new_slave' => \$orig_master_is_new_slave,
  'orig_master_host=s'       => \$orig_master_host,
  'orig_master_ip=s'         => \$orig_master_ip,
  'orig_master_port=i'       => \$orig_master_port,
  'orig_master_user=s'       => \$orig_master_user,
  'orig_master_password=s'   => \$orig_master_password,
  'orig_master_ssh_user=s'   => \$orig_master_ssh_user,
  'new_master_host=s'        => \$new_master_host,
  'new_master_ip=s'          => \$new_master_ip,
  'new_master_port=i'        => \$new_master_port,
  'new_master_user=s'        => \$new_master_user,
  'new_master_password=s'    => \$new_master_password,
  'new_master_ssh_user=s'    => \$new_master_ssh_user,
);
exit &main();
sub current_time_us {
  my ( $sec, $microsec ) = gettimeofday();
  my $curdate = localtime($sec);
  return $curdate . " " . sprintf( "%06d", $microsec );
}
sub sleep_until {
  my $elapsed = tv_interval($_tstart);
  if ( $_running_interval > $elapsed ) {
    sleep( $_running_interval - $elapsed );
  }
}
sub get_threads_util {
  my $dbh                    = shift;
  my $my_connection_id       = shift;
  my $running_time_threshold = shift;
  my $type                   = shift;
  $running_time_threshold = 0 unless ($running_time_threshold);
  $type                   = 0 unless ($type);
  my @threads;
  my $sth = $dbh->prepare("SHOW PROCESSLIST");
  $sth->execute();
  while ( my $ref = $sth->fetchrow_hashref() ) {
    my $id         = $ref->{Id};
    my $user       = $ref->{User};
    my $host       = $ref->{Host};
    my $command    = $ref->{Command};
    my $state      = $ref->{State};
    my $query_time = $ref->{Time};
    my $info       = $ref->{Info};
    $info =~ s/^\s*(.*?)\s*$/$1/ if defined($info);
    next if ( $my_connection_id == $id );
    next if ( defined($query_time) && $query_time < $running_time_threshold );
    next if ( defined($command)    && $command eq "Binlog Dump" );
    next if ( defined($user)       && $user eq "system user" );
    next
      if ( defined($command)
      && $command eq "Sleep"
      && defined($query_time)
      && $query_time >= 1 );
    if ( $type >= 1 ) {
      next if ( defined($command) && $command eq "Sleep" );
      next if ( defined($command) && $command eq "Connect" );
    }
    if ( $type >= 2 ) {
      next if ( defined($info) && $info =~ m/^select/i );
      next if ( defined($info) && $info =~ m/^show/i );
    }
    push @threads, $ref;
  }
  return @threads;
}
sub main {
  if ( $command eq "stop" ) {
    ## Gracefully killing connections on the current master
    # 1. Set read_only= 1 on the new master
    # 2. DROP USER so that no app user can establish new connections
    # 3. Set read_only= 1 on the current master
    # 4. Kill current queries
    # * Any database access failure will result in script die.
    my $exit_code = 1;
    eval {
      ## Setting read_only=1 on the new master (to avoid accident)
      my $new_master_handler = new MHA::DBHelper();
      # args: hostname, port, user, password, raise_error(die_on_error)_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      print current_time_us() . " Set read_only on the new master.. ";
      $new_master_handler->enable_read_only();
      if ( $new_master_handler->is_read_only() ) {
        print "ok.\n";
      }
      else {
        die "Failed!\n";
      }
      $new_master_handler->disconnect();
      # Connecting to the orig master, die if any database error happens
      my $orig_master_handler = new MHA::DBHelper();
      $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
        $orig_master_user, $orig_master_password, 1 );
      ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
      $orig_master_handler->disable_log_bin_local();
      print current_time_us() . " Drpping app user on the orig master..\n";
###########################################################################
      #FIXME_xxx_drop_app_user($orig_master_handler);
###########################################################################
      ## Waiting for N * 100 milliseconds so that current connections can exit
      my $time_until_read_only = 15;
      $_tstart = [gettimeofday];
      my @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_read_only > 0 && $#threads >= 0 ) {
        if ( $time_until_read_only % 5 == 0 ) {
          printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)\n",
            current_time_us(), $#threads + 1, $time_until_read_only * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_read_only--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }
      ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
      print current_time_us() . " Set read_only=1 on the orig master.. ";
      $orig_master_handler->enable_read_only();
      if ( $orig_master_handler->is_read_only() ) {
        print "ok.\n";
      }
      else {
        die "Failed!\n";
      }
      ## Waiting for M * 100 milliseconds so that current update queries can complete
      my $time_until_kill_threads = 5;
      @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
        if ( $time_until_kill_threads % 5 == 0 ) {
          printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)\n",
            current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "\n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_kill_threads--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }
###########################################################################
      print "disable the VIP on old master: $orig_master_host \n";
      &stop_vip();
###########################################################################
      ## Terminating all threads
      print current_time_us() . " Killing all application threads..\n";
      $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
      print current_time_us() . " done.\n";
      $orig_master_handler->enable_log_bin_local();
      $orig_master_handler->disconnect();
      ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    ## Activating master ip on the new master
    # 1. Create app user with write privileges
    # 2. Moving backup script if needed
    # 3. Register new master's ip to the catalog database
    my $exit_code = 10;
    eval {
      my $new_master_handler = new MHA::DBHelper();
      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print current_time_us() . " Set read_only=0 on the new master.\n";
      $new_master_handler->disable_read_only();
      ## Creating an app user on the new master
      print current_time_us() . " Creating app user on the new master..\n";
###########################################################################
      #FIXME_xxx_create_app_user($new_master_handler);
###########################################################################
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();
      ## Update master ip on the catalog database, etc
###############################################################################
      print "enable the VIP: $vip on the new master: $new_master_host \n ";
      &start_vip();
###############################################################################
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {
    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}
###########################################################################
sub start_vip() {
	`ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
	`ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
###########################################################################
sub usage {
  print
"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";
  die;
}
过程如下:

[root@rd-mysql-test4 mha]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf --new_master_host=10.10.10.56 --orig_master_is_new_slave --running_updates_limit=1000000
Wed Aug 12 14:11:23 2015 - [info] MHA::MasterRotate version 0.56.
Wed Aug 12 14:11:23 2015 - [info] Starting online master switch..
Wed Aug 12 14:11:23 2015 - [info] 
Wed Aug 12 14:11:23 2015 - [info] * Phase 1: Configuration Check Phase..
Wed Aug 12 14:11:23 2015 - [info] 
Wed Aug 12 14:11:23 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Aug 12 14:11:23 2015 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Wed Aug 12 14:11:23 2015 - [info] Reading server configuration from /etc/mha/app1.cnf..
Wed Aug 12 14:11:23 2015 - [info] GTID failover mode = 0
Wed Aug 12 14:11:23 2015 - [info] Current Alive Master: 10.10.10.57(10.10.10.57:3306)
Wed Aug 12 14:11:23 2015 - [info] Alive Slaves:
Wed Aug 12 14:11:23 2015 - [info]   10.10.10.56(10.10.10.56:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 14:11:23 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)
Wed Aug 12 14:11:23 2015 - [info]   10.10.10.58(10.10.10.58:3306)  Version=5.6.25-log (oldest major version between slaves) log-bin:enabled
Wed Aug 12 14:11:23 2015 - [info]     Replicating from 10.10.10.57(10.10.10.57:3306)
Wed Aug 12 14:11:23 2015 - [info]     Primary candidate for the new Master (candidate_master is set)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 10.10.10.57(10.10.10.57:3306)? (YES/no): yes
Wed Aug 12 14:11:24 2015 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Wed Aug 12 14:11:24 2015 - [info]  ok.
Wed Aug 12 14:11:24 2015 - [info] Checking MHA is not monitoring or doing failover..
Wed Aug 12 14:11:24 2015 - [info] Checking replication health on 10.10.10.56..
Wed Aug 12 14:11:24 2015 - [info]  ok.
Wed Aug 12 14:11:24 2015 - [info] Checking replication health on 10.10.10.58..
Wed Aug 12 14:11:24 2015 - [info]  ok.
Wed Aug 12 14:11:24 2015 - [info] 10.10.10.56 can be new master.
Wed Aug 12 14:11:24 2015 - [info] 
From:
10.10.10.57(10.10.10.57:3306) (current master)
 +--10.10.10.56(10.10.10.56:3306)
 +--10.10.10.58(10.10.10.58:3306)

To:
10.10.10.56(10.10.10.56:3306) (new master)
 +--10.10.10.58(10.10.10.58:3306)
 +--10.10.10.57(10.10.10.57:3306)

Starting master switch from 10.10.10.57(10.10.10.57:3306) to 10.10.10.56(10.10.10.56:3306)? (yes/NO): yes
Wed Aug 12 14:11:25 2015 - [info] Checking whether 10.10.10.56(10.10.10.56:3306) is ok for the new master..
Wed Aug 12 14:11:25 2015 - [info]  ok.
Wed Aug 12 14:11:25 2015 - [info] 10.10.10.57(10.10.10.57:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Wed Aug 12 14:11:25 2015 - [info] 10.10.10.57(10.10.10.57:3306): Resetting slave pointing to the dummy host.
Wed Aug 12 14:11:25 2015 - [info] ** Phase 1: Configuration Check Phase completed.
Wed Aug 12 14:11:25 2015 - [info] 
Wed Aug 12 14:11:25 2015 - [info] * Phase 2: Rejecting updates Phase..
Wed Aug 12 14:11:25 2015 - [info] 
Wed Aug 12 14:11:25 2015 - [info] Executing master ip online change script to disable write on the current master:
Wed Aug 12 14:11:25 2015 - [info]   /usr/local/bin/master_ip_online_change --command=stop --orig_master_host=10.10.10.57 --orig_master_ip=10.10.10.57 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='cityhouse' --new_master_host=10.10.10.56 --new_master_ip=10.10.10.56 --new_master_port=3306 --new_master_user='mha' --new_master_password='cityhouse' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave
Wed Aug 12 14:11:25 2015 710013 Set read_only on the new master.. ok.
Wed Aug 12 14:11:25 2015 711731 Drpping app user on the orig master..
Wed Aug 12 14:11:25 2015 712040 Waiting all running 1 threads are disconnected.. (max 1500 milliseconds)
{'Time' => '415248','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Wed Aug 12 14:11:26 2015 215501 Waiting all running 1 threads are disconnected.. (max 1000 milliseconds)
{'Time' => '415248','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Wed Aug 12 14:11:26 2015 713698 Waiting all running 1 threads are disconnected.. (max 500 milliseconds)
{'Time' => '415249','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Wed Aug 12 14:11:27 2015 214327 Set read_only=1 on the orig master.. ok.
Wed Aug 12 14:11:27 2015 214975 Waiting all running 1 queries are disconnected.. (max 500 milliseconds)
{'Time' => '415249','Command' => 'Daemon','db' => undef,'Id' => '1','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
disable the VIP on old master: 10.10.10.57 
Wed Aug 12 14:11:27 2015 790437 Killing all application threads..
Wed Aug 12 14:11:27 2015 791066 done.
Wed Aug 12 14:11:27 2015 - [info]  ok.
Wed Aug 12 14:11:27 2015 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Wed Aug 12 14:11:27 2015 - [info] Executing FLUSH TABLES WITH READ LOCK..
Wed Aug 12 14:11:27 2015 - [info]  ok.
Wed Aug 12 14:11:27 2015 - [info] Orig master binlog:pos is mysql-bin.000010:120.
Wed Aug 12 14:11:27 2015 - [info]  Waiting to execute all relay logs on 10.10.10.56(10.10.10.56:3306)..
Wed Aug 12 14:11:27 2015 - [info]  master_pos_wait(mysql-bin.000010:120) completed on 10.10.10.56(10.10.10.56:3306). Executed 0 events.
Wed Aug 12 14:11:27 2015 - [info]   done.
Wed Aug 12 14:11:27 2015 - [info] Getting new master's binlog name and position..
Wed Aug 12 14:11:27 2015 - [info]  mysql-bin.000008:120
Wed Aug 12 14:11:27 2015 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='10.10.10.56', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=120, MASTER_USER='rep', MASTER_PASSWORD='xxx';
Wed Aug 12 14:11:27 2015 - [info] Executing master ip online change script to allow write on the new master:
Wed Aug 12 14:11:27 2015 - [info]   /usr/local/bin/master_ip_online_change --command=start --orig_master_host=10.10.10.57 --orig_master_ip=10.10.10.57 --orig_master_port=3306 --orig_master_user='mha' --orig_master_password='cityhouse' --new_master_host=10.10.10.56 --new_master_ip=10.10.10.56 --new_master_port=3306 --new_master_user='mha' --new_master_password='cityhouse' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave
Wed Aug 12 14:11:27 2015 863530 Set read_only=0 on the new master.
Wed Aug 12 14:11:27 2015 863991 Creating app user on the new master..
enable the VIP: 10.10.10.60 on the new master: 10.10.10.56 
 Wed Aug 12 14:11:27 2015 - [info]  ok.
Wed Aug 12 14:11:27 2015 - [info] 
Wed Aug 12 14:11:27 2015 - [info] * Switching slaves in parallel..
Wed Aug 12 14:11:27 2015 - [info] 
Wed Aug 12 14:11:27 2015 - [info] -- Slave switch on host 10.10.10.58(10.10.10.58:3306) started, pid: 12018
Wed Aug 12 14:11:27 2015 - [info] 
Wed Aug 12 14:11:28 2015 - [info] Log messages from 10.10.10.58 ...
Wed Aug 12 14:11:28 2015 - [info] 
Wed Aug 12 14:11:27 2015 - [info]  Waiting to execute all relay logs on 10.10.10.58(10.10.10.58:3306)..
Wed Aug 12 14:11:27 2015 - [info]  master_pos_wait(mysql-bin.000010:120) completed on 10.10.10.58(10.10.10.58:3306). Executed 0 events.
Wed Aug 12 14:11:27 2015 - [info]   done.
Wed Aug 12 14:11:27 2015 - [info]  Resetting slave 10.10.10.58(10.10.10.58:3306) and starting replication from the new master 10.10.10.56(10.10.10.56:3306)..
Wed Aug 12 14:11:27 2015 - [info]  Executed CHANGE MASTER.
Wed Aug 12 14:11:27 2015 - [info]  Slave started.
Wed Aug 12 14:11:28 2015 - [info] End of log messages from 10.10.10.58 ...
Wed Aug 12 14:11:28 2015 - [info] 
Wed Aug 12 14:11:28 2015 - [info] -- Slave switch on host 10.10.10.58(10.10.10.58:3306) succeeded.
Wed Aug 12 14:11:28 2015 - [info] Unlocking all tables on the orig master:
Wed Aug 12 14:11:28 2015 - [info] Executing UNLOCK TABLES..
Wed Aug 12 14:11:28 2015 - [info]  ok.
Wed Aug 12 14:11:28 2015 - [info] Starting orig master as a new slave..
Wed Aug 12 14:11:28 2015 - [info]  Resetting slave 10.10.10.57(10.10.10.57:3306) and starting replication from the new master 10.10.10.56(10.10.10.56:3306)..
Wed Aug 12 14:11:28 2015 - [info]  Executed CHANGE MASTER.
Wed Aug 12 14:11:28 2015 - [info]  Slave started.
Wed Aug 12 14:11:28 2015 - [info] All new slave servers switched successfully.
Wed Aug 12 14:11:28 2015 - [info] 
Wed Aug 12 14:11:28 2015 - [info] * Phase 5: New master cleanup phase..
Wed Aug 12 14:11:28 2015 - [info] 
Wed Aug 12 14:11:28 2015 - [info]  10.10.10.56: Resetting slave info succeeded.
Wed Aug 12 14:11:28 2015 - [info] Switching master to 10.10.10.56(10.10.10.56:3306) completed successfully.
注:1.在执行过程中若报一下错误:
Wed Aug 12 14:05:12 2015 - [error][/usr/local/share/perl5/MHA/MasterRotate.pm, ln262] We should not start online master switch when one of connections are running long queries on the new master(10.10.10.56(10.10.10.56:3306)). Currently 1 thread(s) are running.
Details:
{'Time' => '93075','Command' => 'Daemon','db' => undef,'Id' => '29','Info' => undef,'User' => 'event_scheduler','State' => 'Waiting on empty queue','Host' => 'localhost'}
Wed Aug 12 14:05:12 2015 - [error][/usr/local/share/perl5/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/local/bin/masterha_master_switch line 53

这是由于new master上的event_scheduler导致,我们可以临时关闭下event_scheduler通过set global event_scheduler=0;待迁移完成后再开启。

2.此过程中的注意事项请参考上面masterha_master_switch调用master_ip_online_change


好了,内容不少,需要我们自己慢慢消化。

可以参考官网:https://code.google.com/p/mysql-master-ha/wiki/Parameters

版权声明:本文为博主原创文章,未经博主允许不得转载。

mysql高可用之mha(补充1)

标签:mysql mha   masterha_master_swit   master_ip_faliover   master_ip_online_cha   masterha_conf_host   

原文地址:http://blog.csdn.net/yanggd1987/article/details/47414739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!