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?
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:
In the file vendor/rails/actionpack/lib/action_controller/dispatcher.rb is the code:
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!, orverify_active_connections!has been changed, etc.