主從複製1062錯誤解決方法

當複製中斷的時候,咱們經常使用的方法是跳過錯誤,好比SET GLOBAL SQL_SLAVE_SKIP_COUNTER =1或者直接slave-skip-errors=1062這樣確實解決了問題,恢復了複製。可是長此以往主從數據相差就很大了。對於複製正常之後,咱們還須要使用pt-table-checksum數據效驗,以及pt-table-sync同步數據,今天線上一個從庫中斷,查看了錯誤,發現是主鍵衝突,至於爲什麼會發現這些狀況,請閱讀MySQL Replication須要注意的問題html

Last_Error: Error 'Duplicate entry '192442' for key 'PRIMARY'' on query. Default database: 'xxxxxxxxxxx'. Query: 'INSERT INTO xxxxxxxxxxxxx(playerId, `type`, `count`)
  VALUES( NAME_CONST('pPlayerId',629014986),  NAME_CONST('pType',8), 0)'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1499475
              Relay_Log_Space: 4829077512
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1062
               Last_SQL_Error: Error 'Duplicate entry '192442' for key 'PRIMARY'' on query. Default database: 'xxxxxxxxxxxxxx'. Query: 'INSERT INTO xxxxxxxxxxx(playerId, `type`, `count`)
  VALUES( NAME_CONST('pPlayerId',629014986),  NAME_CONST('pType',8), 0)'
1 row in set (0.00 sec)

因而根據提示,以及查看錶結構,發現果真是主鍵衝突了,可是表的記錄卻不一致,因此每每咱們跳過錯誤就會致使主從數據不一致的問題。mysql

主庫上的記錄:sql

mysql> select * from xxxx.xxxx where id=192442;
+--------+-----------+------+-------+-------+---------------+
| id     | playerId  | type | count | total | lastResetTime |
+--------+-----------+------+-------+-------+---------------+
| 192442 | 629014986 |    8 |     0 |     0 |             0 |
+--------+-----------+------+-------+-------+---------------+
1 row in set (0.00 sec)

mysql>

從庫上的記錄:bash

mysql> select * from xxxx.xxxx where id=192442;
+--------+-----------+------+-------+-------+---------------+
| id     | playerId  | type | count | total | lastResetTime |
+--------+-----------+------+-------+-------+---------------+
| 192442 | 629015414 |    8 |     0 |     0 |             0 |
+--------+-----------+------+-------+-------+---------------+
1 row in set (0.00 sec)

mysql>

有時候我會手動刪除從庫上提示的相應記錄,可是每每是比較麻煩的。由於可能不止一條記錄重複,可能N條,因此我就簡單的寫了一個腳本,刪除主鍵衝突的記錄,這個腳本只適合主鍵衝突的狀況。spa

#!/bin/bash
#Delete duplicate records primary key conflict
#Write by yayun 2014-05-17

mysql=/usr/local/mysql-5.1.66/bin/mysql
sock=/data/mysql-slave-3311/mysql.sock
passwd=123456

while true
do
    SQL_THREAD=`$mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | egrep 'Slave_SQL_Running' | awk '{print $2}'`
    LAST_ERROR=`$mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | egrep Last_Errno | awk '{print $2}'`
    duplicate=`$mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | grep Last_Error | awk '/Duplicate entry/{print $5}' | awk -F "'" '{print $2}'`
    DATABASE=`$mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | grep Last_Error | awk '{print $13}' | awk -F "'" '{print $2}'`
    TABLE=`$mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | grep Last_Error | awk -F ":" '{print $4}' | awk -F "(" '{print $1}' | awk '{print $NF}'`

    $mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | grep HA_ERR_FOUND_DUPP_KEY
    if [ $? -eq 1 ]
    then
        if [ "$SQL_THREAD" == No ] && [ "$LAST_ERROR" == 1062 ]
        then
            FILED=`$mysql -uroot -p$passwd -S $sock -Nse "desc $DATABASE.$TABLE" | grep PRI | awk '{print $1}'`
            $mysql -uroot -p$passwd -S $sock -e "delete from $DATABASE.$TABLE where $FILED=$duplicate"
            $mysql -uroot -p$passwd -S $sock -e "start slave sql_thread"
        else
            echo "====================== ok ========================"
            $mysql -uroot -p$passwd -S $sock -e 'show slave status\G' | egrep 'Slave_.*_Running'
            echo "====================== ok ========================"
            break
        fi
    fi
done

腳本隨便寫的。你們能夠再本身相應的擴展一下。^_^code

相關文章
相關標籤/搜索