MySQL雙向同步

來源:互聯網
上載者:User

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查看是否同步成功

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.