mysql 主从同步不一致_记录一次MySQL主从不同步解决方法

今同事反映数据有问题,查看了一下MySQL主从不同步,

bd4815e1a520c2d0cf2015f91ddabdc3.png

mysql> show slave status G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.101

Master_User: db_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 22404

Relay_Log_File: relay-bin.000111

Relay_Log_Pos: 23083

Relay_Master_Log_File: mysql-bin.000062

Slave_IO_Running: Yes

Slave_SQL_Running: No

Replicate_Do_DB:

Replicate_Ignore_DB: test

...

Last_Errno: 1594

Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.

Skip_Counter: 0

Exec_Master_Log_Pos: 230837

Relay_Log_Space: 45632

...

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 1594

Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.

...

====================

1.确认主库的bin-log没有问题,

mysqlbinlog mysql-bin.000062

查看输出的内容有没有错误信息,如果出错,说明主库的bin-log有问题,就只能重置主库,重新同步了;

我的数据库的架构是一主多从,只有这一台slave有问题,说明主库的bin-log没有问题;

2.检测从库

根据错误提示,应该是从库的Relay log出了问题,同样的方式,检测relay-bin

mysqlbinlog mysqld-relay-bin.000056

一般你能看到一些导致同步错误的描述,主库没问题,从库出错,这相对来说是个好消息,只要找到同步停止的位置,重新同步就行了。

为了解决错误,需要弃用从库已经生成的Relay log,重新设置同步点,重新生成Relay log ,那么如何找到从库同步的位置呢,查看上面的错误信息,就是如下的

两个值

Relay_Master_Log_File: mysql-bin.000062

Exec_Master_Log_Pos: 230837

3. 命令操作步骤:

# 1. stop slave

mysql> stop slave;

# 重置slave的复制位置

mysql> reset slave;

# 设置slave新的同步位置

mysql> change master to master_log_file='mysql-bin.000062', master_log_pos=230837;

# 重启同步slave

mysql> start slave;

39869f638d96f0354bf5c434598aef20.png

“reset slave” 会删除master.info, relay-log.info 和所有的relay log,这些文件存储了主从同步的位置和状态信息。

/var/lib/mysql目录的文件不需要修改,等Seconds_Behind_Master为0表示已完成同步,主从的数据一致,后面的恢复正常的同步了。

更详细的命令介绍参考MySQL官方文档

https://dev.mysql.com/doc/refman/5.7/en/reset-slave.html

为了防止从库的误操作,最好设置从库只读,超级用户也设置只读

set global read_only=1;

set global super_read_only =1;

到此问题就解决了!

问题的表面总是千变万化,仔细找出问题根源所在。经常根据网上别人的方法,并不能解决自己的问题,所以一定要详细查看自己的错误信息。

参考:https://www.redips.net/mysql/replication-slave-relay-log-corrupted/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值