我试图解决在没有锁定的情况下在大型 MySQL 表上添加列的问题,并且在我的开发环境迁移中它起作用了。但是当我在生产环境中启动它时,应用程序引发了 MySQL 错误:
Mysql2::Error: Lock wait timeout exceeded;
try restarting transaction: UPDATE competition_clients...
这是我的迁移:
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
有没有人遇到同样的问题或者有没有人知道如何在不从主应用程序引发异常的情况下运行迁移?
最佳答案
如果您有 super 查询或一系列查询,我使用过的一个解决方案是延长等待超时时间。
如果您当前无法重新启动服务器或 MySQL,请在 MySQL 终端中输入以下内容:
mysql> set GLOBAL wait_timeout=28800;
当当前 MySQL 实例终止时,此设置将消失。
要永久设置 wait_timeout 参数,请编辑您的 MySQL 配置文件:
~$ vim /etc/my.conf
插入以下行:
wait_timeout = 28800
现在重启MySQL:
sudo /etc/init.d/mysql restart
或者在 Redhat 上:
sudo service mysqld restart
注意事项
等待超时
服务器在关闭非交互式连接之前等待事件的秒数。此超时仅适用于 TCP/IP 和 Unix 套接字文件连接,不适用于使用命名管道或共享内存建立的连接。
interactive_timeout
服务器在关闭交互式连接之前等待其事件的秒数。交互式客户端定义为使用 mysql_real_connect() 的 CLIENT_INTERACTIVE 选项的客户端。
可以这样设置:
interactive_timeout = 28800
我的.cnf
my.cnf 的可能位置:
- /etc/my.cnf
- /etc/mysql/my.cnf
- $MYSQL_HOME/my.cnf
- [数据目录]/my.cnf
- ~/.my.cnf
来源:http://moorberry.net/blog/mysql-lock-wait-timeout-exceeded/
关于ruby-on-rails - 运行 LargeHadronMigrator add_column 期间出现 Mysql 错误 "Lock wait timeout exceeded; try restarting transaction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931722/