MySQL:資料庫事務鎖處理
阿新 • • 發佈:2019-01-02
執行update或insert或delete語句超時報錯:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
-- 錯誤1205(HY000):超出鎖定等待超時; 嘗試重新啟動事務
可以重啟資料庫服務來解決,也可以如下方式解決:
--檢視先當前庫執行緒情況:
show full processlist;
--沒有看到正在執行的慢SQL記錄執行緒
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+
| 2 | root | localhost | estate | Query | 0 | NULL | show full processlist | 0.000 |
| 308 | root | 58.59.25.109:33457 | estate | Sleep | 894 | | NULL | 0.000 |
| 310 | root | 58.59.25.109:34619 | estate | Sleep | 928 | | NULL | 0.000 |
| 312 | root | 58.59.25.109:38101 | estate | Sleep | 0 | | NULL | 0.000 |
+-----+------+--------------------+--------+---------+------+-------+-----------------------+----------+
--再去檢視innodb的事務表INNODB_TRX,看下里面是否有正在鎖定的事務執行緒
--看看ID是否在show full processlist裡面的sleep執行緒中.
--如果是,就證明這個sleep的執行緒事務一直沒有commit或者rollback而是卡住了,我們需要手動kill掉。
SELECT * FROM information_schema.INNODB_TRX\G;
*************************** 1. row ***************************
trx_id: C15
trx_state: RUNNING
trx_started: 2018-03-29 13:42:44
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 4
trx_mysql_thread_id: 310 --發現這個310事務卡住,(這裡的資料是偽造的)
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 1
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
--手動kill掉:
kill 310;
問題解決。