记一次 mysql 主从不同步问题排查

背景:主库用的阿里云RDS mysql5.7 备库则是服务器自建的mysql

原因:因主库RDS被锁定后一段时间,连接不可用,导致主从不同步

 

解决:

登录到从库查看

 

mysql -h  host -uroot -p'password' 

use database;

show slave status\G;    -- 查看对应库的运行情况,发现Last_Error日志:

The slave I/O thread stops because a fatal error is encountered when it try to get the value of SERVER_ID variable from master. Error: The MySQL server is running with the --rds-deny-access option so it cannot execute this statement

 

查找资料,于是一顿操作猛如虎。。。。。

stop slave for channel 'trade';    --先将从库停止,因该实例还有很多其他数据库,所以for channel 指定具体某个库

CHANGE MASTER TO MASTER_HOST = 'host', MASTER_USER = 'user', MASTER_PASSWORD = 'password', MASTER_PORT = 3306, MASTER_LOG_FILE='mysql-bin.000315', MASTER_LOG_POS =505071 for channel 'talent_circle';

-- 其中MASTER_LOG_FILE 与 MASTER_LOG_POS 两个参数需要到master库通过show master status;查看binlog日志以及对应的postion 号

start slave for channel 'trade';  --启动从库

show slave status\G;    再次查看对应库的运行情况,没问题

运行了一段时间后,发现未能同步,查看状态还是有错误,原因在于间隔时间太长,同步某条数据时同步导致

 

只能通过更残暴的方法,停止从库,数据清除,重新将主库的数据导入至从库。主要步骤是,从主库导出sql文件,再解锁主库,停止从库,导入从库,启动从库。(若库或者表较大,建议先锁主库,停止主库写操作,因是凌晨三点操作,且库较小,所以就没进行锁库操作)

步骤一:

mysqldump -hhost(主库地址) -uuser -p'password' --master-data=2 --single-transaction --databases  --add-drop-database  trade  > /opt/dump/bak_190909.sql

--master-data=2   表示在dump过程中记录主库的binlog和pos点,并在dump文件中注释掉这一行;

--databases  指定某个数据库;

--add-drop-database 每个数据库创建之前添加drop数据库语句;

--single-transaction  设置事务的隔离级别为可重复读

步骤二:

stop slave for channel 'trade';    停止从库

步骤三:

mysql -h  host -uuser -p'password' trade< /opt/dump/bak_190909.sql   导入到从库

步骤四:

start slave for channel 'trade';   启动从库

show slave status\G;   查看从库状态

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值