repl_user,主機:192.168.0.9(或 %)
# Windows 加入 MySQL bin 目錄到 PATH 環境變量
mysql -h 192.168.0.8 -u repl_user -p
# 輸入密碼,成功進入 mysql> 表示連通正常


[mysqld]
server-id = 1
log-bin = mysql-bin
# 可選:binlog-do-db = sync_db # 僅同步指定庫
# 可選:binlog-ignore-db = mysql # 忽略系統庫
重啟 MySQL,驗證:
mysql -u root -p -e "SHOW MASTER STATUS;"
# 記錄 File (如 mysql-bin.000001) 和 Position (如 499)


[mysqld]
server-id = 2
master-host = 192.168.0.8
master-port = 3306
master-user = repl_user
master-password = repl_pass
replicate-do-db = sync_db
# relay-log 相關建議顯式配置
relay-log = mysql-relay-bin
relay-log-index = mysql-relay-bin.index

# 重啟 Slave MySQL 服務
mysql -u root -p -e "START SLAVE;"
mysql -u root -p -e "SHOW SLAVE STATUS\G"
# 關鍵指標:
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
# Seconds_Behind_Master: 0

Master 建庫建表插入資料 → Slave 秒級同步可見。
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO
MASTER_HOST = 'new_master_ip',
MASTER_USER = 'repl_user',
MASTER_PASSWORD = 'repl_pass',
MASTER_LOG_FILE = 'recorded_binlog_file',
MASTER_LOG_POS = recorded_log_pos;
START SLAVE;
優勢:無需重啟 Slave 服務,業務無感切換。
CHANGE MASTER TO 而非寫死 master-host 等參數(已棄用)auto_increment_increment/offset 避免主鍵衝突Seconds_Behind_Master、Slave_*_Running,設定告警