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

 生产环境总会遇到由于各种原因导致的主从复制不一致的情况,导致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


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

请使用浏览器的分享功能分享到微信等