上一篇文章已經介紹過 主從複製, 本文對主從複製只是簡單描述,如對主從複製不清楚的,可以先看上一篇文章 主從複製 一:介紹 mysql版本:5.7.20 第一個主服伺服器ip:192.168.71.139 第二個主服伺服器ip:192.168.71.141 二:配置 第一臺主伺服器192.168.7 ...
上一篇文章已經介紹過 主從複製, 本文對主從複製只是簡單描述,如對主從複製不清楚的,可以先看上一篇文章 主從複製
一:介紹
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_password=’123456’,master_log_file=’mysql-bin.000002’, master_log_pos=154;
到此為此配置結束,重啟mysql,登陸mysql,使用show slave status\G檢查配置狀態,發現Slave_IO無法啟動,Salve_IO_Running值為no,出現如下提示
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);
結果如下圖:
如果本文對你有幫助,你可以點一下推薦,謝謝!