Fix MySQL ERROR 1205 Lock wait timeout exceeded: find the blocker
MySQL ERROR 1205 Lock wait timeout exceeded means another transaction held the row lock past the wait limit. Find it in sys.innodb_lock_waits and fix it.
ERROR 1205 (HY000) at line 1: Lock wait timeout exceeded; try restarting transactionFrom mysql -e or a script you get at line 1 as above; the official docs show it as ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction, and app drivers wrap the same text in their own exceptions. Another InnoDB transaction holds a lock on the row you want to change, and your statement gave up after innodb_lock_wait_timeout (50 seconds by default). The session that got the error is the victim; the cause is the other connection that took the lock and never finished. Finding it with sys.innodb_lock_waits is the first job.
[1] Know what is left in your transaction after the error. Per the official docs a timeout rolls back only the current statement, not the whole transaction (innodb_lock_wait_timeout). Below, the UPDATE after an INSERT timed out:
$ cat tx.sqlSET SESSION innodb_lock_wait_timeout = 3;START TRANSACTION;INSERT INTO audit (note) VALUES ('order 42');UPDATE inventory SET qty = qty - 1 WHERE id = 1;SELECT COUNT(*) AS audit_rows_in_trx FROM audit;ROLLBACK;SELECT COUNT(*) AS audit_rows_after_rollback FROM audit;$ mysql -uroot -p'<password>' shop --force < tx.sqlmysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1205 (HY000) at line 4: Lock wait timeout exceeded; try restarting transactionaudit_rows_in_trx1audit_rows_after_rollback0The earlier INSERT is still in the transaction. If the app catches 1205 and then commits, it saves half the work. On 1205, roll back and run the whole transaction again. To have the server roll back the entire transaction automatically, start it with --innodb-rollback-on-timeout (the test server had 0).
[2] While a session is waiting, find the connection holding the lock. From another session, query sys.innodb_lock_waits:
$ mysql -uroot -p'<password>' shop -e "SELECT waiting_pid, waiting_query, blocking_pid, blocking_query, wait_age, sql_kill_blocking_connection FROM sys.innodb_lock_waits\G"mysql: [Warning] Using a password on the command line interface can be insecure.*************************** 1. row *************************** waiting_pid: 12 waiting_query: UPDATE inventory SET qty = qty - 1 WHERE id = 1 blocking_pid: 11 blocking_query: NULL wait_age: 00:00:04sql_kill_blocking_connection: KILL 11A NULL blocking_query is the common picture. The docs say the column is NULL when the blocking session has gone idle (sys.innodb_lock_waits): its statement finished, but it never committed.
[3] See how long that connection has held its transaction. INNODB_TRX lists open transactions; processlist shows the connection state.
$ mysql -uroot -p'<password>' shop -e "SELECT trx_mysql_thread_id, trx_state, trx_started, TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_s, trx_rows_locked, trx_query FROM information_schema.INNODB_TRX ORDER BY trx_started"mysql: [Warning] Using a password on the command line interface can be insecure.trx_mysql_thread_id trx_state trx_started age_s trx_rows_locked trx_query11 RUNNING 2026-10-27 08:14:09 7 1 NULL12 LOCK WAIT 2026-10-27 08:14:12 4 1 UPDATE inventory SET qty = qty - 1 WHERE id = 1$ mysql -uroot -p'<password>' shop -e "SELECT id, user, command, time, state, info FROM information_schema.processlist WHERE id = 11"mysql: [Warning] Using a password on the command line interface can be insecure.id user command time state info11 root Sleep 7 NULLThe transaction is RUNNING while the connection is in Sleep. On a real server, user and host tell you which app owns it; an age_s far beyond a normal request means that app is not closing its transactions.
[4] Pull what the idle connection ran from performance_schema. This is how you find the code path:
$ mysql -uroot -p'<password>' shop -e "SELECT h.SQL_TEXT FROM performance_schema.events_statements_history h JOIN performance_schema.threads t ON t.THREAD_ID = h.THREAD_ID WHERE t.PROCESSLIST_ID = 11 ORDER BY h.EVENT_ID"mysql: [Warning] Using a password on the command line interface can be insecure.SQL_TEXTselect @@version_comment limit 1select $$START TRANSACTIONUPDATE inventory SET qty = qty - 1 WHERE id = 1events_statements_history keeps a fixed number of statements per thread (performance_schema_events_statements_history_size), so in a long transaction the early statements may already be gone.
[5] In an emergency, kill the blocking connection. Its transaction is rolled back — here connection 11's UPDATE was undone, so qty dropped from 10 only once. Check with [4] what will be lost before you kill.
$ mysql -uroot -p'<password>' shop -e "KILL 11"mysql: [Warning] Using a password on the command line interface can be insecure.$ mysql -uroot -p'<password>' shop -e "SELECT COUNT(*) AS open_trx FROM information_schema.INNODB_TRX; UPDATE inventory SET qty = qty - 1 WHERE id = 1; SELECT id, qty FROM inventory"mysql: [Warning] Using a password on the command line interface can be insecure.open_trx0id qty1 92 4The 4 on id = 2 comes from an earlier update to that row: locks are per row, so other rows in the same table changed without waiting. The killed app gets ERROR 2013 (HY000) … Lost connection to MySQL server during query on its next statement.
[6] Fix the cause. Killing is first aid; usually the app keeps transactions open too long.
| What you saw | Where to fix |
|---|---|
blocking_query NULL, connection in Sleep | A missing COMMIT/ROLLBACK on an error path, or an idle connection with autocommit off |
blocking_query is a long-running statement | How many rows that statement locks (an UPDATE … WHERE with no index, etc.) |
| Only during batch jobs | Split the batch into small transactions |
[7] The wait can be set per session. In the test, innodb_lock_wait_timeout = 5 failed after 5 seconds and 20 after 20.
$ time mysql -uroot -p'<password>' shop -e 'SET SESSION innodb_lock_wait_timeout = 5; UPDATE inventory SET qty = qty - 1 WHERE id = 1;'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1205 (HY000) at line 1: Lock wait timeout exceeded; try restarting transaction real 0m5.011suser 0m0.006ssys 0m0.004sA larger value only delays the error, and waiting connections can pile up until you hit the connection limit instead — that is MySQL ERROR 1040 Too many connections. Keep it short for user requests and longer only for batch sessions that can afford to wait.
Analyze your MySQL slow log log in the browser ↗ — nothing is uploaded.