Mysql error "Lock wait timeout exceeded; try restarting transaction" during run LargeHadronMigrator add_column

StackOverflow https://stackoverflow.com/questions/22931722

  •  29-06-2023
  •  | 
  •  

Question

I tried to solve the problem of adding a column on a large MySQL table without locking, and on my dev environment migration it works. But when I launched it on production, then application raised MySQL errors:

Mysql2::Error: Lock wait timeout exceeded; 
try restarting transaction: UPDATE competition_clients...

Here is my migration:

require 'lhm'

class AddPersonToCompetitionClients < ActiveRecord::Migration

  def up
    Lhm.change_table(:competition_clients, atomic_switch: false) do |m|
      m.add_column(:person_id, 'INT(12)')
      m.add_column(:active, 'TINYINT(1)')
      m.add_index([:person_id])
      m.add_index([:active])
    end
  end

  def down
    Lhm.change_table(:competition_clients, atomic_switch: false) do |m|
      m.remove_index([:person_id])
      m.remove_index([:active])
      m.remove_column(:person_id)
      m.remove_column(:active)
    end
  end

end

Have anyone same problem or have anyone a solution how to run migration without raising exception from main app?

Was it helpful?

Solution

One solution I've used, if you have an uber query or series of queries, is to extend the wait timeout.

If you can't currently restart the server or MySQL, enter the following in the MySQL terminal:

mysql> set GLOBAL wait_timeout=28800;

This setting will go away when the current MySQL instance is terminated.

To set the wait_timeout parameter permanently, edit your MySQL configuration file:

~$ vim /etc/my.conf

Insert the following line:

wait_timeout = 28800

Now restart MySQL:

sudo /etc/init.d/mysql restart

Or on Redhat:

sudo service mysqld restart

Notes

wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it. This timeout applies only to TCP/IP and Unix socket file connections, not to connections made using named pipes, or shared memory.

interactive_timeout
The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect().

Can be set like:

interactive_timeout = 28800

my.cnf
Possible locations of my.cnf:

  • /etc/my.cnf
  • /etc/mysql/my.cnf
  • $MYSQL_HOME/my.cnf
  • [datadir]/my.cnf
  • ~/.my.cnf

source: http://moorberry.net/blog/mysql-lock-wait-timeout-exceeded/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top