在單向主從基礎上,建立 雙向主從 (Circular Replication):A → B、B → A,實現雙寫入口。本文記錄具體設定步驟與關鍵風險提示。
Server A (Master) ──binlog──→ Server B (Slave)
↑ │
│ ↓
←────── binlog ──────── Server B (Master)
# my.cnf [mysqld]
log-bin
server-id = 2
# SQL
GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'10.1.1.1' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
# my.cnf [mysqld]
master-host = 10.1.1.2
master-user = replicate
master-password = password
server-id = 1
log-bin # 建議也開啟,便於 B 做 Slave 時有 binlog
# 兩台均執行
START SLAVE;
SHOW SLAVE STATUS\G
# 確認 Slave_IO_Running=Yes, Slave_SQL_Running=Yes
auto_increment_increment=2 + auto_increment_offset=1/2 (A 用 1, B 用 2)server_id 在 binlog 標記來源,遇到自己的 server_id 會忽略,單層循環安全log_slave_updates=ON 並小心設計pt-table-sync 定期校驗修復| 方案 | 特點 |
|---|---|
| MySQL Group Replication (5.7.17+) | Paxos 協議、多主/單主、自動故障切換、一致性保證 |
| Galera Cluster (MariaDB/Percona) | 同步多主、認證式複寫、零資料遺失 |
| MySQL InnoDB Cluster | Group Replication + Router + Shell 一體化 |