http://stackoverflow.com/questions/100631/mysql-server-has-gone-away-with-rails
http://www.ruby-forum.com/topic/123472
暂时采纳了答3,实践中。
【转载】
问:
After our rails app 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`
Restarting the mongrels (not the mysql server) fixes it.
Any hints?
答:
答1
This is probably caused by the persistent connections to mysql going away (time out is likely if its happening over night) and rails failing to restore the connection, which it should be doing by default:
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
the method verify_active_connections! performs several actions, one of which is to recreate any expired 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.
答2
As the other contributors to this thread have said, it is most likely that mysql server has closed the connection to your rails app because of inactivity. The default timeout is 28800 seconds, or 8 hours.
引用
set-variable = wait_timeout=86400
Adding this line to your /etc/my.cnf will raise the timeout to 24 hours
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#option_mysqld_wait_timeout
Although the documentation doesn't indicate it, a value of 0 may disable the timeout completely, but you would need to experiment as this is just speculation.
There are however three other situations that I know of that can generate that error. The first is the mysql server being restarted. This will obviously drop all the connections, but as the mysql client is passive, this won't be noticed till you do the next query.
The second condition is if someone kills your query from the mysql command line, this also drops the connection because it could leave the client in an undefined state.
The last is if your mysql server restarts itself due to a fatal internal error. That is, if you are doing a simple query against a table and instantly see 'mysql has gone away', I'd take a close look at your server's logs to check for hardware error, or database corruption.
答3
Rails 2.3 has a reconnect option for your database connection:
production:
# Your settings
reconnect: true
See:
http://guides.rubyonrails.org/2_3_release_notes.html#reconnecting-mysql-connections
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/49d2a7e9c96cb9f4?pli=1
Good luck!
答4
The connection to the MySQL server is probably timing out.
You should be able to increase the timeout in MySQL, but for a proper fix, have your code check that the database connection is still alive, and re-connect if it's not.
答5
Do you monitor the number of open mysql connections or threads? What is your mysql.ini settings for max_connections?
引用
mysql> show status;
Look at Connections, Max_used_connections, Threads_connected, and Threads_created.
You may need to increase the limits in your mysql config, or perhaps rails is not closing the connection properly*.
Note: I've only used rails briefly..
Here is the mysql docs for server status.
[url]
http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html[/url]
---
分享到:
相关推荐
If you’re an experienced developer, this book will give you the comprehensive, insider information you need., Rails has evolved over the years, and this book has evolved along with it. We still ...
《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...
《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...
《Ruby on Rails与MySQL环境配置详解》 在开发基于Web的应用程序时,Ruby on Rails(简称Rails)框架和MySQL数据库的结合是常见的选择。本文将详细介绍如何在Windows环境下配置Ruby on Rails以支持Rails 2.3.5及...
《敏捷Web开发与Rails》是一本深度探讨如何利用Ruby on Rails框架进行敏捷Web开发的指导书籍,由Dave Thomas、David Heinemeier Hansson等多位在Rails社区有着深厚贡献的作者共同编写。本书不仅覆盖了Rails的基本...
agile web development with rails 5(英文电子书).............................................................................................................................................................
### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...
Agile Web Development with Rails 1-14节_ppt(老师发的修正版)
《敏捷Web开发与Rails:程序指南 第四版》是一本深度探讨使用Ruby on Rails框架进行敏捷Web应用开发的专业书籍。本书旨在帮助开发者充分利用Rails 4的特性,提高开发效率,实现快速迭代和高质量的代码编写。 Rails是...
由于在Windows上直接安装可能会遇到问题,可以尝试使用预编译的二进制版本,或者在命令行中运行`gem install mysql2 --platform=ruby -- --with-mysql-dir="C:\path\to\mysql\installation"`,其中"C:\path\to\mysql...
《敏捷Web开发与Rails》第四版是一本专为软件开发者设计的权威指南,全面涵盖了使用Ruby on Rails框架进行敏捷Web应用开发的知识。Rails 3是该版本的重点,它引入了许多新特性和改进,使得开发过程更为高效且灵活。...
Ruby on Rails (often shortened as Rails) is a server-side web application framework written in Ruby under the MIT License. It uses Model-View-Controller (MVC) architecture and emphasizes convention ...
在Ruby on Rails框架中,与SQLServer 2000集成可能会比与其他常见的数据库系统(如MySQL或PostgreSQL)稍复杂一些,因为SQLServer 2000的兼容性问题和缺少官方支持。然而,通过一些第三方库和适当的配置,我们仍然...
### Agile Web Development with Rails for Rails 3.2 #### 核心知识点概览 - **Rails 3.2概述** - **敏捷开发方法论** - **Model-View-Controller (MVC) 模式** - **Ruby on Rails基础与高级特性** - **面向对象...
在开发Web应用时,Rails框架和MySQL数据库的集成是一个常见的选择。然而,有时在尝试连接Rails应用到MySQL数据库时,可能会遇到一些问题。本篇文章将深入探讨这些常见问题及其解决方案。 首先,Rails与MySQL的连接...