全局读锁通常是由flush table with read lock;这类语句添加,这类语句通常是在各种备份工具为了拿到一致性备份时使用,另外,在具有主从复制架构的环境中做主备切换时也常常使用,除了这两种情况之外,还有一种情况也是最难排查的一种情况,那就是线上系统权限约束不规范的时候,各种人员使用的数据库帐号都具有RELOAD权限时,都可以对数据库加全局读锁。
root@localhost : performance_schema 05:18:09> select * from; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 然后,我们查询events_statements_history_long表中错误号为1064的记录,开启另一个会话(会话2) root@localhost : sbtest 05:32:55> use performance_schema Database changed root@localhost : performance_schema 05:33:03> select THREAD_ID,EVENT_NAME,SOURCE,sys.format_time(TIMER_WAIT) as exec_time,sys.format_time(LOCK_TIME) as lock_time,SQL_TEXT,CURRENT_SCHEMA,MESSAGE_TEXT,ROWS_AFFECTED,ROWS_SENT,ROWS_EXAMINED,MYSQL_ERRNO from events_statements_history where MYSQL_ERRNO=1064/G; *************************** 1. row *************************** THREAD_ID: 119 EVENT_NAME: statement/sql/error SOURCE: socket_connection.cc:101 exec_time: 71.72 us lock_time: 0 ps SQL_TEXT: select * from CURRENT_SCHEMA: sbtest MESSAGE_TEXT: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use ROWS_AFFECTED: 0 ROWS_SENT: 0 ROWS_EXAMINED: 0 MYSQL_ERRNO: 1064 1 row in set (0.01 sec) 可能你不知道错误号是多少,可以查询发生错误次数不为0的语句记录,在里边找到MESSAGE_TEXT字段提示信息为语法错误的就是它了。 root@localhost : performance_schema 05:34:00> select THREAD_ID,EVENT_NAME,SOURCE,sys.format_time(TIMER_WAIT) as exec_time,sys.format_time(LOCK_TIME) as lock_time,SQL_TEXT,CURRENT_SCHEMA,MESSAGE_TEXT,ROWS_AFFECTED,ROWS_SENT,ROWS_EXAMINED,MYSQL_ERRNO,errors from events_statements_history where errors>0/G; *************************** 1. row *************************** THREAD_ID: 119 EVENT_NAME: statement/sql/error SOURCE: socket_connection.cc:101 exec_time: 71.72 us lock_time: 0 ps SQL_TEXT: select * from CURRENT_SCHEMA: sbtest MESSAGE_TEXT: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use ROWS_AFFECTED: 0 ROWS_SENT: 0 ROWS_EXAMINED: 0 MYSQL_ERRNO: 1064 errors: 1 1 row in set (0.00 sec) 使用events_statements_summary_by_digest表查询发生语句执行错误的SQL语句记录,首先,我们在会话1制造一两个语句执行一定会发生错误的语句。 root@localhost : sbtest 05:32:34> select * ; ERROR 1096 (HY000): No tables used root@localhost : sbtest 05:40:57> select * from sbtest4 where id between 100 and 2000 and xx=1; ERROR 1054 (42S22): Unknown column 'xx' in 'where clause' 然后,我们在events_statements_summary_by_digest表中查询发生错误次数大于0的记录,在会话2执行。 root@localhost : performance_schema 05:34:03> select SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,sys.format_time(AVG_TIMER_WAIT) as avg_time,sys.format_time(MAX_TIMER_WAIT) as max_time,sys.format_time(SUM_LOCK_TIME) as sum_lock_time,SUM_ERRORS,FIRST_SEEN,LAST_SEEN from events_statements_summary_by_digest where SUM_ERRORS!=0/G; *************************** 1. row *************************** ...... *************************** 10. row *************************** SCHEMA_NAME: sbtest DIGEST_TEXT: SELECT * # 这里就是第一个执行错误的语句 COUNT_STAR: 1 avg_time: 55.14 us max_time: 55.14 us sum_lock_time: 0 ps SUM_ERRORS: 1 FIRST_SEEN: 2018-06-25 17:40:57 LAST_SEEN: 2018-06-25 17:40:57 *************************** 11. row *************************** SCHEMA_NAME: sbtest DIGEST_TEXT: SELECT * FROM `sbtest4` WHERE `id` BETWEEN ? AND ? AND `xx` = ? # 这里就是第二个执行错误的语句 COUNT_STAR: 1 avg_time: 101.68 us max_time: 101.68 us sum_lock_time: 0 ps SUM_ERRORS: 1 FIRST_SEEN: 2018-06-25 17:41:03 LAST_SEEN: 2018-06-25 17:41:03 11 rows in set (0.00 sec) PS:我们前面说过,events_statements_summary_by_digest表中不记录具体的错误信息,只做错误语句统计,所以,如果需要查询到具体的错误信息(如:具体的错误代码,具体的错误提示信息以及具体的错误SQL文本等),还需要查询events_statements_history或者events_statements_history_long表。
root@localhost : performance_schema 05:45:03> select THREAD_ID,EVENT_NAME,SOURCE,sys.format_time(TIMER_WAIT) as exec_time,sys.format_time(LOCK_TIME) as lock_time,SQL_TEXT,CURRENT_SCHEMA,MESSAGE_TEXT,ROWS_AFFECTED,ROWS_SENT,ROWS_EXAMINED,MYSQL_ERRNO from events_statements_history where MYSQL_ERRNO!=0/G; *************************** 1. row *************************** ...... *************************** 2. row *************************** THREAD_ID: 119 EVENT_NAME: statement/sql/select SOURCE: socket_connection.cc:101 exec_time: 55.14 us lock_time: 0 ps SQL_TEXT: select * CURRENT_SCHEMA: sbtest MESSAGE_TEXT: No tables used ROWS_AFFECTED: 0 ROWS_SENT: 0 ROWS_EXAMINED: 0 MYSQL_ERRNO: 1096 *************************** 3. row *************************** THREAD_ID: 119 EVENT_NAME: statement/sql/select SOURCE: socket_connection.cc:101 exec_time: 101.68 us lock_time: 0 ps SQL_TEXT: select * from sbtest4 where id between 100 and 2000 and xx=1 CURRENT_SCHEMA: sbtest MESSAGE_TEXT: Unknown column 'xx' in 'where clause' ROWS_AFFECTED: 0 ROWS_SENT: 0 ROWS_EXAMINED: 0 MYSQL_ERRNO: 1054 3 rows in set (0.00 sec) 4. 查看SQL执行进度信息
admin@localhost : (none) 12:45:19> 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线程具体的报错原因。
admin@localhost : (none) 12:51:53> 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表可以发现,具体的复制报错信息是因为主键冲突了