4

我正在尝试从我的seeds.rb 文件中执行这一行:

ActiveRecord::Base.connection.execute("UPDATE bairros SET created_at = (SELECT NOW());")

我的名为“bairros”的表有 643k 行,一旦它在rake db:seed命令中到达这个文件,它就会抛出这个错误

    Mysql2::Error: Lost connection to MySQL server during query: UPDATE bairros SET created_at = (SELECT NOW());
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `query'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `block in execute'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `execute'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:220:in `execute'
/home/ubuntu/projetos/AnuncieImoveis/releases/20131015210221/db/seeds.rb:48:in `<top (required)>'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/railties-4.0.0/lib/rails/engine.rb:540:in `load_seed'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:153:in `load_seed'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/bin/ruby_noexec_wrapper:14:in `eval'
/home/ubuntu/.rvm/gems/ruby-2.0.0-p195@thedoors/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

我真的不知道如何解决这个问题。有人可以帮我吗?

4

2 回答 2

9

此错误还有另一个潜在原因,即 ActiveRecord 连接池reaper。启用后,收割者会扫描连接池中的“死”连接并关闭它们。在我的测试中,它似乎过于热心并且也关闭了完全活跃的连接(通常是运行稍微大一点的查询的连接)。

尝试reaping_frequency从您的数据库配置中清除(将其关闭),看看是否有帮助。扫描您的代码库以查找该字符串,并确保它未设置(或简单地删除!)。如果您看到类似 的行config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10,就知道|| 10实际上设置了 10 秒的低默认值。这种模式在 rails 代码库中存在了一段时间,直到更改由于导致各种问题而被恢复,包括杀死长时间运行的查询,但 Heroku 仍然推荐

如果禁用收割者可以解决问题,我建议将其禁用。Rails 不再默认设置它,而且它似乎导致的问题比它解决的要多。

我遇到了像你这样的错误,这就是我修复它的方法。就个人而言,我将其禁用。我在我的博客上更详细地写了我的具体问题。

于 2014-03-15T12:36:41.057 回答
1

“在查询期间丢失与 MySQL 服务器的连接”意味着:
1. 数据库在运行查询时崩溃(可能是由于重新启动、表损坏等)。
2. 由于查询超时(检查变量 wait_timeout 和 interactive_timeout 取决于您的连接类型)、网络问题、某人\某事杀死您的查询,会话断开连接。

于 2014-02-10T13:26:01.313 回答