"MySQL服务器已消失"使用Ruby on Rails [英] "MySQL server has gone away" with Ruby on Rails

查看:74
本文介绍了"MySQL服务器已消失"使用Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的Ruby on Rails应用程序运行了一段时间后,由于"MySQL服务器已消失",它开始抛出500秒钟.通常这会在一夜之间发生.它是从最近开始执行此操作的,我们的服务器配置没有明显变化.

After our Ruby on Rails application has run for a while, it starts throwing 500s with "MySQL server has gone away". Often this happens overnight. It's started doing this recently, with no obvious change in our server configuration.

 Mysql::Error: MySQL server has gone away: SELECT * FROM `widgets`

重新启动mongrels(而不是MySQL服务器)可以解决此问题.

Restarting the mongrels (not the MySQL server) fixes it.

我们如何解决这个问题?

How can we fix this?

推荐答案

这可能是由于与MySQL的持久连接消失了(如果发生在夜间,则可能会超时),并且Ruby on Rails无法恢复连接,默认情况下应该这样做:

This is probably caused by the persistent connections to MySQL going away (time out is likely if it's happening over night) and Ruby on Rails is failing to restore the connection, which it should be doing by default:

在文件vendor/rails/actionpack/lib/action_controller/dispatcher.rb中是代码:

In the file vendor/rails/actionpack/lib/action_controller/dispatcher.rb is the code:

if defined?(ActiveRecord)
  before_dispatch { ActiveRecord::Base.verify_active_connections! }
  to_prepare(:activerecord_instantiate_observers) {ActiveRecord::Base.instantiate_observers }
end

方法verify_active_connections!执行多个操作,其中之一是重新创建任何过期的连接.

The method verify_active_connections! performs several actions, one of which is to recreate any expired connections.

此错误的最可能原因是因为猴子补丁重新定义了调度程序不调用verify_active_connections!verify_active_connections!已更改,等等.

The most likely cause of this error is that this is because a monkey patch has redefined the dispatcher to not call verify_active_connections!, or verify_active_connections! has been changed, etc.

这篇关于"MySQL服务器已消失"使用Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆