mysql主從同步出錯故障處理總結[數據庫技術]

在發生故障切換後,經常遇到的問題就是同步報錯,數據庫很小的時候,dump完再導入很簡單就處理好了,但線上的數據庫都150G-200G,
如果用單純的這種方法,成本太高,故經過一段時間的摸索,總結了幾種處理方法。

生產環境架構圖
目前現網的架構,保存着兩份數據,通過異步複製做的高可用集羣,兩臺機器提供對外服務。在發生故障時,切換到slave上,並將其變成
master,壞掉的機器反向同步新的master,在處理故障時,遇到最多的就是主從報錯。下面是我收錄下來的報錯信息。

常見錯誤
最常見的3種情況
這3種情況是在HA切換時,由於是異步複製,且sync_binlog=0,會造成一小部分binlog沒接收完導致同步報錯。

第一種:在master上刪除一條記錄,而slave上找不到。
Last_SQL_Error: Could not execute Delete_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254

第二種:主鍵重複。在slave已經有該記錄,又在master上插入了同一條記錄。
Last_SQL_Error: Could not execute Write_rows event on table hcy.t1;
Duplicate entry '2' for key 'PRIMARY',
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924

第三種:在master上更新一條記錄,而slave上找不到,丟失了數據。
Last_SQL_Error: Could not execute Update_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000010, end_log_pos 263
異步半同步區別
異步複製
簡單的說就是master把binlog發送過去,不管slave是否接收完,也不管是否執行完,這一動作就結束了.

半同步複製
簡單的說就是master把binlog發送過去,slave確認接收完,但不管它是否執行完,給master一個信號我這邊收
到了,這一動作就結束了。(谷歌寫的代碼,5.5上正式應用。)

異步的劣勢
當master上寫操作繁忙時,當前POS點例如是10,而slave上IO_THREAD線程接收過來的是3,此時master宕機
,會造成相差7個點未傳送到slave上而數據丟失。

特殊的情況
slave的中繼日誌relay-bin損壞。
Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number;
It's not a binary log file that can be used by this version of MySQL
這種情況SLAVE在宕機,或者非法關機,例如電源故障、主板燒了等,造成中繼日誌損壞,同步停掉。

人爲失誤需謹慎:多臺slave存在重複server-id
這種情況同步會一直延時,永遠也同步不完,error錯誤日誌裏一直出現上面兩行信息。解決方法就是把server-id改成不一致即可。

Slave: received end packet from server, apparent master shutdown:
Slave I/O thread: Failed reading log event, reconnecting to retry, log 'mysql-bin.000012' at postion 106
問題處理
刪除失敗
在master上刪除一條記錄,而slave上找不到。

Last_SQL_Error: Could not execute Delete_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000006, end_log_pos 254
解決方法:

由於master要刪除一條記錄,而slave上找不到故報錯,這種情況主上都將其刪除了,那麼從機可以直接跳過。可用命令:

stop slave;
set global sql_slave_skip_counter=1;
start slave;
如果這種情況很多,可用我寫的一個腳本skip_error_replcation.sh,默認跳過10個錯誤(只針對這種情況才跳,其他情況輸出錯誤結果,等待處理),
這個腳本是參考maakit工具包的mk-slave-restart原理用shell寫的,功能上定義了一些自己的東西,不是無論什麼錯誤都一律跳過。)

主鍵重複
在slave已經有該記錄,又在master上插入了同一條記錄。

Last_SQL_Error: Could not execute Write_rows event on table hcy.t1;
Duplicate entry '2' for key 'PRIMARY',
Error_code: 1062;
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000006, end_log_pos 924
解決方法:

在slave上用desc hcy.t1; 先看下錶結構:

mysql> desc hcy.t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | 0       |       |
| name  | char(4) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
刪除重複的主鍵

mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
mysql> select * from t1 where id=2;
在master上和slave上再分別確認一下。

更新丟失
在master上更新一條記錄,而slave上找不到,丟失了數據。

Last_SQL_Error: Could not execute Update_rows event on table hcy.t1;
Can't find record in 't1',
Error_code: 1032;
handler error HA_ERR_KEY_NOT_FOUND;
the event's master log mysql-bin.000010, end_log_pos 794
解決方法:

在master上,用mysqlbinlog 分析下出錯的binlog日誌在幹什麼。

/usr/local/mysql/bin/mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000010 | grep -A '10' 794

#120302 12:08:36 server id 22  end_log_pos 794  Update_rows: table id 33 flags: STMT_END_F
### UPDATE hcy.t1
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='bbc' /* STRING(4) meta=65028 nullable=1 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='BTV' /* STRING(4) meta=65028 nullable=1 is_null=0 */
# at 794
#120302 12:08:36 server id 22  end_log_pos 821  Xid = 60
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
在slave上,查找下更新後的那條記錄,應該是不存在的。

mysql> select * from t1 where id=2;
Empty set (0.00 sec)
然後再到master查看

mysql> select * from t1 where id=2;
+----+------+
| id | name |
+----+------+
|  2 | BTV  |
+----+------+
1 row in set (0.00 sec)
把丟失的數據在slave上填補,然後跳過報錯即可。

mysql> insert into t1 values (2,'BTV');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1 where id=2;   
+----+------+
| id | name |
+----+------+
|  2 | BTV  |
+----+------+
1 row in set (0.00 sec)

mysql> stop slave ;set global sql_slave_skip_counter=1;start slave;
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
……
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
中繼日誌損壞
slave的中繼日誌relay-bin損壞。

Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log
Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number; 
It's not a binary log file that can be used by  this version of MySQL

手工修復
解決方法:找到同步的binlog和POS點,然後重新做同步,這樣就可以有新的中繼日值了。

例子:

mysql> show slave status\G;
*************************** 1. row ***************************
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 1191
               Relay_Log_File: vm02-relay-bin.000005
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000010
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1593
                   Last_Error: Error initializing relay log position: I/O error reading the header from the binary log
                 Skip_Counter: 1
          Exec_Master_Log_Pos: 821

Slave_IO_Running :接收master的binlog信息
                   Master_Log_File
                   Read_Master_Log_Pos

Slave_SQL_Running:執行寫操作
                   Relay_Master_Log_File
                   Exec_Master_Log_Pos

以執行寫的binlog和POS點爲準。

Relay_Master_Log_File: mysql-bin.000010
Exec_Master_Log_Pos: 821
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=821;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.8.22
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000010
          Read_Master_Log_Pos: 1191
               Relay_Log_File: vm02-relay-bin.000002
                Relay_Log_Pos: 623
        Relay_Master_Log_File: mysql-bin.000010
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1191
              Relay_Log_Space: 778
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:

轉自:http://hcymysql.blog.51cto.com/5223301/888007
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章