**MMM (Master-Master Replication Manager for MySQL)** 是一套用 Perl 編寫的腳本工具,專為 **雙主復制** 環境設計,提供:
⚠️ **核心限制**:同一時刻**只允許一個主寫入**(Writer Role 獨占),另一主作熱備;**不保證強一致性**,不適合金融核心業務,適合「高可用優於強一致」的互聯網業務。
| 角色 | Hostname | IP | MySQL 角色 | MMM 角色 |
|---|---|---|---|---|
| DB1 | data01 | 192.168.52.129 | Master | Master (Writer 候選) |
| DB2 | data02 | 192.168.52.128 | Master | Master (Writer 候選) |
| DB3 | oraclem1 | 192.168.52.131 | Slave | Slave + Monitor Agent |
**VIP 規劃**:Writer VIP 192.168.52.120,Reader VIP 池 192.168.52.129/128/131。
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user repl
replication_password repl_1234
agent_user mmm_agent
agent_password mmm_agent_1234
</host>
<host db1>
ip 192.168.52.129
mode master
peer db2
</host>
<host db2>
ip 192.168.52.128
mode master
peer db1
</host>
<host db3>
ip 192.168.52.131
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.52.120
mode exclusive
</role>
<role reader>
hosts db1, db2, db3
ips 192.168.52.129, 192.168.52.128, 192.168.52.131
mode balanced
</role>
關鍵點:mode exclusive 保證 Writer 單點寫入;peer 指定雙主對端;replication_user 需具備 REPLICATION SLAVE 權限。
每台機器只需兩行:
include mmm_common.conf
this db1 # db2/db3 對應修改
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.52.129, 192.168.52.128, 192.168.52.131
</monitor>
<host default>
monitor_user mmm_monitor
monitor_password mmm_monitor_1234
</host>
debug 0
-- 復制賬號
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.52.%' IDENTIFIED BY 'repl_1234';
-- Monitor 賬號
GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.52.%' IDENTIFIED BY 'mmm_monitor_1234';
-- Agent 賬號(需改 read_only、切換主從)
GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.52.%' IDENTIFIED BY 'mmm_agent_1234';
SHOW MASTER STATUS 獲取 File/Position,DB2/DB3 執行 CHANGE MASTER TO ... 指向 DB1。SHOW SLAVE STATUS\G 確保 Slave_IO_Running: Yes、Slave_SQL_Running: Yes、Seconds_Behind_Master: 0。# 三節點啟動 Agent
/etc/init.d/mysql-mmm-agent start
# Monitor 節點啟動 Monitor
chkconfig --add /etc/rc.d/init.d/mysql-mmm-monitor
service mysql-mmm-monitor start
# 查看狀態
mmm_control show
# 節點上線
mmm_control set_online db1
mmm_control set_online db2
mmm_control set_online db3
# 健康檢查
mmm_control checks
# 查看日誌
tail -f /var/log/mysql-mmm/mmm_mond.log
service mysql stop。Check 'mysql' on 'db1' has failed for 10 seconds! → State changed to HARD_OFFLINE → Orphaned role 'writer' assigned to 'db2'。mmm_control show 顯示 DB2 承擔 Writer Role,VIP 192.168.52.120 已綁定 DB2 eth0。mmm_control set_online db1。Orphaned role 'writer' assigned to 'db1',VIP 漂移回 DB1。Agent 收到 set_active_master 後:
Master_Log_File/Read_Master_Log_Pos;若 Old Master 可連,再取其 SHOW MASTER STATUS 以 Old Master 為準。SELECT MASTER_POS_WAIT(...) 追趕同步位置。STOP SLAVE; CHANGE MASTER TO ...; START SLAVE; 重建復制鏈路。auto_increment_increment/offset **必配**,避免雙主主鍵衝突。mmm_control checks 巡檢,監控 rep_backlog 延遲指標。MMM 以輕量級腳本實現了 MySQL 雙主高可用的核心閉環:**健康檢查 → 故障判定 → 角色轉移 → VIP 漂移 → 復制重建**。雖不保證強一致,但在「可用性優先、數據最終一致可接受」的業務場景下,是極具性價比的選擇。