當主庫意外宕機(master_state=dead)時,MHA 支援透過指定參數強制提升指定 Slave 為新 Master。本文模擬 10.1.1.231 主庫故障,手動將 10.1.1.234 提升為新主庫的完整操作紀錄。
masterha_master_switch --master_state=dead \
--conf=/etc/masterha/app1.cnf \
--dead_master_host=10.1.1.231 --dead_master_ip=10.1.1.231 --dead_master_port=63306 \
--new_master_host=10.1.1.234 --new_master_ip=10.1.1.234 --new_master_port=63306
關鍵參數解析:
--master_state=dead:宣告舊 Master 已不可用--dead_master_*:舊 Master 連線資訊(供 MHA 嘗試連線確認)--new_master_*:目標提升節點資訊----- Failover Report -----
app1: MySQL Master failover 10.1.1.231 to 10.1.1.234 succeeded
Master 10.1.1.231 is down!
Started manual(interactive) failover.
The latest slave 10.1.1.234(10.1.1.234:63306) has all relay logs for recovery.
Selected 10.1.1.234 as a new master.
10.1.1.234: OK: Applying all logs succeeded.
Generating relay diff files from the latest slave succeeded.
10.1.1.234: Resetting slave info succeeded.
Master failover to 10.1.1.234(10.1.1.234:63306) completed successfully.
日誌顯示 MHA 自動完成:Relay Log 恢復、Binlog 適用、Slave 元數據重置,全程無需人工干預數據一致性。
mysql> SHOW MASTER STATUS;
+------------------------+----------+--------------+--------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+--------------------------------------+-------------------+
| mysql-slave-bin.000011 | 120 | denovo_ng | mysql,denovo,test,information_schema | |
+------------------------+----------+--------------+--------------------------------------+-------------------+
注意:新 Master 繼續使用原 Slave 的 Binlog 文件名(mysql-slave-bin),此為正常現象。
故障主庫修復上線後,需將其重新配置為 Slave 掛載至新架構:
GRANT REPLICATION SLAVE ON *.* TO 'jpsync'@'10.1.1.231' IDENTIFIED BY 'jppasswd';
FLUSH PRIVILEGES;
# my.cnf 新增
skip_slave_start
# 刪除舊復制元數據
rm -f master.info relay-log.info
CHANGE MASTER TO
MASTER_HOST='10.1.1.234',
MASTER_PORT=63306,
MASTER_USER='jpsync',
MASTER_PASSWORD='jppasswd',
MASTER_LOG_FILE='mysql-master-bin.000011',
MASTER_LOG_POS=120;
START SLAVE;
SHOW SLAVE STATUS\G
# 確認雙 Yes
至此,故障切換與集群修復全流程結束,雙節點恢復高可用架構。