ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

 問題描述:

研發忽然找我,遇到了mysql的 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction,具體報錯以下:html

org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158) Caused by: java.io 

.IOException: java.sql.SQLException: Lock wait timeout exceeded;
try restarting transaction at org.apache.sqoop.mapreduce.AsyncSqlRecordWriter.write(AsyncSqlRecordWriter.java:220)
at org.apache.sqoop.mapreduce.AsyncSqlRecordWriter.write(AsyncSqlRecordWriter.java:46) at
org.apache.hadoop.mapred.MapTask$NewDirectOutputCollector.write(MapTask.java:655) at

 產生這個問題的緣由是由於在mysql中產生了事務A,執行了修改的語句,好比: update t1 set aget=18 where id=1;此時事務並未進行提交,事務B開始運行,也一樣須要修改id爲1的用戶的年齡: update t1 set aget=20 where id=1; 那麼此時事務B會等待事務A結束釋放寫鎖才能執行成功,不然則會等待一段時間,產生報錯:java

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transactionnode

 

下面開始模擬鎖表的場景:mysql

1、打開一個終端,鏈接上mysql,關閉事務自動提交,執行一個update語句

 

查看鏈接的id:sql

 

2、打開另一個終端,用mysql鏈接上去,更新一步驟中的update的那條數據,

而後等待一會,會報ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction數據庫

 

3、再打開一個終端鏈接上去,查看鎖的狀態

   在mysql5.5以後,information_schema數據庫加了三個關於鎖的表apache

     innodb_trx ## 當前運行的全部事務bash

     innodb_locks ## 當前出現的鎖服務器

     innodb_lock_waits ## 鎖等待的對應關係dom

 

這個表對於排查由於事務未提交引發的鎖問題能夠說是舉足輕重。當咱們有事務長時間未提交致使鎖住數據庫,其餘程序拿不到鎖的時候,由於對這張表進行排查。

經過這個表咱們能夠查出鏈接的線程ID,可根據時間判斷懷疑一下

SELECT * FROM information_schema.INNODB_TRX\G

好比咱們獲取一條記錄的線程id, 便可拿着該線程id去information_schma.processlist中獲取他的具體狀況

咱們能夠經過以下的方法找出鏈接的服務器,進而判斷進程:

 

此處host能夠出現端口,而後再用lsof –p $port 來查看是哪一個程序,基本上就能夠確認了,是哪一個程序搞的鬼,接下來看下記錄鎖信息的表 innodb_locks

若是 咱們要排查的問題正鎖死咱們的某張表,那麼該表的數據表就會有所體現。同時和這個表使用的 還有information_schema.innodb_lock_waits

 

此時若是咱們要想恢復,能夠手動殺掉id9的鏈接,若是是死鎖的狀況下。

 

在第一個終端查看:

 

注意:若是咱們若是有權限,可使用能看到相對來講更爲複雜和詳細的信息

 4、其餘排查手段

#經過innodb status 提供的詳細的系統狀況來分析問題。

SHOW ENGINE INNODB STATUS\G 

若是我沒沒有使用show engine innodb status的權限,退而求其次咱們可使用另一種思路來找到是哪一個表持續被鎖,致使拿不到鎖的問題。

show open tables where in_use>0;

查看如今系統正在使用的表,而後使用: 

show full processlist;

查找正在query該表的任務,查看代碼是否有一直沒有提交事物,卻沒有commit的代碼,用這個思路來找問題出如今哪兒。

mysql> show engine innodb status\G 
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
2017-08-08 22:39:01 0x7fde3ce46700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 37 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 13 srv_active, 0 srv_shutdown, 1407 srv_idle
srv_master_thread log flush and writes: 1420
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 44
OS WAIT ARRAY INFO: signal count 44
RW-shared spins 0, rounds 56, OS waits 28
RW-excl spins 0, rounds 0, OS waits 0
RW-sx spins 0, rounds 0, OS waits 0
Spin rounds per wait: 56.00 RW-shared, 0.00 RW-excl, 0.00 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 1305
Purge done for trx's n:o < 1303 undo n:o < 0 state: running but idle
History list length 6
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 422074379878000, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 422074379876176, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 1303, ACTIVE 238 sec
2 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1
MySQL thread id 9, OS thread handle 140592481601280, query id 101 localhost root
Trx read view will not see trx with id >= 1303, sees < 1303
--------
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] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
274 OS file reads, 245 OS file writes, 165 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 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
Hash table size 1593743, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 2549841
Log flushed up to   2549841
Pages flushed up to 2549841
Last checkpoint at  2549832
0 pending log flushes, 0 pending chkp writes
78 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 6596591616
Dictionary memory allocated 342962
Buffer pool size   393192
Free buffers       392904
Database pages     288
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 242, created 46, written 111
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 288, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size   49146
Free buffers       49106
Database pages     40
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 40, created 0, written 27
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 40, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size   49152
Free buffers       49144
Database pages     8
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 8, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 8, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size   49146
Free buffers       49137
Database pages     9
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 9, created 0, written 0
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 9, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size   49152
Free buffers       49076
Database pages     76
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 76, created 0, written 6
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 76, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size   49146
Free buffers       49055
Database pages     91
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 85, created 6, written 17
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 91, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size   49152
Free buffers       49140
Database pages     12
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 11, created 1, written 3
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 12, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size   49146
Free buffers       49106
Database pages     40
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 5, created 35, written 43
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 40, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size   49152
Free buffers       49140
Database pages     12
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 8, created 4, written 15
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 12, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Process ID=17616, Main thread ID=140591056803584, state: sleeping
Number of rows inserted 6, updated 5, deleted 0, read 48
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

  

參考文檔:http://www.cnblogs.com/piperck/p/6212524.html