Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"MySQL server has gone away" with Ruby on Rails

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` 

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

How can we fix this?

like image 993
Gwyn Morfey Avatar asked Sep 19 '08 09:09

Gwyn Morfey


People also ask

How do you fix MySQL server has gone away?

To fix, you can increase the maximal packet size limit max_allowed_packet in my. cnf file, eg. set max_allowed_packet = 128M , then restart your MySQL server: sudo /etc/init. d/mysql restart.

Does Ruby on Rails work with MySQL?

Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL.

What does it mean MySQL server has gone away?

The MySQL server has gone away error, means that MySQL server (mysqld) timed out and closed the connection. By default, MySQL will close connections after eight hours (28800 seconds) if nothing happens.

Why is MySQL not connecting to server?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.


1 Answers

Ruby on Rails 2.3 has a reconnect option for your database connection:

production:   # Your settings   reconnect: true 

See:

  • Ruby on Rails 2.3 Release Notes, sub section 4.8 Reconnecting MySQL Connections.

  • MySQL auto-reconnect revisited

Good luck!

like image 170
mixonic Avatar answered Sep 29 '22 11:09

mixonic