1.伺服器狀態
伺服器A:192.168.1.1
伺服器B:192.168.1.2
2.建立同步處理的使用者
主機域A:192.168.1.2 使用者名稱A:sync_a 密碼A:aaa
主機域B:192.168.1.1 使用者名稱B:sync_b 密碼B:bbb
至少分配以下許可權grant replication slave
3.執行flush privileges
4.停止MySQL
5.配置my.cnf(my.ini)
| 伺服器A |
伺服器B |
user = mysql log-bin = mysql-bin server-id = 1 binlog-do-db = test binlog-ignore-db = mysql replicate-do-db = test replicate-ignore-db = mysql log-slave-updates slave-skip-errors = all sync_binlog = 1 |
user = mysql log-bin = mysql-bin server-id = 2 binlog-do-db = test binlog-ignore-db = mysql replicate-do-db = test replicate-ignore-db = mysql log-slave-updates slave-skip-errors = all sync_binlog = 1 |
server-id必須唯一
binlog-do-db和replicate-do-db表示需要同步的資料庫
binlog-ignore-db和replicate-ignore-db表示不需要同步的資料庫
請不要加入以下命令,該命令並不會解決uid跳號的問題,恰恰相反以下兩行命令才是導致uid跳號的罪魁禍首
auto_increment_increment = 2
auto_increment_offset = 1
6.重新啟動MySQL
7.進入MySQL控制台
伺服器A:
show master status \G
flush tables with read lock;
伺服器B:
show master status \G
flush tables with read lock;
同時記錄下兩台伺服器的File和Position,此處假設:
A: File: mysql-bin.000001
Position: 001
B: File: mysql-bin.000002
Position: 002
伺服器A:
change master to
-> master_host='192.168.1.2',
-> master_user='sync_b',
-> master_password='bbb',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=002;
伺服器B:
change master to
-> master_host='192.168.1.1',
-> master_user='sync_a',
-> master_password='aaa',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=001;
此處填寫的資料完全相反
8.執行show processlist \G查看是否同步成功