11

解决线上主从复制1032报错-10931853

 4 years ago
source link: https://blog.51cto.com/wujianwei/2492806
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

解决线上主从复制1032报错

出错的原因是,主库服务异常重启,导致slave库复制报1032错

也有这样的情况,slave没有限制只读权限,开发或者运维人员在slave库上误删数据,导致生成主库update数据时,由于slave库上记录被提前误删,导致主从复制报错1032,主从不同步了
解决方法:

很多新手选择了my.cnf可配置slave-skip-errors=1032 从而跳过日志中1032 ERROR报错,或者set global sql_slave_skip_counter=1;stop slave; start slave;

然而上面的方式都是不可取的,因为slave库上没这条,master库再更新时,slave还会报错的。所以这个1032的报错必须解决。如何解决呢,咱们下面来细说下

show master status \G;
复制报错如下:

Could not execute Update_rows event on table appdb.hlz_ad_voucher; 
 Can't find record in 'hlz_ad_voucher', Error_code: 1032;
 handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000427, end_log_pos 9279278

解决办法:根据报错结束的binglog位置点,通过解析主库的binlog日志文件来提取出当前正在update更新主库上的记录时的这条SQL记录。

下面在master上是获取到update更新记录的SQL方法:

 [root@localhost ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v -v /data/mysql/binlog/mysql-bin.000427|grep -A 15 9279278|sed -n '/### UPDATE `appdb`.`hlz_ad_voucher`/,/COMMIT/p'|grep -B 100 '# at 9279278'
### UPDATE `appdb`.`hlz_ad_voucher`
### WHERE
###   @1=9400072 /* INT meta=0 nullable=0 is_null=0 */
###   @2=2994652 /* INT meta=0 nullable=0 is_null=0 */
###   @3=0.50 /* DECIMAL(5,2) meta=1282 nullable=0 is_null=0 */
###   @4=0 /* INT meta=0 nullable=0 is_null=0 */
###   @5=0 /* INT meta=0 nullable=0 is_null=0 */
###   @6=1 /* INT meta=0 nullable=0 is_null=0 */
###   @7=0 /* INT meta=0 nullable=0 is_null=0 */
###   @8='2020:05:05' /* DATE meta=0 nullable=0 is_null=0 */
###   @9='新人赠送' /* VARSTRING(1020) meta=1020 nullable=0 is_null=0 */
###   @10='2020-05-05 21:14:20.745' /* DATETIME(3) meta=3 nullable=0 is_null=0 */
###   @11='2020-05-05 21:14:20.745' /* DATETIME(3) meta=3 nullable=0 is_null=0 */
###   @12=0 /* INT meta=0 nullable=0 is_null=0 */
###   @13=0 /* INT meta=0 nullable=0 is_null=0 */
--
#at 9279278

把获取到update更新记录的SQL转换成insert into SQL语句,然后在slave库对应的表插入,最后stop slave;start slave;show slave status\G


[root@localhost ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v -v /data/mysql/binlog/mysql-bin.000427|grep -A 15 9279278|sed -n '/### UPDATE `appdb`.`hlz_ad_voucher`/,/COMMIT/p'|grep -B 100 '# at 9279278'|sed 's/### //g;s/\/\*.*/,/g;s/UPDATE/INSERT INTO/g;s/WHERE/SELECT/g;' | sed -r 's/(@13.*),/\1;/g' | sed 's/@\([0-9]\+\)=//g;' 
INSERT INTO `appdb`.`hlz_ad_voucher`
SELECT
  9400072 ,
  2994652 ,
  0.50 ,
  0 ,
  0 ,
  1 ,
  0 ,
  '2020:05:05' ,
  '新人赠送' ,
  '2020-05-05 21:14:20.745' ,
  '2020-05-05 21:14:20.745' ,
  0 ,
  0 ;
--
#at 9279278

一直重复上面的方法在master的binlog文件中提取到SQL,然后转化成insert into SQL,然后在插入slave库对应的表,stop slave;start slave; 直到不再报错为止


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK