【0】核心表
sys.`innodb_lock_waits`
performance_schema.events_statements_history
performance_schema.data_locks
performance_schema.data_locks_wait
【1】查看锁信息
performance_schema.data_locks
performance_schema.data_locks_wait
线上mysql版本为8.0.11,对于最近的几个版本越来越像Orale了,有了较为丰富的系统字典,也可以查看系统的等待事件,现在线上出现被锁语句的情况也可以通过数据字典直接查询出来了,在此记录下以供参考。
首先查看系统锁情况,下面这条语句可以看到持有锁的线程以及线程ID,这里需要注意下,session_id也就是show processlist查询出来的链接ID,在这个表里表现为PID,同时该表也记录了trx_id,最后还给出了解决方案也就是如何kill持有锁的进程。
mysql> SELECT * FROM sys.`innodb_lock_waits` \G *************************** 1. row *************************** wait_started: 2019-01-28 14:27:03 wait_age: 00:00:03 wait_age_secs: 3 locked_table: `test`.`t` locked_table_schema: test locked_table_name: t locked_table_partition: NULL locked_table_subpartition: NULL locked_index: GEN_CLUST_INDEX locked_type: RECORD waiting_trx_id: 19758723 waiting_trx_started: 2019-01-28 14:27:03 waiting_trx_age: 00:00:03 waiting_trx_rows_locked: 1 waiting_trx_rows_modified: 0 waiting_pid: 4202 waiting_query: update t set id=8 waiting_lock_id: 19758723:4998:4:2 waiting_lock_mode: X blocking_trx_id: 19748728 blocking_pid: 4200 blocking_query: NULL blocking_lock_id: 19748728:4998:4:2 blocking_lock_mode: X blocking_trx_started: 2019-01-28 10:58:27 blocking_trx_age: 03:28:39 blocking_trx_rows_locked: 2 blocking_trx_rows_modified: 1 sql_kill_blocking_query: KILL QUERY 4200 sql_kill_blocking_connection: KILL 4200 1 row in set (0.02 sec
【2】查看当前被锁的语句
##等待锁的语句 SELECT * FROM performance_schema.events_statements_history WHERE thread_id IN( SELECT b.`THREAD_ID` FROM sys.`innodb_lock_waits` AS a , performance_schema.threads AS b WHERE a.waiting_pid = b.`PROCESSLIST_ID`) ORDER BY timer_start ASC;
【3】看持有锁的语句
可以通过如下方式
##持有锁的语句 SELECT * FROM performance_schema.events_statements_history WHERE thread_id IN( SELECT b.`THREAD_ID` FROM sys.`innodb_lock_waits` AS a , performance_schema.threads AS b WHERE a.`blocking_pid` = b.`PROCESSLIST_ID`) ORDER BY timer_start ASC;
大部分转自:https://www.pianshen.com/article/3307195897/