1. 程式人生 > 資料庫 >Mysql資料庫雙機熱備難點分析

Mysql資料庫雙機熱備難點分析

小編在以前給大家介紹過關於Mysql 資料庫雙機熱備的配置方法有興趣的朋友參考一下,本節我們重點對其中的重要環節和需要注意的地方做了總結和分析。

一:介紹

mysql版本:5.7.20

第一個主服伺服器ip:192.168.71.139

第二個主服伺服器ip:192.168.71.141

二:配置

第一臺主伺服器192.168.71.139

1:修改/etc/mysql/my.cnf 檔案,注意這裡的#是註釋,不要寫到配置檔案中

server-id = 141 #伺服器id,不能重複,建議用ip後三位。
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema #忽略寫入binlog日誌的庫

auto-increment-increment = 2 #欄位變化增量值
auto-increment-offset = 1 #初始欄位ID為1
slave-skip-errors = all #忽略所有複製產生的錯誤

2:登陸mysql,建立允許其它伺服器複製的賬號

GRANT REPLICATION SLAVE ON *.* to 'mysql賬號'@'%' identified by '密碼';

3:使用show master status查詢狀態

第二臺主伺服器192.168.71.139

1:修改/etc/mysql/my.cnf 檔案,此處的server-id = 139,其它不變。

使用show master status查詢狀態

此時,需要重新啟動兩臺伺服器的mysql  

在192.168.71.141執行同步語句

master_log_file 值來源於139伺服器,執行show master status後的 File欄位

master_log_file 值來源於139伺服器,執行show master status後的 Position欄位
change master to master_host='192.168.71.139',master_user='master2',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=154;

在192.168.71.139執行同步語句

master_log_file 值來源於141伺服器,執行show master status後的 File欄位

master_log_file 值來源於141伺服器,執行show master status後的 Position欄位

change master to master_host='192.168.71.141',master_user='master1',master_log_pos=154;

到此為此配置結束,重啟mysql,登陸mysql,使用show slave status\G檢查配置狀態,發現Slave_IO無法啟動,出現如下錯誤

The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

通過日誌發現,master和slave的uuids重複了,因為這兩臺伺服器是克隆的,所以需要修改/var/lib/mysql/auto.cnf

這裡修改我只修改最後一個字母,因為修改多了,我mysql都無法啟動。修改完成,重新啟動mysql,再登陸mysql並執行show slave status\G,如下圖

三:測試

在任意一臺伺服器執行如下sql

create table tab141(id int primary key);

create table tab139(id int primary key);

在139伺服器執行如下sql

insert into tab139 values(1);

在141伺服器執行如下sql

insert into tab141 values(2);

結果如下圖:

如果大家還有任何不明白的地方歡迎在下方的留言區域討論。