1062 错误
错误模拟
创建测试数据库
mysql> create database test;
Query OK, 1 row affected (0.09 sec)
mysql> create table test.t1(c1 int primary key,c2 int) ;
Query OK, 0 rows affected (0.04 sec)
从库先插入数据
mysql> insert into test.t1 values (1,1) ;
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.00 sec)
主库插入同主键数据
mysql> insert into test.t1 values (1,2) ;
Query OK, 1 row affected (0.04 sec)
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 2 |
+----+------+
1 row in set (0.00 sec)
查看从库同步状态 发现1062错误
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.61
Master_User: dao
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1293
Relay_Log_File: node2-relay-bin.000009
Relay_Log_Pos: 1252
Relay_Master_Log_File: mysql-bin.000004
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: 1062
Last_Error: Could not execute Write_rows event on table test.t1; Duplicate entry '1' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000004, end_log_pos 1262
Skip_Counter: 0
Exec_Master_Log_Pos: 1039
Relay_Log_Space: 1879
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: Could not execute Write_rows event on table test.t1; Duplicate entry '1' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000004, end_log_pos 1262
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 03fde241-9a55-11e8-aa12-000c291f013a
Master_Info_File: /mysql1/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 180809 21:51:05
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
手工修复数据并跳过错误
修改数据到最新值
mysql> update test.t1 set c2=2 where c1 =1 ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
跳过此错误继续同步
mysql> stop slave ;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 ;
Query OK, 0 rows affected (0.00 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.2.61
Master_User: dao
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1293
Relay_Log_File: node2-relay-bin.000010
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
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: 1293
Relay_Log_Space: 1879
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 03fde241-9a55-11e8-aa12-000c291f013a
Master_Info_File: /mysql1/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
使用参数直接跳过错误
在从库配置文件中加入slave-skip-errors
[mysql@node2 ~]$ cat /mysql1/mysql1.cnf
[client]
port = 3306
socket = /tmp/mysql1.sock
[mysqld]
port = 3306
socket = /tmp/mysql1.sock
#skip-grant-tables
basedir = /usr/local/mysql
datadir = /mysql1/data
pid-file = /mysql1/mysql1.pid
log-error = /mysql1/mysql1.log
log-bin=/mysql1/data/mysql-bin
server_id = 2
slave-skip-errors=1062
log-slave-updates=1
从库插入数据
mysql> insert into test.t1 values (1,1) ;
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.00 sec)
主库插入同主键数据
mysql> insert into test.t1 values (1,2) ;
Query OK, 1 row affected (0.00 sec)
查看同步状态
主库
mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 2309 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从库
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.61
Master_User: dao
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 2309
Relay_Log_File: node2-relay-bin.000012
Relay_Log_Pos: 574
Relay_Master_Log_File: mysql-bin.000004
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: 2309
Relay_Log_Space: 781
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 03fde241-9a55-11e8-aa12-000c291f013a
Master_Info_File: /mysql1/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
发现日志已同步,但是没有报错,虽然没有报错,但现在主库和从库数据并不一致.
查看主库从库数据
主库
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 2 |
+----+------+
1 row in set (0.00 sec)
从库
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
+----+------+
1 row in set (0.00 sec)
使用工具修复此错误
查看错误
[mysql@node1 ~]$ pt-table-checksum --host='192.168.2.61' --user=dao --password=dao --create-replicate-table --nocheck-replication-filters --replicate=test.checksums --no-check-binlog-format --databases=test
TS ERRORS DIFFS ROWS CHUNKS SKIPPED TIME TABLE
08-09T23:33:00 0 0 1 1 0 0.279 test.t1
修复错误
[mysql@node1 ~]$ pt-table-sync --execute --sync-to-master 192.168.2.62 --user=dao --password=dao
验证
主库
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 2 |
+----+------+
1 row in set (0.00 sec)
从库
mysql> select * from test.t1 ;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 2 |
+----+------+
1 row in set (0.00 sec)