小編在以前給大家介紹過關(guān)于Mysql 數(shù)據(jù)庫雙機熱備的配置方法有興趣的朋友參考一下,本節(jié)我們重點對其中的重要環(huán)節(jié)和需要注意的地方做了總結(jié)和分析。
一:介紹
mysql版本:5.7.20
第一個主服服務(wù)器ip:192.168.71.139
第二個主服服務(wù)器ip:192.168.71.141
二:配置
第一臺主服務(wù)器192.168.71.139
1:修改/etc/mysql/my.cnf 文件,注意這里的#是注釋,不要寫到配置文件中
server-id = 141 #服務(wù)器id,不能重復(fù),建議用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 #忽略所有復(fù)制產(chǎn)生的錯誤
2:登陸mysql,創(chuàng)建允許其它服務(wù)器復(fù)制的賬號
GRANT REPLICATION SLAVE ON *.* to 'mysql賬號'@'%' identified by '密碼';
3:使用show master status查詢狀態(tài)
![](/d/20211018/4bfabab338f3fbc6b5c0606d0c48c27d.gif)
第二臺主服務(wù)器192.168.71.139
1:修改/etc/mysql/my.cnf 文件,此處的server-id = 139,其它不變。
使用show master status查詢狀態(tài)
![](/d/20211018/1eebf46f41df1d22018fb85bbf02611b.gif)
此時,需要重新啟動兩臺服務(wù)器的mysql
在192.168.71.141執(zhí)行同步語句
master_log_file 值來源于139服務(wù)器,執(zhí)行show master status后的 File字段
master_log_file 值來源于139服務(wù)器,執(zhí)行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執(zhí)行同步語句
master_log_file 值來源于141服務(wù)器,執(zhí)行show master status后的 File字段
master_log_file 值來源于141服務(wù)器,執(zhí)行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;
到此為此配置結(jié)束,重啟mysql,登陸mysql,使用show slave status\G檢查配置狀態(tài),發(fā)現(xiàn)Slave_IO無法啟動,出現(xiàn)如下錯誤
The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
通過日志發(fā)現(xiàn),master和slave的uuids重復(fù)了,因為這兩臺服務(wù)器是克隆的,所以需要修改/var/lib/mysql/auto.cnf
![](/d/20211018/3550bf962befc59e3e6d1f161aa0fd64.gif)
這里修改我只修改最后一個字母,因為修改多了,我mysql都無法啟動。修改完成,重新啟動mysql,再登陸mysql并執(zhí)行show slave status\G,如下圖
![](/d/20211018/e68cc063236becdf272c31ef0dd9a45b.gif)
三:測試
在任意一臺服務(wù)器執(zhí)行如下sql
create table tab141(id int primary key);
create table tab139(id int primary key);
在139服務(wù)器執(zhí)行如下sql
insert into tab139 values(1);
在141服務(wù)器執(zhí)行如下sql
insert into tab141 values(2);
結(jié)果如下圖:
![](/d/20211018/20a4095dab362fa33ecae8428a4a7c27.gif)
如果大家還有任何不明白的地方歡迎在下方的留言區(qū)域討論。
您可能感興趣的文章:- MySQL備份與恢復(fù)之熱備(3)
- linux系統(tǒng)下實現(xiàn)mysql熱備份詳細(xì)步驟(mysql主從復(fù)制)
- Mysql 數(shù)據(jù)庫雙機熱備的配置方法
- mysql雙機熱備份的實現(xiàn)步驟