千金良方:MySQL性能优化金字塔法则
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

6.6 查看多线程复制报错详情

官方MySQL从5.6版本开始支持基于库级别的并行复制,在MySQL 5.7版本中支持基于事务的并行复制,在启用了并行复制之后,一旦发生复制报错,通常通过show slave status语句无法查看到具体的报错详情(通过show slave status语句只能查看到SQL线程的报错信息,而在多线程复制下,SQL线程的报错信息是根据Worker线程的报错信息汇总的信息),类似如下:

mysql> show slave status\G
............
                Last_Errno: 1062
                Last_Error: Coordinator stopped because there were error(s)in the worker(s).  The  most  recent  failure  being:  Worker  1  failed  executing  transaction '23fb5832-e4bc-11e7-8ea4-525400a4b2e1:2553990'at master log mysql-bin.000034, end_log_pos 98797. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
............
            Last_SQL_Errno: 1062
            Last_SQL_Error: Coordinator stopped because there were error(s)in the worker(s).  The  most  recent  failure  being:  Worker  1  failed  executing  transaction '23fb5832-e4bc-11e7-8ea4-525400a4b2e1:2553990'at master log mysql-bin.000034, end_log_pos 98797. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
............
1 row in set(0.00 sec)

根据报错提示查看performance_schema.replication_applier_status_by_worker表,在该表中详细记录了每一个Worker线程的详细信息,在这里我们就可以找到发生报错的Worker线程具体的报错原因。

mysql> select * from performance_schema.replication_applier_status_by_worker where LAST_ERROR_MESSAGE! =''\G
*************************** 1. row ***************************
      CHANNEL_NAME:
          WORKER_ID: 2
          THREAD_ID: NULL
      SERVICE_STATE: OFF
LAST_SEEN_TRANSACTION: 23fb5832-e4bc-11e7-8ea4-525400a4b2e1:2553991
    LAST_ERROR_NUMBER: 1062
  LAST_ERROR_MESSAGE: Worker 2 failed executing transaction '23fb5832-e4bc-11e7-8ea4-525400a4b2e1:2553991' at master log mysql-bin.000034, end_log_pos 99514; Could not execute Write_rows event on table sbtest.sbtest4; Duplicate entry '833353' for key 'PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log FIRST, end_log_pos 99514
LAST_ERROR_TIMESTAMP: 2018-01-02 14:08:58
1 row in set(0.00 sec)

从查询performance_schema.replication_applier_status_by_worker表的数据中可以发现,具体的复制报错原因是主键冲突了。

提示:由于历史原因,performance_schema中的复制信息记录表只记录与GTID相关的信息,而mysql系统字典库下的slave_master_info、slave_relay_log_info、slave_worker_info表记录的是与binlog位置相关的信息。另外,如果选择相关的复制信息记录到文件中,那么磁盘上还存在着master.info、relay_log.info等文件记录与binlog位置相关的信息。

至此,关于performance_schema的介绍暂且告一段落(更详细的内容可参阅微信公众号“沃趣技术”,其中我们用9个章节对其进行了全方位的介绍),在后续的章节中我们会把performance_schema的知识更多地引入案例中,以方便大家更方便地掌握它。关于performance_schema的使用场景还需要大家共同去挖掘。