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

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

  1. #!/bin/sh
  2. # fetch port 1032 error recored to /tmp/record.bashc.1032.$port
  3. # parament port
  4. if [ -"$1" ] ; then
  5.     PORT=3001
  6. else
  7.     PORT=$1
  8. fi
  9. tmpfile="/tmp/record.bashc.1032.$PORT"
  10. touch $tmpfile
  11. if [ -f $tmpfile ] ; then
  12.     rm -f $tmpfile
  13. fi
  14. while true ; do
  15.    mysql -uroot -h127.0.0.-P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep -i Slave_SQL_Running | grep -i no > /dev/null
  16.    if [ $? -eq 0 ] ; then
  17.     # whether 1032 ?
  18.     mysql -uroot -h127.0.0.-P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep -i Last_SQL_Errno | grep -i 1032 > /dev/null
  19.     if [ $? -eq 0 ] ; then
  20.      table=$(mysql -uroot -h127.0.0.-P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep Last_SQL_Error | awk -'on table ' '{print $2}' | awk -';' '{print $1}')
  21.      grep "$table" $tmpfile > /dev/null
  22.      if [ $? -eq 0 ] ; then
  23.         echo "Error $table is already exists , can't record $tmpfile , Errorno 1032"
  24.         mysql -h127.0.0.-P$PORT -Ae 'STOP SLAVE ; SET GLOBAL sql_slave_skip_counter = 1 ; SELECT SLEEP(0.1) ; START SLAVE'
  25.      else
  26.      echo "Error $table is not exists record it to $tmpfile Errorno 1032"
  27.         echo $table >> $tmpfile
  28.         mysql -uroot -h127.0.0.-P$PORT -Ae 'STOP SLAVE ; SET GLOBAL sql_slave_skip_counter = 1 ; SELECT SLEEP(0.1) ; START SLAVE'
  29.           fi
  30.         else
  31.      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)"
  32.     fi
  33.    else
  34.       echo "IS OK , behind master : $(mysql -h127.0.0.1 -P$PORT -Ae 'SHOW SLAVE STATUS\G' | grep Seconds_Behind_Master | awk '{print $2}')"
  35.    fi
  36.    sleep 0.2
  37. done
使用该脚本需要注意的是  sql_slave_skip_counter = 1 
该参数是跳过一个事务 ,是跳过一个事务,是 跳过一个事务,重要的事情说三遍,如果你的一个事务里面包含了多个dml操作 比如4个update,第二个update出现问题,使用该参数跳过的结果是 整个事务都会被跳过去,会导致数据不一致。
binlog为row模式:
  1. master> select * from t;
  2. +----+-----+
  3. | id | pid |
  4. +----+-----+
  5. | 1 | 1 |
  6. | 2 | 2 |
  7. | 3 | 3 |
  8. +----+-----+
  9. 3 rows in set (0.00 sec)
  10. slave> select * from t;
  11. +----+-----+
  12. | id | pid |
  13. +----+-----+
  14. | 1 | 1 |
  15. | 3 | 3 |
  16. +----+-----+
  17. 2 rows in set (0.00 sec)
  18. master> BEGIN;
  19. Query OK, 0 rows affected (0.00 sec)
  20. master> DELETE FROM t WHERE id = 1;
  21. Query OK, 1 row affected (0.00 sec)
  22. master> DELETE FROM t WHERE id = 2;
  23. Query OK, 1 row affected (0.00 sec)
  24. master> DELETE FROM t WHERE id = 3;
  25. Query OK, 1 row affected (0.00 sec)
  26. master> COMMIT;
  27. Query OK, 0 rows affected (0.01 sec)


  28. slave> show slave status G
  29. *************************** 1. row ***************************
  30. ...
  31. Last_SQL_Errno: 1032
  32. 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
  33. ...
  34. 1 row in set (0.00 sec)
binlog为 statement格式

  1. master> select * from t;
  2. +----+-----+
  3. | id | pid |
  4. +----+-----+
  5. | 4 | 1 |
  6. | 6 | 3 |
  7. +----+-----+
  8. 2 rows in set (0.00 sec)
  9. slave> select * from t;
  10. +----+-----+
  11. | id | pid |
  12. +----+-----+
  13. | 4 | 1 |
  14. | 5 | 2 |
  15. | 6 | 3 |
  16. +----+-----+
  17. 3 rows in set (0.00 sec)
  18. master> BEGIN;
  19. Query OK, 0 rows affected (0.00 sec)
  20. master> delete from t where id = 4;
  21. Query OK, 1 row affected (0.00 sec)
  22. master> insert into t values (5,2);
  23. Query OK, 1 row affected (0.00 sec)
  24. master> delete from t where id = 6;
  25. Query OK, 1 row affected (0.00 sec)
  26. master> COMMIT;
  27. Query OK, 0 rows affected (0.15 sec)
  28. slave> show slave status G
  29. *************************** 1. row ***************************
  30. ...
  31.                Last_SQL_Errno: 1062
  32.                Last_SQL_Error: Error 'Duplicate entry '5' for key 'PRIMARY'' on query. Default database: 'test'. Query: 'insert into t values (5,2)'
  33. ...
  34. 1 row in set (0.00 sec)
  35. slave> stop slave; set global sql_slave_skip_counter = 1; start slave;
  36. Query OK, 0 rows affected (0.05 sec)
  37. slave> select * from t;
  38. +----+-----+
  39. | id | pid |
  40. +----+-----+
  41. | 4 | 1 |
  42. | 5 | 2 |
  43. | 6 | 3 |
  44. +----+-----+
  45. 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


如果您觉得从这篇文章受益,可以赞助 北在南方 一瓶饮料 ^_^

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
Shell Linux 开发工具
Shell脚本中read命令的使用方法(详解)
Shell脚本中read命令的使用方法(详解)
Shell脚本中read命令的使用方法(详解)
|
Ubuntu 关系型数据库 MySQL
提示-bash: command not found的解决方法集锦
提示-bash: command not found的解决方法集锦
|
Shell Linux
解决脚本文件无法执行conda命令的问题:CommandNotFoundError: Your shell has not been properly configured to use
使用Linux系统时,有时候希望利用一个脚本执行多条命令来节省时间,其中如果安装有anaconda,需要切换环境或者关闭conda环境,按道理说,在终端里可以通过命令
710 0
|
Shell 应用服务中间件 nginx
shell脚本-find
find查询服务器内文件
|
Shell Linux
在 Shell 脚本中执行语法检查调试模式
在 Shell 脚本中执行语法检查调试模式
154 0
|
Shell 开发工具 git
easyswoole 更新代码shell
easyswoole 更新代码shell
68 0
|
移动开发 Unix Shell
执行shell脚本时提示bad interpreter:No such file or directory的解决办法
执行shell脚本时提示bad interpreter:No such file or directory的解决办法
1235 0