【Shell】fix 1032报错信息的脚本

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
 生产环境总会遇到由于各种原因导致的主从复制不一致的情况,导致slave出现 1032报错。为了使主从关系能够稳定的运行,大多时候可以选择修复1032 报错 ,先跳过去 ,然后使用  percona  的工具  pt-table-checksum 和 pt-table-sync 进行校验和修复 。 

修复1032 error的脚本如下:


#!/bin/sh

# fetch port 1032 error recored to /tmp/record.bashc.1032.$port

# parament port

if [ -z "$1" ] ; then

    PORT=3001

else

    PORT=$1

fi

tmpfile="/tmp/record.bashc.1032.$PORT"

touch $tmpfile

if [ -f $tmpfile ] ; then

    rm -f $tmpfile

fi

while true ; do

   mysql -uroot -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep -i Slave_SQL_Running | grep -i no > /dev/null

   if [ $? -eq 0 ] ; then

    # whether 1032 ?

    mysql -uroot -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep -i Last_SQL_Errno | grep -i 1032 > /dev/null

    if [ $? -eq 0 ] ; then

     table=$(mysql -uroot -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep Last_SQL_Error | awk -F 'on table ' '{print $2}' | awk -F ';' '{print $1}')

     grep "$table" $tmpfile > /dev/null

     if [ $? -eq 0 ] ; then

        echo "Error $table is already exists , can't record $tmpfile , Errorno 1032"

        mysql -h127.0.0.1 -P$PORT -Ae 'STOP SLAVE ; SET GLOBAL sql_slave_skip_counter = 1 ; SELECT SLEEP(0.1) ; START SLAVE'

     else

     echo "Error $table is not exists record it to $tmpfile Errorno 1032"

        echo $table >> $tmpfile

        mysql -uroot -h127.0.0.1 -P$PORT -Ae 'STOP SLAVE ; SET GLOBAL sql_slave_skip_counter = 1 ; SELECT SLEEP(0.1) ; START SLAVE'

          fi

        else

     echo "Error , is not 1032 error , please maunal fix , infor $(mysql -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep Last_SQL_Error)"

    fi

   else

      echo "IS OK , behind master : $(mysql -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep Seconds_Behind_Master | awk '{print $2}')"

   fi

   sleep 0.2

done 

使用该脚本需要注意的是  sql_slave_skip_counter = 1 
该参数是跳过一个事务 ,是跳过一个事务,是 跳过一个事务,重要的事情说三遍,如果你的一个事务里面包含了多个dml操作 比如4个update,第二个update出现问题,使用该参数跳过的结果是 整个事务都会被跳过去,会导致数据不一致。
binlog为row模式:


master> select * from t;
 
+----+-----+

| id | pid |

+----+-----+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

+----+-----+

3 rows in set (0.00 sec)

slave> select * from t;

+----+-----+

| id | pid |

+----+-----+

| 1 | 1 |

| 3 | 3 |

+----+-----+

2 rows in set (0.00 sec)

master> BEGIN;

Query OK, 0 rows affected (0.00 sec)

master> DELETE FROM t WHERE id = 1;

Query OK, 1 row affected (0.00 sec)

master> DELETE FROM t WHERE id = 2;

Query OK, 1 row affected (0.00 sec)

master> DELETE FROM t WHERE id = 3;

Query OK, 1 row affected (0.00 sec) 

master> COMMIT;

Query OK, 0 rows affected (0.01 sec)





slave> show slave status G

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

...

Last_SQL_Errno: 1032

Last_SQL_Error: Could not execute Delete_rows event on table test.t; Can't find record in 't', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000002, end_log_pos 333

...

1 row in set (0.00 sec) 

binlog为 statement格式


master> select * from t;
 
+----+-----+

| id | pid |

+----+-----+

| 4 | 1 |

| 6 | 3 |

+----+-----+

2 rows in set (0.00 sec)

slave> select * from t;

+----+-----+

| id | pid |

+----+-----+

| 4 | 1 |

| 5 | 2 |

| 6 | 3 |

+----+-----+

3 rows in set (0.00 sec)

master> BEGIN;

Query OK, 0 rows affected (0.00 sec)

master> delete from t where id = 4;

Query OK, 1 row affected (0.00 sec)

master> insert into t values (5,2);

Query OK, 1 row affected (0.00 sec)

master> delete from t where id = 6;

Query OK, 1 row affected (0.00 sec)

master> COMMIT;

Query OK, 0 rows affected (0.15 sec)

slave> show slave status G

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

...

               Last_SQL_Errno: 1062

               Last_SQL_Error: Error 'Duplicate entry '5' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'insert into t values (5,2)'

...

1 row in set (0.00 sec)

slave> stop slave; set global sql_slave_skip_counter = 1; start slave;

Query OK, 0 rows affected (0.05 sec)

slave> select * from t;

+----+-----+

| id | pid |

+----+-----+

| 4 | 1 |

| 5 | 2 |

| 6 | 3 |

+----+-----+

3 rows in set (0.00 sec) 

所以修复1032之后务必使用上面提供的工具(当然如果你们的工作环境有更好的工具也可以) 修复数据不一致。
推荐一下关于 sql_slave_skip_counter 的参考资料
[1] MySQL小误区:关于set global sql_slave_skip_counter=N 命令的一些点
[2] Another reason why SQL_SLAVE_SKIP_COUNTER is bad in MySQL

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
18天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
102 1
|
21天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
16 0
|
28天前
|
存储 监控 Linux
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐⭐Linux 查看当前正在运行的进程信息 ps命令 使用指南
42 0
|
28天前
|
存储 Unix Shell
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
【Shell 命令集合 系统管理 】⭐⭐Linux 显示系统的基本信息 uname命令 使用指南
34 1
|
28天前
|
存储 Linux Shell
【Shell 命令集合 系统设置 】Linux 获取指定模块的元信息 minfo命令 使用指南
【Shell 命令集合 系统设置 】Linux 获取指定模块的元信息 minfo命令 使用指南
27 0
|
1天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
8 0
|
2天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
3天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
14 1
|
7天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
137 0
|
7天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
124 0