slave agent收到new master发送的set_active_master信息后如何进行切换主库
源码 Agent\Helpers\Actions.pm
set_active_master($new_master)
Try to catch up with the old master as far as possible and change the master to the new host.
(Syncs to the master log if the old master is reachable. Otherwise syncs to the relay log.)
一、获取同步的binlog和position
先获取new master的show slave status中的 Master_Log_File、Read_Master_Log_Pos
wait_log=Master_Log_File, wait_pos=Read_Master_Log_Pos
如果old master可以连接,再获取old master的show master status中的File、Position
wait_log=File, wait_pos=Position # 覆盖new master的slave信息,以old master 为准
二、slave追赶old master的同步位置
SELECT MASTER_POS_WAIT('wait_log', wait_pos);
# 停止同步
STOP SLAVE;
三、设置new master信息
# 此时 new master已经对外提供写操作。
# (在main线程里new master先接收到激活的消息,new master 转换(包含vip操作)完成后,然后由_distribute_roles将master变动同步到slave上)
# 在new master转换完成后,如果能执行flush logs,更方便管理
# 获取new master的binlog数据。
SHOW MASTER STATUS;
# 从配置文件/etc/mysql-mmm/mmm_common.conf 读取同步帐户、密码
replication_user、replication_password
# 设置新的同步位置
CHANGE MASTER TO MASTER_HOST='$new_peer_host', MASTER_PORT=$new_peer_port
, MASTER_USER='$repl_user', MASTER_PASSWORD='$repl_password'
, MASTER_LOG_FILE='$master_log', MASTER_LOG_POS=$master_pos;
# 开启同步
START SLAVE;
====== 潜在问题
双主的binlog必须完全一致,否则切换会出问题
如果new master比slave的同步位置晚,会导致重复执行。只有row模式下才能保证数据一致性
如有错误,欢迎指正