os: centos 7.4.1708
db: mysql 8.0.19
任何数据库都需要对锁进行监控,尤其是长时间的锁,mysql 也不例外。
mysql 通过 show full processlist、show engine innodb status 可以查看到。
其实通过 information_schema.innodb_trx 更容易监控到锁的情况。
[root@node2 ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
[root@node2 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.19 |
+-----------+
1 row in set (0.00 sec)
mysql> create table tmp_t0 (
id int not null,
name varchar(100),
memo varchar(100),
primary key (id)
)
;
mysql> insert into tmp_t0(id,name,memo)
values(1,'a','aa'),(2,'b','bb'),(3,'c','cc');
session 1 窗口操作
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tmp_t0 where id=2 for update;
+----+------+------+
| id | name | memo |
+----+------+------+
| 2 | b | bb |
+----+------+------+
1 row in set (0.00 sec)
mysql>
session 2 窗口操作
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tmp_t0 where id=2 for update;
session 2 执行 for update 后就一直处于等待状态
session 3 窗口操作
mysql> show full processlist;
+----+-----------------+-----------+------+---------+------+------------------------+--------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+------+------------------------+--------------------------------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 2369 | Waiting on empty queue | NULL |
| 8 | root | localhost | test | Sleep | 859 | | NULL |
| 10 | root | localhost | test | Query | 38 | statistics | select * from tmp_t0 where id=2 for update |
| 11 | root | localhost | NULL | Query | 0 | starting | show full processlist |
+----+-----------------+-----------+------+---------+------+------------------------+--------------------------------------------+
4 rows in set (0.00 sec)
可以看到 id = 4 状态为 Waiting on empty queue
截取了 TRANSACTIONS 部分
mysql> show engine innodb status\G
------------
TRANSACTIONS
------------
Trx id counter 2073
Purge done for trx's n:o < 2070 undo n:o < 0 state: running but idle
History list length 11
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421962444566840, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421962444564224, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 2072, ACTIVE 136 sec
1 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 10, OS thread handle 140487441049344, query id 39 localhost root
---TRANSACTION 2070, ACTIVE 957 sec
2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 8, OS thread handle 140487441340160, query id 23 localhost root
mysql> select * from information_schema.innodb_trx;
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| trx_id | trx_state | trx_started | trx_requested_lock_id | trx_wait_started | trx_weight | trx_mysql_thread_id | trx_query | trx_operation_state | trx_tables_in_use | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking |
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
| 2072 | RUNNING | 2020-03-27 09:04:08 | NULL | NULL | 1 | 10 | NULL | NULL | 0 | 1 | 1 | 1136 | 1 | 0 | 0 | REPEATABLE READ | 1 | 1 | NULL | 0 | 0 | 0 | 0 |
| 2070 | RUNNING | 2020-03-27 08:50:27 | NULL | NULL | 2 | 8 | NULL | NULL | 0 | 1 | 2 | 1136 | 1 | 0 | 0 | REPEATABLE READ | 1 | 1 | NULL | 0 | 0 | 0 | 0 |
+--------+-----------+---------------------+-----------------------+------------------+------------+---------------------+-----------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+
2 rows in set (0.00 sec)
mysql> select * from performance_schema.events_stages_current;
mysql> select * from performance_schema.events_statements_current;
+-----------+----------+--------------+----------------------+---------------------------------+------------------+------------------+----------------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+-------------+---------------+-------------+-----------------------+-------------+-------------------+--------------------------------------------------------+--------+----------+---------------+-----------+---------------+-------------------------+--------------------+------------------+------------------------+--------------+--------------------+-------------+-------------------+------------+-----------+-----------+---------------+--------------------+------------------+--------------------+---------------------+--------------+
| THREAD_ID | EVENT_ID | END_EVENT_ID | EVENT_NAME | SOURCE | TIMER_START | TIMER_END | TIMER_WAIT | LOCK_TIME | SQL_TEXT | DIGEST | DIGEST_TEXT | CURRENT_SCHEMA | OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | OBJECT_INSTANCE_BEGIN | MYSQL_ERRNO | RETURNED_SQLSTATE | MESSAGE_TEXT | ERRORS | WARNINGS | ROWS_AFFECTED | ROWS_SENT | ROWS_EXAMINED | CREATED_TMP_DISK_TABLES | CREATED_TMP_TABLES | SELECT_FULL_JOIN | SELECT_FULL_RANGE_JOIN | SELECT_RANGE | SELECT_RANGE_CHECK | SELECT_SCAN | SORT_MERGE_PASSES | SORT_RANGE | SORT_ROWS | SORT_SCAN | NO_INDEX_USED | NO_GOOD_INDEX_USED | NESTING_EVENT_ID | NESTING_EVENT_TYPE | NESTING_EVENT_LEVEL | STATEMENT_ID |
+-----------+----------+--------------+----------------------+---------------------------------+------------------+------------------+----------------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+-------------+---------------+-------------+-----------------------+-------------+-------------------+--------------------------------------------------------+--------+----------+---------------+-----------+---------------+-------------------------+--------------------+------------------+------------------------+--------------+--------------------+-------------+-------------------+------------+-----------+-----------+---------------+--------------------+------------------+--------------------+---------------------+--------------+
| 31 | 1 | 1 | statement/sql/select | srv_session.cc:1107 | 3664208536000 | 3664499768000 | 291232000 | 0 | SELECT @@skip_networking, @@skip_name_resolve, @@have_ssl='YES', @@ssl_key, @@ssl_ca, @@ssl_capath, @@ssl_cert, @@ssl_cipher, @@ssl_crl, @@ssl_crlpath, @@tls_version | 1ebda414995104bec2d678a2f3a337c8cc504cafa93982ba395404ef9dbc5419 | SELECT @@`skip_networking` , @@`skip_name_resolve` , @@`have_ssl` = ? , @@`ssl_key` , @@`ssl_ca` , @@`ssl_capath` , @@`ssl_cert` , @@`ssl_cipher` , @@`ssl_crl` , @@`ssl_crlpath` , @@`tls_version` | NULL | NULL | NULL | NULL | NULL | 0 | NULL | NULL | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL | 0 | 2 |
| 48 | 23 | 23 | statement/sql/select | init_net_server_extension.cc:95 | 1514092713290000 | 1514092937623000 | 224333000 | 110000000 | select * from tmp_t0 where id=2 for update | ce70bef8d279a18cef7f9f297370a959f18de8b6bfce756fdf7beea6b178a3ef | SELECT * FROM `tmp_t0` WHERE `id` = ? FOR UPDATE | test | NULL | NULL | NULL | NULL | 0 | NULL | NULL | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 22 | TRANSACTION | 0 | 23 |
| 50 | 11 | 11 | statement/sql/select | init_net_server_extension.cc:95 | 2335405793460000 | 2385985207283000 | 50579413823000 | 159000000 | select * from tmp_t0 where id=2 for update | ce70bef8d279a18cef7f9f297370a959f18de8b6bfce756fdf7beea6b178a3ef | SELECT * FROM `tmp_t0` WHERE `id` = ? FOR UPDATE | test | NULL | NULL | NULL | NULL | 1205 | HY000 | Lock wait timeout exceeded; try restarting transaction | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 10 | TRANSACTION | 0 | 39 |
| 51 | 216 | NULL | statement/sql/select | init_net_server_extension.cc:95 | 3122432593967000 | 3122433115342000 | 521375000 | 157000000 | select * from events_statements_current | d2cb1ecf648cf0ec173e79a74aa085d330c1cabcabbbd5785623ab5f69b0a811 | SELECT * FROM `events_statements_current` | performance_schema | NULL | NULL | NULL | NULL | 0 | NULL | NULL | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | NULL | NULL | 0 | 245 |
+-----------+----------+--------------+----------------------+---------------------------------+------------------+------------------+----------------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+-------------+---------------+-------------+-----------------------+-------------+-------------------+--------------------------------------------------------+--------+----------+---------------+-----------+---------------+-------------------------+--------------------+------------------+------------------------+--------------+--------------------+-------------+-------------------+------------+-----------+-----------+---------------+--------------------+------------------+--------------------+---------------------+--------------+
4 rows in set (0.00 sec)
mysql> select * from performance_schema.events_transactions_current;
+-----------+----------+--------------+-------------+-----------+-----------------+-----------+---------------+-----------+-----------+----------+--------------------+------------------+------------------+------------------+-------------+-----------------+------------+----------------------+---------------------------------+-----------------------------+-----------------------+------------------+--------------------+
| THREAD_ID | EVENT_ID | END_EVENT_ID | EVENT_NAME | STATE | TRX_ID | GTID | XID_FORMAT_ID | XID_GTRID | XID_BQUAL | XA_STATE | SOURCE | TIMER_START | TIMER_END | TIMER_WAIT | ACCESS_MODE | ISOLATION_LEVEL | AUTOCOMMIT | NUMBER_OF_SAVEPOINTS | NUMBER_OF_ROLLBACK_TO_SAVEPOINT | NUMBER_OF_RELEASE_SAVEPOINT | OBJECT_INSTANCE_BEGIN | NESTING_EVENT_ID | NESTING_EVENT_TYPE |
+-----------+----------+--------------+-------------+-----------+-----------------+-----------+---------------+-----------+-----------+----------+--------------------+------------------+------------------+------------------+-------------+-----------------+------------+----------------------+---------------------------------+-----------------------------+-----------------------+------------------+--------------------+
| 1 | 14 | 14 | transaction | COMMITTED | 421962444565096 | AUTOMATIC | NULL | NULL | NULL | NULL | handler.cc:1325 | 3495576342000 | 3495598130000 | 21788000 | READ WRITE | REPEATABLE READ | YES | 0 | 0 | 0 | NULL | NULL | NULL |
| 35 | 1 | 1 | transaction | COMMITTED | 421962444564224 | AUTOMATIC | NULL | NULL | NULL | NULL | handler.cc:1325 | 3075847630000 | 3075898553000 | 50923000 | READ WRITE | REPEATABLE READ | YES | 0 | 0 | 0 | NULL | NULL | NULL |
| 48 | 22 | NULL | transaction | ACTIVE | NULL | AUTOMATIC | NULL | NULL | NULL | NULL | transaction.cc:219 | 1498970095131000 | 2975384014689000 | 1476413919558000 | READ WRITE | REPEATABLE READ | NO | 0 | 0 | 0 | NULL | 21 | STATEMENT |
| 50 | 10 | NULL | transaction | ACTIVE | NULL | AUTOMATIC | NULL | NULL | NULL | NULL | transaction.cc:219 | 2323728069650000 | 2975384022560000 | 651655952910000 | READ WRITE | REPEATABLE READ | NO | 0 | 0 | 0 | NULL | 9 | STATEMENT |
| 51 | 207 | 207 | transaction | COMMITTED | 421962444566840 | AUTOMATIC | NULL | NULL | NULL | NULL | handler.cc:1325 | 2927067241633000 | 2927069226779000 | 1985146000 | READ WRITE | REPEATABLE READ | YES | 0 | 0 | 0 | NULL | 206 | STATEMENT |
+-----------+----------+--------------+-------------+-----------+-----------------+-----------+---------------+-----------+-----------+----------+--------------------+------------------+------------------+------------------+-------------+-----------------+------------+----------------------+---------------------------------+-----------------------------+-----------------------+------------------+--------------------+
5 rows in set (0.00 sec)
参考: