mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 107 | | |
+------------------+----------+--------------+------------------+
若無輸出,需修改 /etc/my.cnf 添加並重啟:
log-bin = mysql-bin
relay-log = relay-bin
binlog_format = MIXED
binlog_cache_size = 1M
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
mysqldump -uroot -p123456 --single-transaction --master-data=2 --all-databases \
--flush-logs > db.sql
mysql -uroot -p123456 < db.sql
從 db.sql 開頭獲取 Binlog 位置:
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000007', MASTER_LOG_POS=107
在 Slave 執行:
CHANGE MASTER TO
MASTER_HOST='10.254.141.23',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=107;
START SLAVE;
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
Seconds_Behind_Master: 0 表示無延遲,主從同步成功。
mysql> SET SQL_LOG_BIN=0;