MySQL 双向备份也被称为 主主备份 ,即两个 MySQL 服务都是 Master,其中任意一个服务又是另一个服务的 Slave。
准备
服务器
MySQL服务器
版本
IP地址
masterA
5.6.41
192.168.1.201
masterB
5.6.41
192.168.1.202
注:
备份的 MySQL 服务器版本尽量保持一致,不同的版本可能二进制日志格式不兼容。
具体操作
注意
操作过程中注意两边数据的一致!!!
masterA 配置
my.cnf
[mysqld]
# 服务器唯一标识
server-id=1
# 二进制日志文件名
log-bin=mysql-bin
# 需要备份的数据库,多个数据库用 , 分隔
binlog-do-db=piumnl
# 需要复制的数据库,多个数据库用 , 分隔
replicate-do-db=piumnl
# 中继日志文件名
relay_log=mysqld-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
# 可不加
# 禁用 dns 解析,会使授权时使用的域名无效
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
创建备份用户
masterA & masterB 都要创建备份用户:
create user 'rep'@'%' identified by 'rep'; # 创建一个账户
grant replication slave on *.* to 'rep'@'%'; # 授予该账户对任意数据库任意表的主从备份权限
备注:
Linux 下 MySQL 对 root@% 用户关闭了 grant_priv 权限,所以如果是远程登录会出现授权失败的情况