溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL 雙向備份的實現(xiàn)方法

發(fā)布時間:2020-09-04 03:42:52 來源:腳本之家 閱讀:138 作者:魏晉秋 欄目:MySQL數(shù)據(jù)庫

MySQL 雙向備份也被稱為 主主備份 ,即兩個 MySQL 服務(wù)都是 Master,其中任意一個服務(wù)又是另一個服務(wù)的 Slave。

準(zhǔn)備

服務(wù)器

MySQL服務(wù)器 版本 IP地址
masterA 5.6.41 192.168.1.201
masterB 5.6.41 192.168.1.202

注:備份的 MySQL 服務(wù)器版本盡量保持一致,不同的版本可能二進(jìn)制日志格式不兼容。

具體操作

注意

操作過程中注意兩邊數(shù)據(jù)的一致?。?!

masterA 配置

my.cnf

[mysqld]
# 服務(wù)器唯一標(biāo)識
server-id=1
# 二進(jìn)制日志文件名
log-bin=mysql-bin

# 需要備份的數(shù)據(jù)庫,多個數(shù)據(jù)庫用 , 分隔
binlog-do-db=piumnl
# 需要復(fù)制的數(shù)據(jù)庫,多個數(shù)據(jù)庫用 , 分隔
replicate-do-db=piumnl
# 中繼日志文件名
relay_log=mysqld-relay-bin
# 手動啟動同步服務(wù),避免突然宕機(jī)導(dǎo)致的數(shù)據(jù)日志不同步
skip-slave-start=ON
# 互為主從需要加入這一行
log-slave-updates=ON
# 禁用符號鏈接,防止安全風(fēng)險,可不加
symbolic-links=0

# 可不加
# resolve - [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
master-info-repository=table
relay-log-info-repository=table
relay-log-recovery=1

# 可不加
# 禁用 dns 解析,會使授權(quán)時使用的域名無效
skip-host-cache
skip-name-resolve

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

masterB 配置

my.cnf

# 不再解釋各個配置項
[mysqld]
server-id=2
log-bin=mysql-bin

binlog-do-db=piumnl
replicate-do-db=piumnl
relay_log=mysql-relay-bin
skip-slave-start=ON
log-slave-updates=ON
symbolic-links=0

# resolve - [Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0
master-info-repository=table
relay-log-info-repository=table
relay-log-recovery=1

skip-host-cache
skip-name-resolve

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

創(chuàng)建備份用戶

masterA & masterB 都要創(chuàng)建備份用戶:

create user 'rep'@'%' identified by 'rep';  # 創(chuàng)建一個賬戶
grant replication slave on *.* to 'rep'@'%'; # 授予該賬戶對任意數(shù)據(jù)庫任意表的主從備份權(quán)限

備注:

  1. Linux 下 MySQL 對 root@% 用戶關(guān)閉了 grant_priv 權(quán)限,所以如果是遠(yuǎn)程登錄會出現(xiàn)授權(quán)失敗的情況
  2. 此處備份用戶帳號和密碼可不一致,此處為了簡化操作使用一樣的帳號和密碼

重啟服務(wù)器

重啟服務(wù)器

開啟備份

masterA

查看 masterB 狀態(tài)

show master status\G;
# 此處需要關(guān)注 File 和 Position 值

開啟備份

stop slave;

# master_log_file 就是第一步操作的 File 值
# master_log_pos 就是第一步操作的 Position 值
change master to master_host=<master_hostname>, master_user=<rep_username>, master_port=<master_port>, master_password=<rep_password>, master_log_file='mysql-log.000003', master_log_pos=154;
start slave;

查看結(jié)果

show slave status\G;
# 查看最重要的兩項,兩個都必須為 Yes ,有一個為 No 都要去查看錯誤日志文件,看看什么地方存在問題
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes

masterB

反向重復(fù) masterA 的操作

測試

分別在 masterA 和 masterB 中插入數(shù)據(jù),并查看另一臺服務(wù)器是否及時出現(xiàn)預(yù)期的數(shù)據(jù)

問題

MySQL Slave Failed to Open the Relay Log

這應(yīng)該是中繼日志出現(xiàn)問題,可嘗試如下操作

stop slave;
flush logs;
start slave;

Got fatal error 1236 from master when reading data from binary log

從主庫中拉取日志時,發(fā)現(xiàn)主庫的 mysql_bin.index 文件中的第一個文件不存在。

# 進(jìn)行如下操作重置
# 如果二進(jìn)制日志或中繼日志有其他作用,請勿進(jìn)行如下操作
reset master;
reset slave;
flush logs;

<database>.<table>

使用 <database>.<table> 進(jìn)行插入、更新和刪除操作,將不會進(jìn)行備份( 這是巨坑 )?。?!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI