MySQL 双向备份

MySQL 双向备份

也被称为 主主备份 ,即两个 MySQL 服务都是 Master,其中任意一个服务又是另外一个服务的 Slave。

准备

服务器html

MySQL服务器 版本 IP地址
masterA 5.6.41 192.168.1.201
masterB 5.6.41 192.168.1.202

注:mysql

  1. 备份的 MySQL 服务器版本尽可能保持一致,不一样的版本可能二进制日志格式不兼容。

具体操做

注意

操做过程当中注意两边数据的一致!!!sql

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'@'%';  # 授予该帐户对任意数据库任意表的主从备份权限

备注:app

  1. Linux 下 MySQL 对 root@% 用户关闭了 grant_priv 权限,因此若是是远程登陆会出现受权失败的状况
  2. 此处备份用户账号和密码可不一致,此处为了简化操做使用同样的账号和密码

重启服务器

重启服务器ide

开启备份

masterA测试

  1. 查看 masterB 状态this

    show master status\G;
    # 此处须要关注 File 和 Position 值
  2. 开启备份

    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;
  3. 查看结果

    show slave status\G;
    # 查看最重要的两项,两个都必须为 Yes ,有一个为 No 都要去查看错误日志文件,看看什么地方存在问题
    # Slave_IO_Running: Yes
    # Slave_SQL_Running: Yes

masterB

  1. 反向重复 masterA 的操做

测试

分别在 masterA 和 masterB 中插入数据,并查看另外一台服务器是否及时出现预期的数据

问题

Relay Log

MySQL Slave Failed to Open the Relay Log

这应该是中继日志出现问题,可尝试以下操做

stop slave;
flush logs;
start slave;

mysql_bin.index

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

从主库中拉取日志时,发现主库的 mysql_bin.index 文件中的第一个文件不存在。

# 进行以下操做重置
# 若是二进制日志或中继日志有其余做用,请勿进行以下操做
reset master;
reset slave;
flush logs;

<database>.<table>

使用 <database>.<table> 进行插入、更新和删除操做,将不会进行备份( 这是巨坑 )!!!


参考文献

  1. MySQL互为主从及Keepalived配置vip——主从配置篇
  2. MySQL 双机备份
  3. 【MySQL】Got fatal error 1236缘由和解决方法
  4. RESET MASTER和RESET SLAVE使用场景和说明,以及清除主从同步关系
  5. MySql 5.6关于replicate secure增强
  6. MySQL 错误 “could not be resolved: ..” 和引起的思考

若有错误或问题,欢迎提出来一块儿交流讨论

相关文章
相关标签/搜索