独立脚本报告“MySQL 服务器已消失”; my.cnf 有问题吗?

发布于 2024-11-08 15:48:17 字数 714 浏览 1 评论 0 原文

我有一个 ruby​​ 脚本,需要“mysql2”并使用 mysql 的查询语句
@db.query("从region_id=#{region["id"]}"的街道中选择sname,id")
但它总是向我报告这样的错误:

main.rb:261:in `query': MySQL server has gone away (Mysql2::Error)
    from main.rb:261:in `block (3 levels) in <main>'
    from main.rb:258:in `each'
    from main.rb:258:in `block (2 levels) in <main>'
    from main.rb:254:in `each'
    from main.rb:254:in `block in <main>'
    from main.rb:253:in `loop'
    from main.rb:253:in `<main>'

我的Ubuntu上的脚本没问题,但是,我的生产环境是CentOS,在那里我从源代码安装了MySQL。我的 Rails 应用程序运行正常,并且还需要“mysql2”,但是当我运行此脚本时,它失败了。

我认为问题与 my.cnf 有关,但我不知道如何配置它。

I have a ruby script that requires 'mysql2' and uses query statements to mysql like
@db.query("SELECT sname,id FROM streets where region_id=#{region["id"]}")
but it reports me a error always like this:

main.rb:261:in `query': MySQL server has gone away (Mysql2::Error)
    from main.rb:261:in `block (3 levels) in <main>'
    from main.rb:258:in `each'
    from main.rb:258:in `block (2 levels) in <main>'
    from main.rb:254:in `each'
    from main.rb:254:in `block in <main>'
    from main.rb:253:in `loop'
    from main.rb:253:in `<main>'

The script on my Ubuntu is OK, however, my production environment is CentOS and on there I installed MySQL from source code. My Rails app run there is OK and it also requires 'mysql2', but when I run this script it fails.

I think the problem is related to my.cnf but I don't know how configure it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪了杯 2024-11-15 15:48:17

mysql2 gem 为 一个选项 .org/2_3_release_notes.html#reconnecting-mysql-connections" rel="nofollow">在连接断开时重新连接到 MySQL。不要更新 my.cnf 文件,而是更新 gem 的 database.yml 文件以包含该

  reconnect: true

选项。这将确保 mysql2 gem 在需要时尝试重新连接到 MySQL。

The mysql2 gem provides an option to reconnect to MySQL in the event that the connection goes away. Rather than updating your my.cnf file, update your gem's database.yml file to include the

  reconnect: true

option. This will ensure that the mysql2 gem will attempt to reconnect to MySQL when required.

最终幸福 2024-11-15 15:48:17

你直接在对象@db上调用.query方法...这个对象到底是什么?

我的意思是...如果您在 Rails 下,您应该调用如下查询:

result = ActiveRecord::Base.connection.execute("sql here")

我认为这可能是原因,因为 Rails 管理连接池(例如,5 个已建立的连接,每个查询根据请求转到其中一个)。

无论如何,如果这不是原因,那么可能是您的 mysql2 gem 是使用系统上不同版本的 libmysqlclient.so 库编译的......
然后,当您尝试执行查询时,它会在内部抛出有关不兼容协议的错误并断开连接...

请检查您的 libmysqlclient.so 及其头文件是否具有相同版本...
或者,如果您没有安装更多版本......
然后请尝试重新编译mysql2 gem。

希望这有帮助。
问候,诺斯

you call .query method directly on object @db... what exactly is this object?

I mean... if you are under Rails, you should call queries like:

result = ActiveRecord::Base.connection.execute("sql here")

I think it could be the cause, because Rails manages connection pool (eg. 5 established connections and every query goes to one of them, based on requests).

Anyway, if this is not the cause, then it can be that your mysql2 gem is compiled with different version of libmysqlclient.so library on the system...
Then when you try to do a query, it internally throws an error about incompatible protocol and disconnects...

Please check that your libmysqlclient.so and it's header files are of the same version...
Or if you don't have more versions of them installed...
and then please try to recompile mysql2 gem.

Hope this helps.
Regards, NoICE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文