read-onlyread-only自增配置避免主鍵衝突:auto_increment_increment=3,Offset 分別為 1、2、3。
[mysqld]
server-id=1
log-bin=mysql-bin
log-slave-updates=1 # 關鍵:將從庫收到的 binlog 寫入自己的 binlog,供 C 同步
binlog-format=ROW
auto-increment-increment=3
auto-increment-offset=1
[mysqld]
server-id=2
log-bin=mysql-bin
log-slave-updates=1
binlog-format=ROW
auto-increment-increment=3
auto-increment-offset=2
read-only=1
[mysqld]
server-id=3
log-bin=mysql-bin
log-slave-updates=1
binlog-format=ROW
auto-increment-increment=3
auto-increment-offset=3
read-only=1
-- A 上執行
RESET MASTER;
CREATE USER 'repli_user'@'%' IDENTIFIED BY 'repli_pwd';
GRANT REPLICATION SLAVE ON *.* TO 'repli_user'@'%';
FLUSH PRIVILEGES;
-- 導出全量數據(含 binlog 位點)
mysqldump -uroot -p -P3307 -A -E -R --master-data=1 --single-transaction > master_A.sql
-- B 上執行
RESET SLAVE;
RESET MASTER;
CREATE USER 'repli_user'@'%' IDENTIFIED BY 'repli_pwd';
GRANT REPLICATION SLAVE ON *.* TO 'repli_user'@'%';
CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_USER='repli_user',
MASTER_PASSWORD='repli_pwd',
MASTER_PORT=3307;
-- 導入 A 的全量數據
mysql -uroot -p -P3308 < master_A.sql
START SLAVE;
SHOW SLAVE STATUS\G -- 確認 Slave_IO_Running=Yes, Slave_SQL_Running=Yes
-- B 備份供 C 使用
mysqldump -uroot -p -P3308 -A -E -R --master-data=1 --single-transaction > master_B.sql
-- C 上執行
RESET SLAVE;
CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_USER='repli_user',
MASTER_PASSWORD='repli_pwd',
MASTER_PORT=3308;
mysql -uroot -p -P3309 < master_B.sql
START SLAVE;
SHOW SLAVE STATUS\G
Seconds_Behind_Master 持續增長需排查慢 SQL、網絡、磁盤 IO。三級鏈式主從適用於「一寫多讀、跨機房容災」場景。核心在於 log-slave-updates 打通 binlog 傳遞鏈路,配合自增偏移量消除主鍵衝突。生產環境建議引入 MHA/Orchestrator 實現自動故障轉移。