環境信息
- OS:CentOS 6.5
- MySQL:5.6(RPM 安裝)
- Master:192.168.17.140
- Slave:192.168.17.141
Master 配置步驟
1. 修改 /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1
binlog_format=mixed
重啟:
service mysqld restart
2. 創建復制賬號並查看 Binlog 位置
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'root'@'192.168.17.141' IDENTIFIED BY 'happy123';
SHOW MASTER STATUS\G;
-- 記錄:File: mysqlmaster-bin.000004, Position: 327
Slave 配置步驟
1. 修改 /etc/my.cnf
[mysqld]
server-id=2
log-bin=mysqlslave-bin.log
sync_binlog=1
binlog_format=mixed
重啟:
service mysqld restart
2. 配置主庫連接信息
CHANGE MASTER TO
MASTER_HOST='192.168.17.140',
MASTER_USER='root',
MASTER_PASSWORD='happy123',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysqlmaster-bin.000004',
MASTER_LOG_POS=327;
3. 啟動復制並處理 UUID 衝突
START SLAVE;
SHOW SLAVE STATUS\G;
-- 若報錯:Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs
-- 原因:克隆機器 auto.cnf UUID 相同
-- 解決:
STOP SLAVE;
# 修改從庫 data 目錄下 auto.cnf 的 server-uuid 為新值
# 或刪除 auto.cnf 重啟 MySQL 自動生成
START SLAVE;
SHOW SLAVE STATUS\G;
-- 確認:Slave_IO_Running: Yes, Slave_SQL_Running: Yes
驗證測試
-- Master 建表插入
CREATE TABLE test.t1 (id INT PRIMARY KEY);
INSERT INTO test.t1 VALUES (1);
-- Slave 查詢
SELECT * FROM test.t1; -- 應返回 1 行
關鍵避坑總結
- server-id 必須唯一(IP 最後段 + 端口組合佳)
- Binlog 位置必須精確(File + Position)
- 克隆機器必改 auto.cnf UUID
- 防火牆/SELinux 必放行
- 日志是最好的老師:報錯先看
.err 日志,90% 問題日志里寫著呢