A WooCommerce order hangs for almost a minute and then fails. A cron import dies halfway through. Your PHP error log fills with SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction. MySQL isn't down and the table isn't broken. One query is stuck waiting for a row that another session refuses to let go of. Here's how to find that other session, clear the jam, and stop it coming back.
Symptom: What You'll See
- Pages or admin actions hang for roughly 50 seconds, then fail. 50 seconds is MySQL's default lock wait.
- The error shows up as
ERROR 1205 (HY000): Lock wait timeout exceededin phpMyAdmin, WP-CLI, or your app logs. - It hits writes (
UPDATE,DELETE,INSERT) on busy tables likewp_options,wp_postmeta,wp_wc_ordersor session tables. - Plain page views usually still load fine, because ordinary
SELECTs don't wait on row locks in InnoDB.
Cause: Who's Holding the Lock?
InnoDB locks rows while a transaction changes them, and keeps those locks until the transaction commits or rolls back. Error 1205 means your query waited longer than innodb_lock_wait_timeout for one of those locks and gave up.
It's often mixed up with a deadlock, but it's a different problem:
| Error 1205: lock wait timeout | Error 1213: deadlock | |
|---|---|---|
| What happened | One session waited too long behind another | Two sessions blocked each other in a circle |
| How fast it fails | After the timeout (default 50s) | Immediately; InnoDB picks a victim |
| Usual culprit | A long or forgotten open transaction | Two transactions locking rows in opposite order |
| What gets rolled back | Only the failed statement, by default | The whole victim transaction |
The usual suspects behind a 1205:
- A long-running write. A bulk
UPDATEorDELETEon millions of rows, a big import, or a plugin cleaning up postmeta. - An idle open transaction. A script ran
START TRANSACTION, changed a row, and then got stuck on an API call, a sleep, or a crash without committing. The connection sits idle while it holds the lock. - Missing indexes. An
UPDATE ... WHEREon an unindexed column scans and locks far more rows than it changes. - Overlapping cron jobs. The same import or sync starts again before the previous run has finished, and both fight over the same rows.
- Backups taking locks.
mysqldumpwithout--single-transactionlocks tables for the whole dump.
Fix: Find and Clear the Blocker
Step 1: See who is waiting on whom
Connect as root (on a VPS: mysql -u root -p; in WHM: the root MySQL shell) while the problem is happening. On MySQL 8.0 and newer, the sys schema gives you the answer directly:
SELECT waiting_pid, waiting_query,
blocking_pid, blocking_query,
wait_age, sql_kill_blocking_connection
FROM sys.innodb_lock_waits\G
On MariaDB and MySQL 5.7, use the InnoDB tables instead:
SELECT r.trx_mysql_thread_id AS waiting_pid,
r.trx_query AS waiting_query,
b.trx_mysql_thread_id AS blocking_pid,
b.trx_query AS blocking_query,
b.trx_started AS blocker_started
FROM information_schema.innodb_lock_waits w
JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id
JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id;
If blocking_query is NULL, the blocker is an idle connection with an open transaction. Its last statement finished, but it never committed.
Step 2: Look for old open transactions
SELECT trx_mysql_thread_id, trx_state, trx_started,
TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_sec,
trx_rows_locked, trx_query
FROM information_schema.innodb_trx
ORDER BY trx_started;
Anything open for minutes with a high trx_rows_locked is your prime suspect. Cross-check its user and host with SHOW FULL PROCESSLIST; to see which site or script owns it.
Step 3: Kill the blocker (carefully)
KILL 12345; -- the blocking_pid from step 1
MySQL rolls back that transaction, the locks are released, and the waiting queries go through. The rollback of a huge transaction can take a while, so don't restart MySQL halfway through it. That only makes recovery longer.
Before you kill anything, check what it is. A long backup or migration you started on purpose is better left to finish.
Step 4: Retry the failed work
By default, MySQL rolls back only the statement that timed out, not the whole transaction. Code that just carries on can leave you with half-applied changes. Either have the app roll back and retry the whole transaction, or set innodb_rollback_on_timeout=ON so a timeout rolls back everything. That one needs a MySQL restart.
Prevention: Keep Transactions Short
- Commit quickly. Never hold a transaction open across an HTTP call, an email send, or user input. Do the slow work first, then open the transaction, write, and commit.
- Batch big writes. Delete or update in chunks instead of one giant statement:
With WP-CLI, run cleanups in a loop instead of one huge query.DELETE FROM wp_postmeta WHERE meta_key = '_old_cache' LIMIT 5000; -- repeat until 0 rows affected - Index your WHERE columns. Run
EXPLAIN UPDATE .... If it showstype: ALL, the statement is scanning and locking the whole table. - Stop cron overlaps. Wrap long jobs in
flockso a second copy can't start:flock -n /tmp/import.lock php /home/user/import.php - Dump without locking. Use
mysqldump --single-transaction --quickfor InnoDB databases. - Tune the timeout only as a last step. Raising it hides the problem, and lowering it makes failures faster. For a web app, failing faster is usually better. Set it per session for a known long job instead of globally:
SET SESSION innodb_lock_wait_timeout = 120;
To spot problems early, check SHOW ENGINE INNODB STATUS\G during busy hours and watch the TRANSACTIONS section for anything marked ACTIVE for hundreds of seconds.
Frequently Asked Questions
Should I just increase innodb_lock_wait_timeout?
Rarely. If a transaction is stuck for 50 seconds, it'll usually be stuck for 500 too, and your visitors will wait longer before seeing the error. Find and fix the blocker first. Raise the timeout only for a specific, known long job, and do it per session.
Is it safe to KILL a MySQL connection?
Generally yes. MySQL rolls back whatever that transaction changed, so data stays consistent. The risks are killing a legitimate backup or migration, and waiting for a long rollback. Check the query and the user in the processlist before you kill it.
Can I fix this on shared cPanel hosting?
You can't kill other users' queries or change server settings, but you can find long-running queries from your own account in phpMyAdmin's Status → Processes tab, stop overlapping cron jobs, and batch big cleanups. If the blocker isn't yours, open a ticket with the exact time of the errors.
Why does this happen more with WooCommerce?
Checkout, stock updates, Action Scheduler jobs and webhooks all write to the same order and meta rows at the same time. A slow payment-gateway call inside a transaction or a big backlog of scheduled actions can easily hold locks long enough to trigger 1205. Clearing out old completed actions and making sure WP-Cron runs from a real system cron helps a lot.
