【Shell】fix 1032報錯資訊的指令碼

楊奇龍發表於2015-09-05
 生產環境總會遇到由於各種原因導致的主從複製不一致的情況,導致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


如果您覺得從這篇文章受益,可以贊助 北在南方 一瓶飲料 ^_^

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-1791474/,如需轉載,請註明出處,否則將追究法律責任。

相關文章