Rails的3.1 / mysql2错误:" MySQL服务器已消失" [英] Rails 3.1 / mysql2 error : "MySQL server has gone away"

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

问题描述

我遇到麻烦了升级我的rails 2.3.14 /红宝石1.8.7应用3.1.1 / 1.9.2:我有一些

I'm experiencing trouble upgrading my rails 2.3.14 / ruby 1.8.7 app to 3.1.1/1.9.2 : I have some

(ActiveRecord::StatementInvalid) "Mysql2::Error: MySQL server has gone away"

错误发生零星。重要的是要precise,我从来没有过这样的问题上与MySQL的创业板2.3.14和完全相同的数据库(这样的错误不应该来自MySQL的(v5.5.10))。

errors happening sporadically. It's important to precise that I never had such issues with the 'mysql' gem on 2.3.14 and the exactly same db (so the bug shouldn't come from mysql (v5.5.10)).

例如:

$ rails c production
Loading production environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > ActiveRecord::Base.connection.active?
 => false
ruby-1.9.2-p290 :002 > exit
$ rails c production
Loading production environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > ActiveRecord::Base.connection.active?
 => true 

这只是发生在我的(远程)生产数据库,与本地的开发数据库没有问题。我试图设置重新连接:真正的在我的database.yml,但它导致了

This happens only with my (remote) production database, no problem with my local development db. I've tried to set "reconnect: true" in my database.yml but it led to a

Mysql2::Error: Host '****' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts':...

我试图用一个小RB脚本只加载mysql2和ActiveRecord的隔离问题,但我没能重现错误的方式(所以它可能与轨道叠加)。

I've tried to isolate the problem with a little rb script only loading mysql2 and activerecord but I didn't manage to reproduce the bug that way (so it may be linked to the rails stack).

我不能从mysql2'回去,因为编码问题的MySQL的宝石(<一href="http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/">http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/ )。因此,我不得不回滚我生产我的铁轨2.3.14的应用程序,这令我非常难过很...

I can't go back from the 'mysql2' to the 'mysql' gem because of encoding issues ( http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/ ). As a consequence, I had to rollback my production to my rails 2.3.14 app, which saddens me very much...

你看看我能做些什么来调试呢?我甚至不能找到重现该错误的法子...有任何人遇到了同样的错误?

Do you see what I can do to debug this ? I can't even find a sure way to reproduce the error... Have anyone met the same bug ?

我刚刚发现几个人mentionning这个错误(例如:<一href="https://github.com/brianmario/mysql2/issues/213">https://github.com/brianmario/mysql2/issues/213)但不是一个解决方案。

I just found few people mentionning this bug (ex : https://github.com/brianmario/mysql2/issues/213) but not a solution.

感谢您的帮助。

推荐答案

好吧,我想我解决我的问题。我没有注意到它时,我贴我的问题,但似乎错误是超时相关:大约20秒后,ActiveRecord的losts其连接

Ok, I think I solved my problem. I didn't notice it when I posted my question, but it seemed that the error was timeout related : after about 20s, activerecord losts its connection.

$ rails runner "sleep 23; puts ActiveRecord::Base.connection.active?"
=> true
$ rails runner "sleep 25; puts ActiveRecord::Base.connection.active?"
=> false

所以,我挖进一步,我意识到,MySQL和mysql2宝石没有处理MySQL的WAIT_TIMEOUT'参数相同的方式:mysql的宝石不设置这样它使用的 MySQL的默认值28800 ,而<一href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb#LC277">mysql2创业板将其设置为2592000 如果在database.yml中没有定义。 但我有IM pression该值2592000超过这个参数的最大值:<一href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout">2147483 !这可能导致意外的行为,我描述了...

So I dug further and I realized that mysql and mysql2 gems didn't deal with the MySQL 'wait_timeout' param the same way : mysql gem doesn't set it thus uses the MySQL default value 28800, whereas mysql2 gem sets it at 2592000 if not defined in the database.yml. But I have the impression that the value 2592000 is over the max value for this param : 2147483 ! Which could lead to the unexpected behavior I described...

我建立一个脚本测试显示错误: https://gist.github.com/1514154

I build a script test showing the bug : https://gist.github.com/1514154

如果我有一些明显的随机拆卸,装导轨控制台(参见我的问题),我想这是因为我的应用程序花费很长的时间来加载和打字我的命令之前,我有时会等待几秒钟。

And if I had some apparently random disconnect while loading rails console (cf my question), I think it's because of my app taking a long time to load and me sometimes waiting a few seconds before typing my command.

我无法解释为什么我们这么少遇到这个问题。也许是针对我的配置(远程数据库,MySQL的版本?)。我试着用另一种远程临时数据库:错误没有重现......

I can't explain why we are so few to encounter this problem. Perhaps it's specific to my conf (remote database, MySQL version ?). I've tried with another remote staging database : the bug didn't reproduce...

因此​​,作为一个结论,我将设置 WAIT_TIMEOUT:2147483 在我的database.yml中。也许拉请求护栏......

So as a conclusion, I will set wait_timeout: 2147483 in my database.yml. And maybe pull request rails...

这篇关于Rails的3.1 / mysql2错误:&QUOT; MySQL服务器已消失&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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