| 角色 | IP | OS | MySQL 版本 | 安裝路徑 |
|---|---|---|---|---|
| Master | 10.0.0.101 | Windows Server 2003 | 5.1.47 (必須 ≤ Slave 版本) | D:\MySQLServer5.1 |
| Slave | 10.0.0.102 | Windows Server 2003 | 5.1.47 (建議 ≥ Master) | D:\MySQLServer5.1 |
版本相容性鐵律:
✅ Master 5.0 → Slave 5.1/5.5/5.6/5.7/8.0 (向上相容)
❌ Master 5.1 → Slave 5.0 (向下不相容)
❌ Master 5.0.4 → Slave 5.0.3 (Binlog 格式變更)
生產建議:Master/Slave 同主版本 (如同為 5.7.x 或 8.0.x),避免跨大版本複制。
[mysqld]
server-id = 1 # 唯一,1-2^32-1
log-bin = mysql-bin # 啟用 Binlog,檔案前綴
binlog-format = ROW # 5.1+ 推薦 ROW,減少主從不一致
binlog-do-db = test # 僅記錄 test 庫 (可多個,逗號分隔)
binlog-ignore-db = mysql # 不記錄系統庫
expire_logs_days = 7 # 自動清理 Binlog
max_binlog_size = 100M # 單檔大小上限
sync_binlog = 1 # 每次提交 fsync,最安全
innodb_flush_log_at_trx_commit = 1
修改後:net stop mysql && net start mysql
-- 僅允許 Slave IP 段連線
CREATE USER 'repl'@'10.0.0.%' IDENTIFIED BY 'ReplP@ssw0rd!';
-- 必須權限
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'10.0.0.%';
FLUSH PRIVILEGES;
驗證:SHOW GRANTS FOR 'repl'@'10.0.0.%';
[mysqld]
server-id = 2 # 與 Master 不同
master-host = 10.0.0.101 # Master IP (5.1 僅支援配置檔方式)
master-port = 3306
master-user = repl
master-password = ReplP@ssw0rd!
replicate-do-db = test # 僅複制 test 庫
# replicate-ignore-db = mysql # 可選
relay-log = relay-bin # Relay Log 檔案前綴
relay-log-index = relay-bin.index
read-only = ON # 防止誤寫入 Slave (5.7+ 建議)
skip-slave-start = ON # 啟動不自動拉取,手動控制
⚠️ 5.1 版本特有:master-host/user/password 僅支援寫在 my.ini;5.5+ 建議改用 CHANGE MASTER TO 動態配置,不寫密碼明文。
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS; -- 記錄 File、Position
# 另開視窗備份
mysqldump -u root -p --single-transaction --master-data=2 test > test_master.sql
UNLOCK TABLES;
mysql -u root -p test < test_master.sql
net start mysql
-- 登入 Slave 驗證
SHOW SLAVE STATUS\G
-- 關鍵指標
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
5.5+ 動態配置方式 (不寫密碼於配置檔):
CHANGE MASTER TO
MASTER_HOST='10.0.0.101',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='ReplP@ssw0rd!',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=123456;
START SLAVE;
-- Master 執行
USE test;
CREATE TABLE t_repl (id INT PRIMARY KEY AUTO_INCREMENT, val VARCHAR(20));
INSERT INTO t_repl (val) VALUES ('from_master_1'), ('from_master_2');
-- Slave 查詢
USE test;
SELECT * FROM t_repl;
-- 應看到兩行資料,證明複制正常
| 錯誤現象 | 原因 | 解決 |
|---|---|---|
Slave_IO_Running: No Last_IO_Error: error connecting to master | 網路不通、帳號密碼錯、Master 防火牆、bind-address 限制 | telnet 3306、檢查帳號 Host、Master GRANT、防火牆放行 |
Slave_SQL_Running: No Last_SQL_Error: Duplicate entry '1' for key 'PRIMARY' | 主鍵衝突、Slave 手動寫入、多主循環複制 | SET GLOBAL sql_slave_skip_counter=1; START SLAVE; 跳過單條、或重新同步 |
Seconds_Behind_Master 持續增大 | 大事務、DDL、Slave 單執行緒瓶頸、磁碟慢 | 拆大事務、5.7+ 開啟 MTS (slave_parallel_workers)、升級硬體 |
ERROR 1189: Net error reading from master (LOAD DATA FROM MASTER) | 非 MyISAM 表、大表鎖表超時 | 改用 mysqldump/XtraBackup 初始化、勿用 LOAD DATA FROM MASTER |
sql_modesync_binlog=1、innodb_flush_log_at_trx_commit=1read_only=ON、super_read_only=ON 5.7+)Seconds_Behind_Master、Slave_IO/SQL_Running、Relay Log 積壓、主從校驗 (pt-table-checksum)--single-transaction --master-data=2LOAD DATA FROM MASTER,僅支援 MyISAM、會全域鎖表、已棄用