背景
测试同事通过更新某个表的数据进行功能测试,突然说锁表了,也不知道原因为何,一直用的Navicat,然后用show OPEN TABLES where In_use > 0;
命令也查询不到具体的进程ID
常规解决办法
- 查询是否锁表
show OPEN TABLES where In_use > 0;
- 查询进程
show processlist;
查询到相对应的进程,然后
kill id;
通过查看事务处理
- 查看正在锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
- 查看等待锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
通过Innodb处理
在MySQL中有一个方法可以探窥到锁情况,它会打印出所有innodb的信息:
SHOW ENGINE INNODB STATUS;
如下,是通过语句获取到的信息(其中的IP全部用10.10.10.10已替换),后面会简要的进行分析这段文本
=====================================
2019-12-18 11:44:35 7f4f743f4700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 5 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 60826 srv_active, 0 srv_shutdown, 30696 srv_idle
srv_master_thread log flush and writes: 91522
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 9916
OS WAIT ARRAY INFO: signal count 9842
Mutex spin waits 2763, rounds 43928, OS waits 1328
RW-shared spins 8532, rounds 255229, OS waits 8469
RW-excl spins 82, rounds 3658, OS waits 94
Spin rounds per wait: 15.90 mutex, 29.91 RW-shared, 44.61 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 188109012
Purge done for trx's n:o < 188101227 undo n:o < 0 state: running but idle
History list length 684
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 2326, OS thread handle 0x7f4f743f4700, query id 915743 10.10.10.10 root init
SHOW ENGINE INNODB STATUS
---TRANSACTION 188109010, not started
MySQL thread id 2324, OS thread handle 0x7f4f74476700, query id 915731 10.10.10.10 yt_dev cleaning up
---TRANSACTION 0, not started
MySQL thread id 2321, OS thread handle 0x7f4f3c0ae700, query id 914695 10.10.10.10 root cleaning up
---TRANSACTION 0, not started
MySQL thread id 2319, OS thread handle 0x7f4f4014b700, query id 915351 10.10.10.10 root cleaning up
---TRANSACTION 188108991, not started
MySQL thread id 2277, OS thread handle 0x7f4f360f8700, query id 915671 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 0, not started
MySQL thread id 2314, OS thread handle 0x7f4f35be4700, query id 912387 10.10.10.10 root cleaning up
---TRANSACTION 188108516, not started
MySQL thread id 2275, OS thread handle 0x7f4f745af700, query id 914221 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188108974, not started
MySQL thread id 2286, OS thread handle 0x7f4f40312700, query id 915594 10.10.10.10 yt_dev cleaning up
---TRANSACTION 0, not started
MySQL thread id 2287, OS thread handle 0x7f4f3c171700, query id 908400 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188108951, not started
MySQL thread id 2280, OS thread handle 0x7f4f74372700, query id 915501 10.10.10.10 yt_dev cleaning up
---TRANSACTION 188108970, not started
MySQL thread id 2276, OS thread handle 0x7f4f740e8700, query id 915576 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188108004, not started
MySQL thread id 2261, OS thread handle 0x7f4f741ec700, query id 915637 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188109011, not started
MySQL thread id 2259, OS thread handle 0x7f4f36139700, query id 915739 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188107491, not started
MySQL thread id 2251, OS thread handle 0x7f4f36076700, query id 908114 10.10.10.10 yt_dev cleaning up
---TRANSACTION 188105855, not started
MySQL thread id 2237, OS thread handle 0x7f4f35e6e700, query id 915667 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188106734, not started
MySQL thread id 2191, OS thread handle 0x7f4f3c0ef700, query id 904938 10.10.10.10 yttest_opr cleaning up
---TRANSACTION 188108971, not started
MySQL thread id 2220, OS thread handle 0x7f4f4018c700, query id 915685 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188109008, not started
MySQL thread id 2180, OS thread handle 0x7f4f3617a700, query id 915721 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188105318, not started
MySQL thread id 2132, OS thread handle 0x7f4f4020e700, query id 915614 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 0, not started
MySQL thread id 2090, OS thread handle 0x7f4f74066700, query id 849932 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188108998, not started
MySQL thread id 1950, OS thread handle 0x7f4f402d1700, query id 915711 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188108968, not started
MySQL thread id 1747, OS thread handle 0x7f4f40088700, query id 915566 10.10.10.10 yttest_opr cleaning up
---TRANSACTION 188043721, not started
MySQL thread id 1376, OS thread handle 0x7f4f400c9700, query id 915665 10.10.10.10 ytdev_phsp_data cleaning up
---TRANSACTION 188106729, not started
MySQL thread id 21, OS thread handle 0x7f4f7416a700, query id 904903 10.10.10.10 yttest_opr cleaning up
---TRANSACTION 188108889, ACTIVE 39 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 2315, OS thread handle 0x7f4f74539700, query id 915249 10.10.10.10 root updating
UPDATE `ytphsp_busi_v2`.`app_evaluation` SET `is_visible`='1' WHERE `id`='26'
------- TRX HAS BEEN WAITING 39 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 5023 page no 3 n bits 104 index `PRIMARY` of table `ytphsp_busi_v2`.`app_evaluation` trx id 188108889 lock_mode X locks rec but not gap waiting
Record lock, heap no 31 PHYSICAL RECORD: n_fields 12; compact format; info bits 0
0: len 8; hex 800000000000001a; asc ;;
1: len 6; hex 00000b363265; asc 62e;;
2: len 7; hex 6100000228141d; asc a ( ;;
3: len 8; hex 800000000bebc201; asc ;;
4: len 4; hex 8000000f; asc ;;
5: len 4; hex 80000000; asc ;;
6: SQL NULL;
7: len 4; hex 80000000; asc ;;
8: SQL NULL;
9: SQL NULL;
10: SQL NULL;
11: SQL NULL;
------------------
---TRANSACTION 188101221, ACTIVE 3405 sec
2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 2
MySQL thread id 2065, OS thread handle 0x7f4f35ca7700, query id 881823 10.10.10.10 ytdev_phsp_data cleaning up
Trx read view will not see trx with id >= 188101222, sees < 188101222
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests (write thread)
I/O thread 8 state: waiting for completed aio requests (write thread)
I/O thread 9 state: waiting for completed aio requests (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
146414 OS file reads, 68947 OS file writes, 48073 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 1556, seg size 1558, 17 merges
merged operations:
insert 31, delete mark 15, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 138401, node heap has 187 buffer(s)
803.44 hash searches/s, 57.79 non-hash searches/s
---
LOG
---
Log sequence number 64441780891
Log flushed up to 64441780891
Pages flushed up to 64441780614
Last checkpoint at 64441780614
0 pending log writes, 0 pending chkp writes
21801 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 68681728; in additional pool allocated 0
Dictionary memory allocated 3026787
Buffer pool size 4096
Free buffers 1025
Database pages 2884
Old database pages 1044
Modified db pages 3
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 9822, not young 3172429
0.00 youngs/s, 0.00 non-youngs/s
Pages read 143394, created 910, written 38680
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 2884, unzip_LRU len: 0
I/O sum[30]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 1229, id 139978308536064, state: sleeping
Number of rows inserted 1171, updated 98547, deleted 974, read 21120891
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 863.03 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
这里面最需要注意的一段就是如下这段,里面有死锁的事务ID,进程ID,能够进一步查询
搜寻这段的关键词:
TRX HAS BEEN WAITING
RECORD LOCKS space id
TRANSACTION
MySQL thread id
------- TRX HAS BEEN WAITING 39 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 5023 page no 3 n bits 104 index `PRIMARY` of table `ytphsp_busi_v2`.`app_evaluation` trx id 188108889 lock_mode X locks rec but not gap waiting
Record lock, heap no 31 PHYSICAL RECORD: n_fields 12; compact format; info bits 0
0: len 8; hex 800000000000001a; asc ;;
1: len 6; hex 00000b363265; asc 62e;;
2: len 7; hex 6100000228141d; asc a ( ;;
3: len 8; hex 800000000bebc201; asc ;;
4: len 4; hex 8000000f; asc ;;
5: len 4; hex 80000000; asc ;;
6: SQL NULL;
7: len 4; hex 80000000; asc ;;
8: SQL NULL;
9: SQL NULL;
10: SQL NULL;
11: SQL NULL;
------------------
---TRANSACTION 188101221, ACTIVE 3405 sec
2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 2
MySQL thread id 2065, OS thread handle 0x7f4f35ca7700, query id 881823 10.10.10.10 ytdev_phsp_data cleaning up
Trx read view will not see trx with id >= 188101222, sees < 188101222
如上,可以找到事务ID=188101221,进程ID=2065
然后,kill 2065
杀掉进程即可