gpt4 book ai didi

mysql - Rails 3.1.0 和 Resque—— "Mysql2::Error: MySQL server has gone away"

转载 作者:行者123 更新时间:2023-11-29 03:43:50 25 4
gpt4 key购买 nike

帮助!我总是在随机时间收到“MySQL 服务器已消失”的错误。同一个 worker 有时会运行,然后在下一次以这种方式崩溃。我似乎找不到这种疯狂的模式。

我尝试了以下解决方案:

ActiveRecord::Base.verify_active_connections!

根据 resque faq 在我的每个任务中执行方法.

我也试过这段代码:

adapter = ActiveRecord::Base.configurations[Rails.env]['adapter']

if adapter == "mysql2"
module ActiveRecord::ConnectionAdapters
class Mysql2Adapter
alias_method :execute_without_retry, :execute

def execute(*args)
execute_without_retry(*args)
rescue ActiveRecord::StatementInvalid => e
if e.message =~ /server has gone away/i
warn "Server timed out, retrying"
reconnect!
retry
else
raise e
end
end
end
end
end

在初始化器中,作为 per code by a Resque developer ,在 StackOverflow 和其他地方发现了一些修改。

我也同时尝试过以上两种方法。结果是一样的。

我还尝试根据 this StackOverflow post 在我的 database.yml 中设置 wait_timeout: 2147483无济于事。

我正在使用:

  • rails 3.1.0
  • 重新请求 1.20.0
  • resque-scheduler 1.9.9
  • mysql2 0.3.11

一切都在带有 RVM/Homebrew/Passenger 的 Mac 上运行。 mysql 数据库在我的机器本地。

这是一个错误的例子:

Mysql2::Error: MySQL server has gone away: SHOW FIELDS FROM `m_variables`
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `block in execute'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:473:in `columns'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `block (2 levels) in initialize'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `block in initialize'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `yield'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `default'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:106:in `block in initialize'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `yield'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `default'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:711:in `columns_hash'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/locking/optimistic.rb:145:in `locking_enabled?'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:110:in `to_a'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
/Users/fdot/Sites/mq/app/workers/m_variable_type_assigner.rb:25:in `perform'

这是另一个例子:

Mysql2::Error: MySQL server has gone away: SELECT `users`.* FROM `users`
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `query'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:687:in `block in exec_query'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:685:in `exec_query'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:679:in `select'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:470:in `find_by_sql'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:111:in `to_a'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
/Users/fdot/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
/Users/fdot/Sites/mq/app/workers/transient_user_cleaner.rb:21:in `perform'

我在这里不知所措。非常感谢您的帮助!

最佳答案

当连接太多,mysql 无法处理时,就会发生这种情况。

尝试限制您正在进行的连接和处理的数量。不是并行运行,而是让一些请求串行运行。

这更像是一个 Mysql 错误,而不是您的语言或框架错误:请参阅 here了解更多详情

关于mysql - Rails 3.1.0 和 Resque—— "Mysql2::Error: MySQL server has gone away",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9442489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com