1. 程式人生 > >ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction解決

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

mysql lock wait

一、問題描述:

同事反饋線上一個表有其中一條數據無法刪除,其他都正常,我拿到刪數據的sql,嘗試執行,報錯如下:

mysql> delete from facebook_posts where id = 7048962;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

二、問題處理

從報錯信息看,應該是關於這條數據有事物未提交,鎖等待超時了,下面我們就開始驗證並解決問題

1、在sql執行期間,通過information_schema.innodb_trx表找到這個sql的事物ID(5316933097

mysql> select trx_id,trx_started,trx_requested_lock_id,trx_mysql_thread_id,trx_query from information_schema.innodb_trx  where trx_query=‘delete from facebook_posts where id = 7048962‘;
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
| trx_id     | trx_started         | trx_requested_lock_id  | trx_mysql_thread_id | trx_query                                     |
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
| 5316933097 | 2017-08-15 07:31:57 | 5316933097:923:24693:6 |          1798850878 | delete from facebook_posts where id = 7048962 |
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
1 row in set (0.00 sec)

關於innodb_trx表字段含義的解釋:

mysql> desc information_schema.innodb_trx;       
+----------------------------+---------------------+------+-----+---------------------+-------+
| Field                      | Type                | Null | Key | Default             | Extra |
+----------------------------+---------------------+------+-----+---------------------+-------+
| trx_id                     | varchar(18)         | NO   |     |                     |       |#事務ID
| trx_state                  | varchar(13)         | NO   |     |                     |       |#事物狀態
| trx_started                | datetime            | NO   |     | 0000-00-00 00:00:00 |       |#事物開始時間
| trx_requested_lock_id      | varchar(81)         | YES  |     | NULL                |       |#事物請求鎖ID
| trx_wait_started           | datetime            | YES  |     | NULL                |       |#事物開始等待時間
| trx_weight                 | bigint(21) unsigned | NO   |     | 0                   |       |#
| trx_mysql_thread_id        | bigint(21) unsigned | NO   |     | 0                   |       |#事物線程ID,即show processlist看到ID
| trx_query                  | varchar(1024)       | YES  |     | NULL                |       |#具體SQL
| trx_operation_state        | varchar(64)         | YES  |     | NULL                |       |#事物當前操作狀態
| trx_tables_in_use          | bigint(21) unsigned | NO   |     | 0                   |       |#事物中有多少個表被使用
| trx_tables_locked          | bigint(21) unsigned | NO   |     | 0                   |       |#使用擁有多少個鎖
| trx_lock_structs           | bigint(21) unsigned | NO   |     | 0                   |       |#
| trx_lock_memory_bytes      | bigint(21) unsigned | NO   |     | 0                   |       |#事物鎖住的內存大小
| trx_rows_locked            | bigint(21) unsigned | NO   |     | 0                   |       |#事物鎖住的行數
| trx_rows_modified          | bigint(21) unsigned | NO   |     | 0                   |       |#使用修改的行數
| trx_concurrency_tickets    | bigint(21) unsigned | NO   |     | 0                   |       |#事物並發票數
| trx_isolation_level        | varchar(16)         | NO   |     |                     |       |#事物隔離級別
| trx_unique_checks          | int(1)              | NO   |     | 0                   |       |#是否唯一性檢查
| trx_foreign_key_checks     | int(1)              | NO   |     | 0                   |       |#是否外鍵檢查
| trx_last_foreign_key_error | varchar(256)        | YES  |     | NULL                |       |#最後的外鍵錯誤
| trx_adaptive_hash_latched  | int(1)              | NO   |     | 0                   |       |#
| trx_adaptive_hash_timeout  | bigint(21) unsigned | NO   |     | 0                   |       |#
| trx_is_read_only           | int(1)              | NO   |     | 0                   |       |#
| trx_autocommit_non_locking | int(1)              | NO   |     | 0                   |       |#
+----------------------------+---------------------+------+-----+---------------------+-------+
24 rows in set (0.00 sec)

2、通過上面步驟1找到的事物ID,找到占有鎖的事物ID(5316888834

mysql> select * from information_schema.innodb_lock_waits  where requesting_trx_id=5316933097;
+-------------------+------------------------+-----------------+------------------------+
| requesting_trx_id | requested_lock_id      | blocking_trx_id | blocking_lock_id       |
+-------------------+------------------------+-----------------+------------------------+
| 5316933097        | 5316933097:923:24693:6 | 5316888834      | 5316888834:923:24693:6 |
+-------------------+------------------------+-----------------+------------------------+
1 row in set (0.00 sec)

關於innodb_lock_waits 表的字段含義的解釋:

mysql> desc information_schema.innodb_lock_waits;
+-------------------+-------------+------+-----+---------+-------+
| Field             | Type        | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| requesting_trx_id | varchar(18) | NO   |     |         |       |#請求鎖的事物ID
| requested_lock_id | varchar(81) | NO   |     |         |       |#請求鎖的鎖ID
| blocking_trx_id   | varchar(18) | NO   |     |         |       |#當前擁有鎖的事物ID
| blocking_lock_id  | varchar(81) | NO   |     |         |       |#當前擁有鎖的鎖ID
+-------------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3、通過步驟2找到的占有鎖的事物ID,找到占有鎖的事物線程ID(1790259884

mysql> select * from  information_schema.innodb_trx where trx_id=5316888834  \G
*************************** 1. row ***************************
                    trx_id: 5316888834
                 trx_state: RUNNING
               trx_started: 2017-08-15 06:00:21
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 6
       trx_mysql_thread_id: 1790259884
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         trx_tables_locked: 0
          trx_lock_structs: 6
     trx_lock_memory_bytes: 1184
           trx_rows_locked: 10
         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
1 row in set (0.00 sec)

4、通過步驟3找的事物ID,可以查看下這個事物發起的賬號和主機信息,提供給開發人員查找異常的真正原因,並kill這個事物ID,這條數據就可以正常刪除了

#查看下這個事物發起的賬號和主機信息
mysql> select * from  information_schema.processlist where ID=1790259884;
+------------+----------+---------------------+--------+---------+------+-------+------+
| ID         | USER     | HOST                | DB     | COMMAND | TIME | STATE | INFO |
+------------+----------+---------------------+--------+---------+------+-------+------+
| 1790259884 | spider_w | 172.31.11.143:46120 | db_mta | Sleep   | 1319 |       | NULL |
+------------+----------+---------------------+--------+---------+------+-------+------+
1 row in set (0.01 sec)
#kill 這個未提交的事物線程ID
mysql> CALL mysql.rds_kill(1790259884);
Query OK, 0 rows affected (0.00 sec)
#刪除數據
mysql> delete from facebook_posts where id = 7041232;
Query OK, 1 row affected (0.02 sec)


本文出自 “屌絲運維男” 博客,請務必保留此出處http://navyaijm.blog.51cto.com/4647068/1956551

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