ruby-on-rails - 运行 LargeHadronMigrator add_column 期间出现 Mysql 错误 "Lock wait timeout exceeded; try restarting transaction"

标签 ruby-on-rails rails-migrations

我试图解决在没有锁定的情况下在大型 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/

相关文章:

mysql - Rails 4.2 确保数据库和实例中两列的唯一性

ruby-on-rails-4 - 即使没有挂起的迁移,Rails 4 也会抛出挂起的迁移错误

ruby - 在迁移中从 lambda 指定默认值

ruby-on-rails - 数组 :Class 的未定义方法 `where'

ruby-on-rails - 使用 RSpec 测试模块内部的类

mysql - 在 Active Record 中添加自定义列数据类型

ruby-on-rails - 为什么 ActiveRecord 对我的新表设置了不需要的外键约束?

ruby-on-rails - 在 ruby​​ 中获取 oauth2 access_token 所有者电子邮件地址?

ruby-on-rails - Requirejs-rails 阻止升级到 rails 4

javascript - 如何在辅助 Rails 中从 collection_select 向路线发送 POST