在遇到锁等待的情况下,在oracle中,非常容易定位到堵塞者和被堵塞者,在mysql中没有那么的直观,不过还是可以解决问题。
1.首先看information_schema.innodb_lock_waits锁等待的信息,可以看到堵塞和被堵塞者。
2.再看事务的信息,可以详细事务信息。
3.如果你想找到正在的原因,需要show engine innodb status,不过之前要创建一张表。如果你只要解决问题,跳过此步骤。
4.在mysql中kill堵塞者的trx_mysql_thread_id,这里是kill 33 。
下面来做个实验:
session1:
mysql> begin;
mysql> UPDATE test SET version_num = 11668 WHERE TYPE='contact' AND tenant_id='0001456d8d47ee01';
session2:
mysql> begin;
mysql> UPDATE test SET version_num = 11668 WHERE TYPE='contact' AND tenant_id='0001456d8d47ee01';
开始解决问题:
mysql> SELECT * FROM information_schema.innodb_lock_waits\G;
*************************** 1. row ***************************
requesting_trx_id: 455371209
requested_lock_id: 455371209:826:5:3
blocking_trx_id: 455371207
blocking_lock_id: 455371207:826:5:3
1 row in set (0.00 sec)
mysql> select * from information_schema.innodb_trx\G;
*************************** 1. row ***************************
trx_id: 455371209
trx_state: LOCK WAIT
trx_started: 2015-09-01 15:30:02
trx_requested_lock_id: 455371209:826:5:3
trx_wait_started: 2015-09-01 15:31:33
trx_weight: 2
trx_mysql_thread_id: 34
trx_query: UPDATE test SET version_num = 11668 WHERE TYPE='contact' AND tenant_id='0001456d8d47ee01'
trx_operation_state: starting index read
trx_tables_in_use: 1
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 360
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 2. row ***************************
trx_id: 455371207
trx_state: RUNNING
trx_started: 2015-09-01 15:17:41
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 6
trx_mysql_thread_id: 33
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 4
trx_lock_memory_bytes: 1184
trx_rows_locked: 5
trx_rows_modified: 2
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
2 rows in set (0.00 sec)
mysql> kill 33;
要想查到原因:
mysql> CREATE TABLE innodb_lock_monitor (a INT) ENGINE=INNODB;
mysql> show engine innodb status;
| Type | Name | Status| InnoDB | |
=====================================
2015-09-01 15:24:10 7f6eec2b7700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 2 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 38 srv_active, 0 srv_shutdown, 601378 srv_idle
srv_master_thread log flush and writes: 601416
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 34238
OS WAIT ARRAY INFO: signal count 33938
Mutex spin waits 27996, rounds 84220, OS waits 1699
RW-shared spins 106203, rounds 2242849, OS waits 29426
RW-excl spins 7, rounds 4219, OS waits 138
Spin rounds per wait: 3.01 mutex, 21.12 RW-shared, 602.71 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 455371214
Purge done for trx's n:o < 455371209 undo n:o < 0 state: running but idle
History list length 938
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 35, OS thread handle 0x7f6eec2b7700, query id 4434 10.10.32.105 eim init
show engine innodb status
---TRANSACTION 0, not started
MySQL thread id 32, OS thread handle 0x7f6eec5c1700, query id 4396 10.10.32.105 eim cleaning up
---TRANSACTION 0, not started
MySQL thread id 29, OS thread handle 0x7f6eec37a700, query id 4431 10.10.32.105 eim cleaning up
---TRANSACTION 455371196, not started
MySQL thread id 28, OS thread handle 0x7f6eec339700, query id 4430 10.10.32.105 eim cleaning up
---TRANSACTION 455371209, ACTIVE 386 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1
MySQL thread id 34, OS thread handle 0x7f6eec2f8700, query id 4433 10.10.32.105 eim updating
UPDATE test SET version_num = 11668 WHERE TYPE='contact' AND tenant_id='0001456d8d47ee01'
------- TRX HAS BEEN WAITING 4 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371209 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 7; hex 636f6e74616374; asc contact;;
2: len 16; hex 30303031343564363066373332363031; asc 000145d60f732601;;
------------------
TABLE LOCK table `eim`.`test` trx id 455371209 lock mode IX
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371209 lock_mode X
Record lock, heap no 8 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 6; hex 77625f6d7367; asc wb_msg;;
2: len 16; hex 30303031346139626635313537633031; asc 00014a9bf5157c01;;
RECORD LOCKS space id 826 page no 3 n bits 80 index `PRIMARY` of table `eim`.`test` trx id 455371209 lock_mode X locks rec but not gap
Record lock, heap no 6 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 16; hex 30303031346139626635313537633031; asc 00014a9bf5157c01;;
1: len 6; hex 00001b2469c9; asc $i ;;
2: len 7; hex 6e000006d030a0; asc n 0 ;;
3: len 6; hex 77625f6d7367; asc wb_msg;;
4: len 4; hex 80000002; asc ;;
5: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371209 lock_mode X locks gap be
fore rec
Record lock, heap no 9 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 11; hex 77625f6d73675f74797065; asc wb_msg_type;;
2: len 16; hex 30303031346139626635313561363032; asc 00014a9bf515a602;;
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371209 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 7; hex 636f6e74616374; asc contact;;
2: len 16; hex 30303031343564363066373332363031; asc 000145d60f732601;;
---TRANSACTION 455371207, ACTIVE 389 sec
4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2
MySQL thread id 33, OS thread handle 0x7f6eec276700, query id 4420 10.10.32.105 eim cleaning up
TABLE LOCK table `eim`.`test` trx id 455371207 lock mode IX
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371207 lock_mode X
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 7; hex 636f6e74616374; asc contact;;
2: len 16; hex 30303031343564363066373332363031; asc 000145d60f732601;;
Record lock, heap no 4 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 7; hex 636f6e74616374; asc contact;;
2: len 16; hex 30303031343935323333343364313035; asc 000149523343d105;;
RECORD LOCKS space id 826 page no 3 n bits 80 index `PRIMARY` of table `eim`.`test` trx id 455371207 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 16; hex 30303031343564363066373332363031; asc 000145d60f732601;;
1: len 6; hex 00001b2469c7; asc $i ;;
2: len 7; hex 6d000002190ce4; asc m ;;
3: len 7; hex 636f6e74616374; asc contact;;
4: len 4; hex 80002d94; asc - ;;
5: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
Record lock, heap no 4 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 16; hex 30303031343935323333343364313035; asc 000149523343d105;;
1: len 6; hex 00001b2469c7; asc $i ;;
2: len 7; hex 6d000002190d0c; asc m ;;
3: len 7; hex 636f6e74616374; asc contact;;
4: len 4; hex 80002d94; asc - ;;
5: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
RECORD LOCKS space id 826 page no 5 n bits 80 index `ind_pvr_tenant_id_TYPE` of table `eim`.`test` trx id 455371207 lock_mode X locks gap be
fore rec
Record lock, heap no 5 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 16; hex 30303031343536643864343765653031; asc 0001456d8d47ee01;;
1: len 5; hex 67726f7570; asc group;;
2: len 16; hex 30303031343935323432656261383562; asc 0001495242eba85b;;
--------
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
13307281 OS file reads, 158393 OS file writes, 5894 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 3077, seg size 3079, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 276671, node heap has 383 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 22518654725
Log flushed up to 22518654725
Pages flushed up to 22518654725
Last checkpoint at 22518654725
0 pending log writes, 0 pending chkp writes
2161 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 137363456; in additional pool allocated 0
Dictionary memory allocated 1490194
Buffer pool size 8191
Free buffers 1023
Database pages 6785
Old database pages 2485
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 620, not young 40511902
0.00 youngs/s, 0.00 non-youngs/s
Pages read 13288688, created 131964, written 132892
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: 6785, unzip_LRU len: 0
I/O sum[28]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Main thread process no. 1266, id 140114381858560, state: sleeping
Number of rows inserted 18, updated 24, deleted 0, read 50839215
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================