MySQL 双向备份也被称为 主主备份 ,即两个 MySQL 服务都是 Master,其中任意一个服务又是另一个服务的 Slave。准备服务器MySQL服务器IP地址masterA192.168.1.201masterB192.168.1.202具体操作注意操作过程中注意两边数据的一致!!!masterA 配置my.cnf[mysqld]# 服务器唯一标识server-id=1# 二进制日志文件名log-bin=mysql-bin# 需要备份的数据库,多个数据库用 , 分隔binlog-do-db=piumnl# 需要复制的数据库,多个数据库用 , 分隔replicate-do-db=piumnl# 中继日志文件名relay_log=mysqld-relay-bin# 互为主从需要加入这一行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: 0master-info-repository=tablerelay-log-info-repository=tablerelay-log-recovery=1# 可不加# 禁用 dns 解析,会使授权时使用的域名无效skip-host-cacheskip-name-resolvesql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESmasterB 配置my.cnf# 不再解释各个配置项[mysqld]server-id=2log-bin=mysql-binbinlog-do-db=piumnlreplicate-do-db=piumnlrelay_log=mysql-relay-binlog-slave-updates=ONsymbolic-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: 0master-info-repository=tablerelay-log-info-repository=tablerelay-log-recovery=1skip-host-cacheskip-name-resolvesql_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 权限此处备份用户帐号和密码可不一致,此处为了简化操作使用一样的帐号和密码重启服务器重启服务器开启备份masterA查看 masterB 状态show master status\G;# 此处需要关注 File 和 Position 值开启备份stop slave;# master_log_file 就是第一步操作的 File 值# master_log_pos 就是第一步操作的 Position 值change master to master_host=<master_hostname>, master_user=<rep_username>, master_password=<rep_password>, master_log_file=‘mysql-log.000003’, master_log_pos=154;start slave;查看结果show slave status\G;# 查看最重要的两项,两个都必须为 Yes ,有一个为 No 都要去查看错误日志文件,看看什么地方存在问题# Slave_IO_Running: Yes# Slave_SQL_Running: YesmasterB反向重复 masterA 的操作测试分别在 masterA 和 masterB 中插入数据,并查看另一台服务器是否及时出现预期的数据注:使用 <database>.<table> 方式进行插入、更新和删除操作,将不会备份。如果操作弄乱了 master 与 slave 的日志,可使用如下操作进行重置。reset master; # 重置 master 的配置,包括二进制日志reset slave; # 重置 slave 的配置