Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: "lock wait timeout exceeded"

Tags:

database

mysql

I am trying to delete several rows from a MySQL 5.0.45 database:

delete from bundle_inclusions;

The client works for a while and then returns the error:

Lock wait timeout exceeded; try restarting transaction

It's possible there is some uncommitted transaction out there that has a lock on this table, but I need this process to trump any such locks. How do I break the lock in MySQL?

like image 929
Adrian Dunston Avatar asked Sep 15 '08 16:09

Adrian Dunston


People also ask

How do I fix MySQL lock wait timeout exceeded?

In summary, if we face a “Lock Wait Timeout Exceeded” error in MySQL, we need to first understand the effects that such an error can have to our infrastructure, then track the offensive transaction and act on it either with shell scripts like track_lockwait.sh, or database management software like ClusterControl.

What Causes lock wait timeout exceeded?

The “Lock wait timeout exceeded; try restarting transaction” error will occur when a query cannot proceed because it is blocked by a rowlock. Typically, a deadlock happens when two or more transactions are writing to the same rows, but in a different order.

What is lock wait timeout?

Lock timeout detection is a database manager feature that prevents applications from waiting indefinitely for a lock to be released.

What is lock wait MySQL?

If a transaction is waiting for a lock, it is in a LOCK WAIT state. (The INFORMATION_SCHEMA INNODB_TRX table indicates transaction state values.) The Performance Schema data_locks table holds one or more rows for each LOCK WAIT transaction, indicating any lock requests that prevent its progress.

What is Lock_wait_timeout in MySQL?

This variable specifies the timeout in seconds for attempts to acquire metadata locks. The permissible values range from 1 to 31536000 (1 year).


2 Answers

Linux: In mysql configuration (/etc/my.cnf or /etc/mysql/my.cnf), insert / edit this line

innodb_lock_wait_timeout = 50

Increase the value sufficiently (it is in seconds), restart database, perform changes. Then revert the change and restart again.

like image 111
Lars Bohl Avatar answered Sep 18 '22 14:09

Lars Bohl


I agree with Erik; TRUNCATE TABLE is the way to go. However, if you can't use that for some reason (for example, if you don't really want to delete every row in the table), you can try the following options:

  • Delete the rows in smaller batches (e.g. DELETE FROM bundle_inclusions WHERE id BETWEEN ? and ?)
  • If it's a MyISAM table (actually, this may work with InnoDB too), try issuing a LOCK TABLE before the DELETE. This should guarantee that you have exclusive access.
  • If it's an InnoDB table, then after the timeout occurs, use SHOW INNODB STATUS. This should give you some insight into why the lock acquisition failed.
  • If you have the SUPER privilege you could try SHOW PROCESSLIST ALL to see what other connections (if any) are using the table, and then use KILL to get rid of the one(s) you're competing with.

I'm sure there are many other possibilities; I hope one of these help.

like image 28
Matt Solnit Avatar answered Sep 19 '22 14:09

Matt Solnit