MySQL Replication (主從同步) 是實現讀寫分離、災備、多源分析的核心基礎設施。本文基於 MySQL 5.0/5.7 通用原理,詳述雙機熱備 (Master-Master) 與標準單向 (Master-Slave) 完整配置流程。
主從同步涉及三個關鍵線程:
關鍵檔案:
mysql-bin.000001 (Binlog)、master.info (記錄同步位點)relay-bin.000001 (Relay Log)、master.info (主庫連線資訊)、relay-log.info (SQL 執行位點)| 角色 | IP | Hostname | Server ID | MySQL 版本 |
|---|---|---|---|---|
| Master (A) | 10.10.0.119 | db-master | 1 | 5.0.45 / 5.7.x |
| Slave (B) | 10.10.8.112 | db-slave | 2 | 5.0.45 / 5.7.x |
前置條件:
service iptables stop / ufw allow 3306sql_mode[mysqld]
server-id = 1 # 全局唯一,1-2^32-1
log-bin = mysql-bin # 啟用 Binlog,指定檔案前綴
binlog-do-db = test # 僅記錄 test 庫變更 (可選,建議全記錄再用 replicate-do-db 過濾)
# binlog-ignore-db = mysql # 忽略系統庫
sync_binlog = 1 # 每次提交刷盤,最高數據安全 (效能折衷可設 100-1000)
binlog_format = ROW # 強烈推薦 ROW 格式,避免非確定性函數導致主從不一致
expire_logs_days = 7 # 自動清理 7 天前 Binlog,防磁碟滿
max_binlog_size = 100M # 單檔大小限制
service mysqld restart
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'10.10.8.112' IDENTIFIED BY 'ReplPass123!';
-- 僅授予 REPLICATION SLAVE 權限,最小權限原則
-- 指定 Slave IP,禁止 '%'
FLUSH PRIVILEGES;
CREATE DATABASE test;
USE test;
CREATE TABLE mytest (username VARCHAR(20), password VARCHAR(20));
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 840
Binlog_Do_DB: test
Binlog_Ignore_DB:
Executed_Gtid_Set: (GTID 模式 5.6+ 才有)
1 row in set (0.00 sec)
[mysqld]
server-id = 2 # 唯一,不同於 Master
# 以下參數 5.6+ 建議用 CHANGE MASTER TO 動態設定,寫死配置檔不靈活
# master-host=10.10.0.119 # 已棄用
# master-user=repl_user
# master-password=ReplPass123!
# master-port=3306
# master-connect-retry=60
# replicate-do-db=test # 僅同步 test 庫
relay-log = relay-bin # 指定 Relay Log 前綴
relay-log-index = relay-bin.index
log-slave-updates = 1 # 關鍵:Slave 重放的變更也寫入自身 Binlog,支援鏈式/級聯同步
read-only = 1 # 建議:防止誤在 Slave 寫入導致不一致
service mysqld restart
mysql> STOP SLAVE;
mysql> RESET SLAVE ALL; -- 清除舊 master.info、relay-log.info (關鍵!避免舊位點干擾)
mysql> CHANGE MASTER TO
MASTER_HOST='10.10.0.119',
MASTER_USER='repl_user',
MASTER_PASSWORD='ReplPass123!',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003', -- 來自 Master SHOW MASTER STATUS
MASTER_LOG_POS=840,
MASTER_CONNECT_RETRY=60;
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.0.119
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 98
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 236
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes <-- 關鍵:I/O 線程正常
Slave_SQL_Running: Yes <-- 關鍵:SQL 線程正常
Replicate_Do_DB: test
Seconds_Behind_Master: 0 <-- 延遲秒數,0 為同步
Last_Error: <-- 空為無錯誤
1 row in set (0.00 sec)
-- Master 寫入
mysql> USE test; INSERT INTO mytest VALUES ('user1', 'pass1');
-- Slave 立即查詢
mysql> USE test; SELECT * FROM mytest;
+----------+----------+
| username | password |
+----------+----------+
| user1 | pass1 |
+----------+----------+
在單向同步基礎上,互為 Master/Slave:
-- 在原 Master (A) 上新增 Slave 配置,指向 B
CHANGE MASTER TO
MASTER_HOST='10.10.8.112',
MASTER_USER='repl_user',
MASTER_PASSWORD='ReplPass123!',
MASTER_LOG_FILE='mysql-bin.000001', -- B 的 Binlog 位置
MASTER_LOG_POS=120;
START SLAVE;
-- 在原 Slave (B) 上新增 Master 配置 (已有 log-bin)
-- 確保 B 的 my.cnf 有 log-bin, server-id=2
-- 雙方 auto_increment_offset/auto_increment_increment 設定避免主鍵衝突
-- A: auto_increment_offset=1, auto_increment_increment=2
-- B: auto_increment_offset=2, auto_increment_increment=2
警告:雙主寫入極易衝突,**生產極少純雙主寫入**,常見架構為「一主一備 + 讀寫分離」或「MHA/Orchestrator 自動故障轉移」。
| 現象 | 排查方向 | 解決 |
|---|---|---|
| Slave_IO_Running: No / Connecting | 網路不通、帳號密碼錯、Master 防火牆、MASTER_HOST/IP 錯 | telnet Master 3306;檢查 GRANT Host 匹配;查 Master error.log |
| Slave_SQL_Running: No | SQL 執行錯誤:主鍵衝突、外鍵失敗、字元集不符、非確定性函數 | 看 Last_Error、Last_SQL_Error;跳過錯誤 SET GLOBAL sql_slave_skip_counter=1; START SLAVE; (謹慎);修正數據後重啟 |
| Seconds_Behind_Master 偏大/NULL | Slave 機能不足、長事務、大查詢阻塞、網路延遲、Binlog 格式 | 優化 Slave 硬體/參數;改 ROW 格式;檢查長查詢;並行複制 (5.7+ slave_parallel_workers) |
| 主從數據不一致 | 直接在 Slave 寫入、Binlog 格式 STATEMENT 非確定函數、GTID 漏洞 | pt-table-checksum/pt-table-sync 校驗修復;強制 ROW 格式;啟用 GTID |
| Relay Log 損壞/遺失 | 磁碟故障、人為刪除 | STOP SLAVE; RESET SLAVE; CHANGE MASTER TO ... MASTER_LOG_FILE/pos 重新指定; START SLAVE; |
binlog_format=ROW,避免 90% 不一致根因。Slave_IO_Running=Yes、Slave_SQL_Running=Yes、Seconds_Behind_Master < 阈值。pt-table-checksum + pt-table-sync (Percona Toolkit)。STOP SLAVE) 或 xtrabackup 熱備,不影響主庫。MySQL 主從同步看似配置簡單,實則涉及網路、存儲、鎖、日誌格式、一致性協議等深層機制。掌握三線程模型、Binlog/Relay Log 流轉、關鍵狀態解讀、故障排查體系,才能構建可靠的資料庫高可用架構。