传统复制常见错误及填坑方法

传统复制常见错误及填坑方法

环境

  • 主库:IP:192.168.1.21;版本:5.7.18
  • 备库:IP:192.168.1.128;版本:5.6.36
  • 主库测试库: sbtest
  • 备库配置:Replicate_Do_DB: sbtest

1、表不存在导致插入更新失败

1.1、模拟复制错误产生

1.1.1、生成测试表sbtest.test
  • 在指定的复制库下生成测试表test。但,不在sbtest库下操作,而选择了在mysql库下操作生成sbtest.test
 
 
  1. #主库上操作
  2. mysql> use mysql
  3. Database changed
  4. mysql> create table sbtest.test ( id int );
  5. Query OK, 0 rows affected (0.08 sec)
  6. mysql> use sbtest;
  7. Database changed
  8. mysql> show tables;
  9. +------------------+
  10. | Tables_in_sbtest |
  11. +------------------+
  12. | sanguoyanyi |
  13. | test |
  14. | xiyouji |
  15. +------------------+
  16. 3 rows in set (0.00 sec)
1.1.2、在备库上查看是否同步test表
 
 
  1. #备库上操作
  2. root@localhost : sbtest 03:33:13> use sbtest;
  3. Database changed
  4. root@localhost : sbtest 03:41:18> show tables;
  5. +------------------+
  6. | Tables_in_sbtest |
  7. +------------------+
  8. | sanguoyanyi |
  9. | xiyouji |
  10. +------------------+
  11. 2 rows in set (0.00 sec)
  12. root@localhost : sbtest 03:41:43> show slave status\G;
  13. *************************** 1. row ***************************
  14. Slave_IO_State: Waiting for master to send event
  15. Master_Host: 192.168.1.21
  16. Master_User: repl
  17. Master_Port: 3306
  18. Connect_Retry: 60
  19. Master_Log_File: mysql-bin.000026
  20. Read_Master_Log_Pos: 331
  21. Relay_Log_File: mysql-relay-bin.000004
  22. Relay_Log_Pos: 463
  23. Relay_Master_Log_File: mysql-bin.000026
  24. Slave_IO_Running: Yes
  25. Slave_SQL_Running: Yes
  26. Replicate_Do_DB: sbtest
  27. Replicate_Ignore_DB:
  28. Replicate_Do_Table:
  29. Replicate_Ignore_Table:
  30. Replicate_Wild_Do_Table:
  31. Replicate_Wild_Ignore_Table:
  32. Last_Errno: 0
  33. Last_Error:
  34. Skip_Counter: 0
  35. Exec_Master_Log_Pos: 331
  36. Relay_Log_Space: 636
  37. Until_Condition: None
  38. Until_Log_File:
  39. Until_Log_Pos: 0
  40. Master_SSL_Allowed: No
  41. Master_SSL_CA_File:
  42. Master_SSL_CA_Path:
  43. Master_SSL_Cert:
  44. Master_SSL_Cipher:
  45. Master_SSL_Key:
  46. Seconds_Behind_Master: 0
  47. Master_SSL_Verify_Server_Cert: No
  48. Last_IO_Errno: 0
  49. Last_IO_Error:
  50. Last_SQL_Errno: 0
  51. Last_SQL_Error:
  52. Replicate_Ignore_Server_Ids:
  53. Master_Server_Id: 12001
  54. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  55. Master_Info_File: mysql.slave_master_info
  56. SQL_Delay: 0
  57. SQL_Remaining_Delay: NULL
  58. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  59. Master_Retry_Count: 86400
  60. Master_Bind:
  61. Last_IO_Error_Timestamp:
  62. Last_SQL_Error_Timestamp:
  63. Master_SSL_Crl:
  64. Master_SSL_Crlpath:
  65. Retrieved_Gtid_Set:
  66. Executed_Gtid_Set:
  67. Auto_Position: 0
  68. 1 row in set (0.00 sec)
  69. #这也是为什么不推荐使用replicate-do-db, replicate-ignore-db配置参数的原因。原因是设置replicate_do_db或replicate_ignore_db后,MySQL执行sql前检查的是当前默认数据库,所以跨库更新语句在Slave上会被忽略。
1.1.3、主库上往test表中插入数据
 
 
  1. mysql> insert into test values (2);
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> select * from test;
  4. +------+
  5. | id |
  6. +------+
  7. | 2 |
  8. +------+
  9. 1 row in set (0.00 sec)
1.1.4、查看备库复制情况
 
 
  1. root@localhost : sbtest 04:05:27> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.21
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000026
  9. Read_Master_Log_Pos: 590
  10. Relay_Log_File: mysql-relay-bin.000004
  11. Relay_Log_Pos: 463
  12. Relay_Master_Log_File: mysql-bin.000026
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: No
  15. Replicate_Do_DB: sbtest
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 1146
  22. Last_Error: Error executing row event: 'Table 'sbtest.test' doesn't exist'
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 331
  25. Relay_Log_Space: 895
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 1146
  40. Last_SQL_Error: Error executing row event: 'Table 'sbtest.test' doesn't exist'
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 12001
  43. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State:
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp: 170522 04:05:17
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. 1 row in set (0.00 sec)
  58. #报错显示表sbtest.test不存在

1.2、复制错误跳过

1.2.1、主库上查看event执行信息
 
 
  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+--------------+------------------+-------------------+
  5. | mysql-bin.000026 | 590 | | | |
  6. +------------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)
  8. mysql> show binlog events in 'mysql-bin.000026';
  9. +------------------+-----+----------------+-----------+-------------+--------------------------------------------------+
  10. | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
  11. +------------------+-----+----------------+-----------+-------------+--------------------------------------------------+
  12. | mysql-bin.000026 | 4 | Format_desc | 12001 | 123 | Server ver: 5.7.18-log, Binlog ver: 4 |
  13. | mysql-bin.000026 | 123 | Previous_gtids | 12001 | 154 | |
  14. | mysql-bin.000026 | 154 | Anonymous_Gtid | 12001 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  15. | mysql-bin.000026 | 219 | Query | 12001 | 331 | use `mysql`; create table sbtest.test ( id int ) |
  16. | mysql-bin.000026 | 331 | Anonymous_Gtid | 12001 | 396 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
  17. | mysql-bin.000026 | 396 | Query | 12001 | 470 | BEGIN |
  18. | mysql-bin.000026 | 470 | Table_map | 12001 | 519 | table_id: 334 (sbtest.test) |
  19. | mysql-bin.000026 | 519 | Write_rows | 12001 | 559 | table_id: 334 flags: STMT_END_F |
  20. | mysql-bin.000026 | 559 | Xid | 12001 | 590 | COMMIT /* xid=1091 */ |
  21. +------------------+-----+----------------+-----------+-------------+--------------------------------------------------+
  22. 9 rows in set (0.00 sec)
1.2.2、结合主备信息跳过错误
 
 
  1. #show slave status\G显示的有用信息
  2. Master_Log_File: mysql-bin.000026
  3. Read_Master_Log_Pos: 590
  4. Exec_Master_Log_Pos: 331
 
 
  1. #结合主库的show binlog events in 'mysql-bin.000026'以及备库的show slave status\G可知需要跳过519(insert)以及559(commit)两个事务
  2. root@localhost : sbtest 04:29:56> stop slave;
  3. Query OK, 0 rows affected (0.01 sec)
  4. root@localhost : sbtest 04:30:41> set global sql_slave_skip_counter=2;
  5. Query OK, 0 rows affected (0.00 sec)
  6. root@localhost : sbtest 04:30:44> start slave;
  7. Query OK, 0 rows affected (0.02 sec)
  8. root@localhost : sbtest 04:30:47> show slave status\G;
  9. *************************** 1. row ***************************
  10. Slave_IO_State: Waiting for master to send event
  11. Master_Host: 192.168.1.21
  12. Master_User: repl
  13. Master_Port: 3306
  14. Connect_Retry: 60
  15. Master_Log_File: mysql-bin.000026
  16. Read_Master_Log_Pos: 590
  17. Relay_Log_File: mysql-relay-bin.000006
  18. Relay_Log_Pos: 286
  19. Relay_Master_Log_File: mysql-bin.000026
  20. Slave_IO_Running: Yes
  21. Slave_SQL_Running: Yes
  22. Replicate_Do_DB: sbtest
  23. Replicate_Ignore_DB:
  24. Replicate_Do_Table:
  25. Replicate_Ignore_Table:
  26. Replicate_Wild_Do_Table:
  27. Replicate_Wild_Ignore_Table:
  28. Last_Errno: 0
  29. Last_Error:
  30. Skip_Counter: 0
  31. Exec_Master_Log_Pos: 590
  32. Relay_Log_Space: 625
  33. Until_Condition: None
  34. Until_Log_File:
  35. Until_Log_Pos: 0
  36. Master_SSL_Allowed: No
  37. Master_SSL_CA_File:
  38. Master_SSL_CA_Path:
  39. Master_SSL_Cert:
  40. Master_SSL_Cipher:
  41. Master_SSL_Key:
  42. Seconds_Behind_Master: 0
  43. Master_SSL_Verify_Server_Cert: No
  44. Last_IO_Errno: 0
  45. Last_IO_Error:
  46. Last_SQL_Errno: 0
  47. Last_SQL_Error:
  48. Replicate_Ignore_Server_Ids:
  49. Master_Server_Id: 12001
  50. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  51. Master_Info_File: mysql.slave_master_info
  52. SQL_Delay: 0
  53. SQL_Remaining_Delay: NULL
  54. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  55. Master_Retry_Count: 86400
  56. Master_Bind:
  57. Last_IO_Error_Timestamp:
  58. Last_SQL_Error_Timestamp:
  59. Master_SSL_Crl:
  60. Master_SSL_Crlpath:
  61. Retrieved_Gtid_Set:
  62. Executed_Gtid_Set:
  63. Auto_Position: 0
  64. 1 row in set (0.00 sec)
1.2.3、手动补上数据
 
 
  1. root@localhost : sbtest 04:53:01> CREATE TABLE `test` (
  2. -> `id` int(11) DEFAULT NULL
  3. -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  4. -> ;
  5. Query OK, 0 rows affected (0.23 sec)
  6. root@localhost : sbtest 04:53:24> insert into test values (2);
  7. Query OK, 1 row affected (0.05 sec)
  8. root@localhost : sbtest 04:54:11> select * from test;
  9. +------+
  10. | id |
  11. +------+
  12. | 2 |
  13. +------+
  14. 1 row in set (0.00 sec)

2、ERROR 1062 从库插入了数据,主键重复

2.1、模拟错误产生

2.1.1、创建测试表sbtest.shuihuchuan
 
 
  1. #主库上创建测试表sbtest.shuihuchuan
  2. mysql> CREATE TABLE `shuihuchuan` (
  3. -> `id` int(11) NOT NULL AUTO_INCREMENT,
  4. -> `people` varchar(20) DEFAULT NULL,
  5. -> `nickname` varchar(30) DEFAULT NULL,
  6. -> PRIMARY KEY (`id`)
  7. -> );
  8. Query OK, 0 rows affected (0.09 sec)
  9. mysql> insert into shuihuchuan(nickname,people) values ('hubaoyi','songjiang'),('tuotatianwang','chaogai'),('yuqinlin','lujunyi'),('zhiduoxing','wuyong');
  10. Query OK, 4 rows affected (0.02 sec)
  11. Records: 4 Duplicates: 0 Warnings: 0
  12. mysql> select * from shuihuchuan;
  13. +----+-----------+---------------+
  14. | id | people | nickname |
  15. +----+-----------+---------------+
  16. | 1 | songjiang | hubaoyi |
  17. | 2 | chaogai | tuotatianwang |
  18. | 3 | lujunyi | yuqinlin |
  19. | 4 | wuyong | zhiduoxing |
  20. +----+-----------+---------------+
  21. 4 rows in set (0.00 sec)
2.1.2、备库上查看同步情况
 
 
  1. root@localhost : sbtest 05:34:54> show tables;
  2. +------------------+
  3. | Tables_in_sbtest |
  4. +------------------+
  5. | sanguoyanyi |
  6. | shuihuchuan |
  7. | test |
  8. | xiyouji |
  9. +------------------+
  10. 4 rows in set (0.00 sec)
  11. root@localhost : sbtest 05:34:56> select * from shuihuchuan;
  12. +----+-----------+---------------+
  13. | id | people | nickname |
  14. +----+-----------+---------------+
  15. | 1 | songjiang | hubaoyi |
  16. | 2 | chaogai | tuotatianwang |
  17. | 3 | lujunyi | yuqinlin |
  18. | 4 | wuyong | zhiduoxing |
  19. +----+-----------+---------------+
  20. 4 rows in set (0.00 sec)
2.1.3、备库上插入数据
 
 
  1. root@localhost : sbtest 05:36:46> insert into shuihuchuan(nickname,people) values ('ruyunlong','gongsunsheng');
  2. Query OK, 1 row affected (0.03 sec)
  3. root@localhost : sbtest 05:38:50> select * from shuihuchuan;
  4. +----+--------------+---------------+
  5. | id | people | nickname |
  6. +----+--------------+---------------+
  7. | 1 | songjiang | hubaoyi |
  8. | 2 | chaogai | tuotatianwang |
  9. | 3 | lujunyi | yuqinlin |
  10. | 4 | wuyong | zhiduoxing |
  11. | 5 | gongsunsheng | ruyunlong |
  12. +----+--------------+---------------+
  13. 5 rows in set (0.00 sec)
2.1.4、主库上插入数据
 
 
  1. mysql> insert into shuihuchuan(nickname,people) values ('ruyunlong','gongsunsheng');
  2. Query OK, 1 row affected (0.02 sec)
  3. mysql> select * from shuihuchuan;
  4. +----+--------------+---------------+
  5. | id | people | nickname |
  6. +----+--------------+---------------+
  7. | 1 | songjiang | hubaoyi |
  8. | 2 | chaogai | tuotatianwang |
  9. | 3 | lujunyi | yuqinlin |
  10. | 4 | wuyong | zhiduoxing |
  11. | 5 | gongsunsheng | ruyunlong |
  12. +----+--------------+---------------+
  13. 5 rows in set (0.00 sec)
2.1.5、查看备库复制情况
 
 
  1. root@localhost : sbtest 05:41:14> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 192.168.1.21
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000026
  9. Read_Master_Log_Pos: 2801
  10. Relay_Log_File: mysql-relay-bin.000006
  11. Relay_Log_Pos: 2202
  12. Relay_Master_Log_File: mysql-bin.000026
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: No
  15. Replicate_Do_DB: sbtest
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 1062
  22. Last_Error: Could not execute Write_rows event on table sbtest.shuihuchuan; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000026, end_log_pos 2770
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 2506
  25. Relay_Log_Space: 2836
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 1062
  40. Last_SQL_Error: Could not execute Write_rows event on table sbtest.shuihuchuan; Duplicate entry '5' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000026, end_log_pos 2770
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 12001
  43. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State:
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp: 170522 05:40:50
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0
  57. 1 row in set (0.00 sec)
  58. #可以看到报主键重复的错误

2.2、主键重复错误解决

2.2.1、备库上删除重复主键的记录
 
 
  1. root@localhost : sbtest 05:46:29> stop slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. root@localhost : sbtest 05:41:17> delete from shuihuchuan where id = 5;
  4. Query OK, 1 row affected (0.04 sec)
  5. root@localhost : sbtest 05:46:40> start slave;
  6. Query OK, 0 rows affected (0.02 sec)
  7. root@localhost : sbtest 05:46:46> show slave status\G;
  8. *************************** 1. row ***************************
  9. Slave_IO_State: Waiting for master to send event
  10. Master_Host: 192.168.1.21
  11. Master_User: repl
  12. Master_Port: 3306
  13. Connect_Retry: 60
  14. Master_Log_File: mysql-bin.000026
  15. Read_Master_Log_Pos: 2801
  16. Relay_Log_File: mysql-relay-bin.000007
  17. Relay_Log_Pos: 286
  18. Relay_Master_Log_File: mysql-bin.000026
  19. Slave_IO_Running: Yes
  20. Slave_SQL_Running: Yes
  21. Replicate_Do_DB: sbtest
  22. Replicate_Ignore_DB:
  23. Replicate_Do_Table:
  24. Replicate_Ignore_Table:
  25. Replicate_Wild_Do_Table:
  26. Replicate_Wild_Ignore_Table:
  27. Last_Errno: 0
  28. Last_Error:
  29. Skip_Counter: 0
  30. Exec_Master_Log_Pos: 2801
  31. Relay_Log_Space: 2836
  32. Until_Condition: None
  33. Until_Log_File:
  34. Until_Log_Pos: 0
  35. Master_SSL_Allowed: No
  36. Master_SSL_CA_File:
  37. Master_SSL_CA_Path:
  38. Master_SSL_Cert:
  39. Master_SSL_Cipher:
  40. Master_SSL_Key:
  41. Seconds_Behind_Master: 0
  42. Master_SSL_Verify_Server_Cert: No
  43. Last_IO_Errno: 0
  44. Last_IO_Error:
  45. Last_SQL_Errno: 0
  46. Last_SQL_Error:
  47. Replicate_Ignore_Server_Ids:
  48. Master_Server_Id: 12001
  49. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  50. Master_Info_File: mysql.slave_master_info
  51. SQL_Delay: 0
  52. SQL_Remaining_Delay: NULL
  53. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  54. Master_Retry_Count: 86400
  55. Master_Bind:
  56. Last_IO_Error_Timestamp:
  57. Last_SQL_Error_Timestamp:
  58. Master_SSL_Crl:
  59. Master_SSL_Crlpath:
  60. Retrieved_Gtid_Set:
  61. Executed_Gtid_Set:
  62. Auto_Position: 0
  63. 1 row in set (0.00 sec)

3、ERROR 1032 删除或更新数据,从库找不到记录

3.1、Could not execute Delete_rows错误

3.1.1、模拟复制报错
 
 
  1. #先在备库上将id=2记录删除
  2. root@localhost : sbtest 06:19:08> delete from shuihuchuan where id = 2;
  3. Query OK, 1 row affected (0.02 sec)
  4. #在主库上删除记录
  5. mysql> delete from shuihuchuan where id = 2;
  6. Query OK, 1 row affected (0.03 sec)
  7. #查看备库复制状态
  8. root@localhost : sbtest 06:19:27> show slave status\G;
  9. *************************** 1. row ***************************
  10. Slave_IO_State: Waiting for master to send event
  11. Master_Host: 192.168.1.21
  12. Master_User: repl
  13. Master_Port: 3306
  14. Connect_Retry: 60
  15. Master_Log_File: mysql-bin.000026
  16. Read_Master_Log_Pos: 3095
  17. Relay_Log_File: mysql-relay-bin.000007
  18. Relay_Log_Pos: 286
  19. Relay_Master_Log_File: mysql-bin.000026
  20. Slave_IO_Running: Yes
  21. Slave_SQL_Running: No
  22. Replicate_Do_DB: sbtest
  23. Replicate_Ignore_DB:
  24. Replicate_Do_Table:
  25. Replicate_Ignore_Table:
  26. Replicate_Wild_Do_Table:
  27. Replicate_Wild_Ignore_Table:
  28. Last_Errno: 1032
  29. Last_Error: Could not execute Delete_rows event on table sbtest.shuihuchuan; Can't find record in 'shuihuchuan', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000026, end_log_pos 3064
  30. Skip_Counter: 0
  31. Exec_Master_Log_Pos: 2801
  32. Relay_Log_Space: 3130
  33. Until_Condition: None
  34. Until_Log_File:
  35. Until_Log_Pos: 0
  36. Master_SSL_Allowed: No
  37. Master_SSL_CA_File:
  38. Master_SSL_CA_Path:
  39. Master_SSL_Cert:
  40. Master_SSL_Cipher:
  41. Master_SSL_Key:
  42. Seconds_Behind_Master: NULL
  43. Master_SSL_Verify_Server_Cert: No
  44. Last_IO_Errno: 0
  45. Last_IO_Error:
  46. Last_SQL_Errno: 1032
  47. Last_SQL_Error: Could not execute Delete_rows event on table sbtest.shuihuchuan; Can't find record in 'shuihuchuan', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000026, end_log_pos 3064
  48. Replicate_Ignore_Server_Ids:
  49. Master_Server_Id: 12001
  50. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  51. Master_Info_File: mysql.slave_master_info
  52. SQL_Delay: 0
  53. SQL_Remaining_Delay: NULL
  54. Slave_SQL_Running_State:
  55. Master_Retry_Count: 86400
  56. Master_Bind:
  57. Last_IO_Error_Timestamp:
  58. Last_SQL_Error_Timestamp: 170522 06:21:01
  59. Master_SSL_Crl:
  60. Master_SSL_Crlpath:
  61. Retrieved_Gtid_Set:
  62. Executed_Gtid_Set:
  63. Auto_Position: 0
  64. 1 row in set (0.00 sec)
3.1.2、记录已经不存在,直接跳过错误就可以
 
 
  1. root@localhost : sbtest 06:25:11> stop slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. root@localhost : sbtest 06:26:22> set global sql_slave_skip_counter=2;
  4. Query OK, 0 rows affected (0.00 sec)
  5. #具体跳过多少事务需要结合slave status中的Read_Master_Log_Pos、Exec_Master_Log_Pos以及show binlog events in 'mysql-bin.000026'中的事务项来判断
  6. root@localhost : sbtest 06:26:26> start slave;
  7. Query OK, 0 rows affected (0.02 sec)
  8. root@localhost : sbtest 06:26:28> show slave status\G;
  9. *************************** 1. row ***************************
  10. Slave_IO_State: Waiting for master to send event
  11. Master_Host: 192.168.1.21
  12. Master_User: repl
  13. Master_Port: 3306
  14. Connect_Retry: 60
  15. Master_Log_File: mysql-bin.000026
  16. Read_Master_Log_Pos: 3095
  17. Relay_Log_File: mysql-relay-bin.000009
  18. Relay_Log_Pos: 286
  19. Relay_Master_Log_File: mysql-bin.000026
  20. Slave_IO_Running: Yes
  21. Slave_SQL_Running: Yes
  22. Replicate_Do_DB: sbtest
  23. Replicate_Ignore_DB:
  24. Replicate_Do_Table:
  25. Replicate_Ignore_Table:
  26. Replicate_Wild_Do_Table:
  27. Replicate_Wild_Ignore_Table:
  28. Last_Errno: 0
  29. Last_Error:
  30. Skip_Counter: 0
  31. Exec_Master_Log_Pos: 3095
  32. Relay_Log_Space: 625
  33. Until_Condition: None
  34. Until_Log_File:
  35. Until_Log_Pos: 0
  36. Master_SSL_Allowed: No
  37. Master_SSL_CA_File:
  38. Master_SSL_CA_Path:
  39. Master_SSL_Cert:
  40. Master_SSL_Cipher:
  41. Master_SSL_Key:
  42. Seconds_Behind_Master: 0
  43. Master_SSL_Verify_Server_Cert: No
  44. Last_IO_Errno: 0
  45. Last_IO_Error:
  46. Last_SQL_Errno: 0
  47. Last_SQL_Error:
  48. Replicate_Ignore_Server_Ids:
  49. Master_Server_Id: 12001
  50. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  51. Master_Info_File: mysql.slave_master_info
  52. SQL_Delay: 0
  53. SQL_Remaining_Delay: NULL
  54. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  55. Master_Retry_Count: 86400
  56. Master_Bind:
  57. Last_IO_Error_Timestamp:
  58. Last_SQL_Error_Timestamp:
  59. Master_SSL_Crl:
  60. Master_SSL_Crlpath:
  61. Retrieved_Gtid_Set:
  62. Executed_Gtid_Set:
  63. Auto_Position: 0
  64. 1 row in set (0.00 sec)

3.2、Could not execute Update_rows错误

3.2.1、模拟复制报错
 
 
  1. #备库上删除id=5的记录
  2. root@localhost : sbtest 06:30:10> delete from shuihuchuan where id = 5;
  3. Query OK, 1 row affected (0.03 sec)
  4. #主库上更新id=5的记录
  5. mysql> update shuihuchuan set people = 'guansheng',nickname = 'dadao' where id = 5 ;
  6. Query OK, 1 row affected (0.02 sec)
  7. Rows matched: 1 Changed: 1 Warnings: 0
  8. #查看备库复制情况
  9. root@localhost : sbtest 06:30:36> show slave status\G;
  10. *************************** 1. row ***************************
  11. Slave_IO_State: Waiting for master to send event
  12. Master_Host: 192.168.1.21
  13. Master_User: repl
  14. Master_Port: 3306
  15. Connect_Retry: 60
  16. Master_Log_File: mysql-bin.000026
  17. Read_Master_Log_Pos: 3412
  18. Relay_Log_File: mysql-relay-bin.000009
  19. Relay_Log_Pos: 286
  20. Relay_Master_Log_File: mysql-bin.000026
  21. Slave_IO_Running: Yes
  22. Slave_SQL_Running: No
  23. Replicate_Do_DB: sbtest
  24. Replicate_Ignore_DB:
  25. Replicate_Do_Table:
  26. Replicate_Ignore_Table:
  27. Replicate_Wild_Do_Table:
  28. Replicate_Wild_Ignore_Table:
  29. Last_Errno: 1032
  30. Last_Error: Could not execute Update_rows event on table sbtest.shuihuchuan; Can't find record in 'shuihuchuan', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000026, end_log_pos 3381
  31. Skip_Counter: 0
  32. Exec_Master_Log_Pos: 3095
  33. Relay_Log_Space: 942
  34. Until_Condition: None
  35. Until_Log_File:
  36. Until_Log_Pos: 0
  37. Master_SSL_Allowed: No
  38. Master_SSL_CA_File:
  39. Master_SSL_CA_Path:
  40. Master_SSL_Cert:
  41. Master_SSL_Cipher:
  42. Master_SSL_Key:
  43. Seconds_Behind_Master: NULL
  44. Master_SSL_Verify_Server_Cert: No
  45. Last_IO_Errno: 0
  46. Last_IO_Error:
  47. Last_SQL_Errno: 1032
  48. Last_SQL_Error: Could not execute Update_rows event on table sbtest.shuihuchuan; Can't find record in 'shuihuchuan', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000026, end_log_pos 3381
  49. Replicate_Ignore_Server_Ids:
  50. Master_Server_Id: 12001
  51. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  52. Master_Info_File: mysql.slave_master_info
  53. SQL_Delay: 0
  54. SQL_Remaining_Delay: NULL
  55. Slave_SQL_Running_State:
  56. Master_Retry_Count: 86400
  57. Master_Bind:
  58. Last_IO_Error_Timestamp:
  59. Last_SQL_Error_Timestamp: 170522 06:32:13
  60. Master_SSL_Crl:
  61. Master_SSL_Crlpath:
  62. Retrieved_Gtid_Set:
  63. Executed_Gtid_Set:
  64. Auto_Position: 0
  65. 1 row in set (0.00 sec)
3.2.2、从库恢复不存在的记录,并解决错误
 
 
  1. #利用mysqlbinlog找出出错对应位置的执行语句,通过sql语句找到当时update对应的主键值
  2. [root@host-192-168-1-21 binlog]# mysqlbinlog -vv --base64-output=decode-rows --start-position=3095 --stop-position=3381 mysql-bin.000026 |grep -i -10 update
  3. SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
  4. /*!\C utf8mb4 *//*!*/;
  5. SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=45/*!*/;
  6. SET @@session.lc_time_names=0/*!*/;
  7. SET @@session.collation_database=DEFAULT/*!*/;
  8. BEGIN
  9. /*!*/;
  10. # at 3234
  11. #170522 6:31:47 server id 12001 end_log_pos 3296 CRC32 0xc7c33055 Table_map: `sbtest`.`shuihuchuan` mapped to number 337
  12. # at 3296
  13. #170522 6:31:47 server id 12001 end_log_pos 3381 CRC32 0x16f7949a Update_rows: table id 337 flags: STMT_END_F
  14. ### UPDATE `sbtest`.`shuihuchuan`
  15. ### WHERE
  16. ### @1=5 /* INT meta=0 nullable=0 is_null=0 */
  17. ### @2='gongsunsheng' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
  18. ### @3='ruyunlong' /* VARSTRING(120) meta=120 nullable=1 is_null=0 */
  19. ### SET
  20. ### @1=5 /* INT meta=0 nullable=0 is_null=0 */
  21. ### @2='guansheng' /* VARSTRING(80) meta=80 nullable=1 is_null=0 */
  22. ### @3='dadao' /* VARSTRING(120) meta=120 nullable=1 is_null=0 */
  23. ROLLBACK /* added by mysqlbinlog */ /*!*/;
  24. SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
  25. #利用主键值找到主库对应的数据行
  26. mysql> select * from shuihuchuan where id = 5;
  27. +----+-----------+----------+
  28. | id | people | nickname |
  29. +----+-----------+----------+
  30. | 5 | guansheng | dadao |
  31. +----+-----------+----------+
  32. 1 row in set (0.00 sec)
  33. #在备库中插入主库查询到的记录
  34. root@localhost : sbtest 06:50:23> stop slave;
  35. Query OK, 0 rows affected (0.02 sec)
  36. root@localhost : sbtest 06:49:56> insert into shuihuchuan(id,people,nickname) values (5,'guansheng','dadao');
  37. Query OK, 1 row affected (0.04 sec)
  38. root@localhost : sbtest 06:50:06> select * from shuihuchuan;
  39. +----+-----------+------------+
  40. | id | people | nickname |
  41. +----+-----------+------------+
  42. | 1 | songjiang | hubaoyi |
  43. | 3 | lujunyi | yuqinlin |
  44. | 4 | wuyong | zhiduoxing |
  45. | 5 | guansheng | dadao |
  46. +----+-----------+------------+
  47. 4 rows in set (0.00 sec)
  48. root@localhost : sbtest 06:50:29> start slave;
  49. Query OK, 0 rows affected (0.03 sec)
  50. root@localhost : sbtest 06:50:35> show slave status\G;
  51. *************************** 1. row ***************************
  52. Slave_IO_State: Waiting for master to send event
  53. Master_Host: 192.168.1.21
  54. Master_User: repl
  55. Master_Port: 3306
  56. Connect_Retry: 60
  57. Master_Log_File: mysql-bin.000026
  58. Read_Master_Log_Pos: 3412
  59. Relay_Log_File: mysql-relay-bin.000010
  60. Relay_Log_Pos: 286
  61. Relay_Master_Log_File: mysql-bin.000026
  62. Slave_IO_Running: Yes
  63. Slave_SQL_Running: Yes
  64. Replicate_Do_DB: sbtest
  65. Replicate_Ignore_DB:
  66. Replicate_Do_Table:
  67. Replicate_Ignore_Table:
  68. Replicate_Wild_Do_Table:
  69. Replicate_Wild_Ignore_Table:
  70. Last_Errno: 0
  71. Last_Error:
  72. Skip_Counter: 0
  73. Exec_Master_Log_Pos: 3412
  74. Relay_Log_Space: 942
  75. Until_Condition: None
  76. Until_Log_File:
  77. Until_Log_Pos: 0
  78. Master_SSL_Allowed: No
  79. Master_SSL_CA_File:
  80. Master_SSL_CA_Path:
  81. Master_SSL_Cert:
  82. Master_SSL_Cipher:
  83. Master_SSL_Key:
  84. Seconds_Behind_Master: 0
  85. Master_SSL_Verify_Server_Cert: No
  86. Last_IO_Errno: 0
  87. Last_IO_Error:
  88. Last_SQL_Errno: 0
  89. Last_SQL_Error:
  90. Replicate_Ignore_Server_Ids:
  91. Master_Server_Id: 12001
  92. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  93. Master_Info_File: mysql.slave_master_info
  94. SQL_Delay: 0
  95. SQL_Remaining_Delay: NULL
  96. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  97. Master_Retry_Count: 86400
  98. Master_Bind:
  99. Last_IO_Error_Timestamp:
  100. Last_SQL_Error_Timestamp:
  101. Master_SSL_Crl:
  102. Master_SSL_Crlpath:
  103. Retrieved_Gtid_Set:
  104. Executed_Gtid_Set:
  105. Auto_Position: 0
  106. 1 row in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值