I'm looking after a rails app running Passenger, and MySQL, on a VPS running Ubuntu Intrepid and earlier this morning, the webserver stopped being able to connect to the server. My exception notifier sent me a message with this error:

ActionView::TemplateError: Mysql::Error: MySQL server has gone away: SELECT * FROM `table_name` WHERE (`table_name`.`id` = 70)

Shelling into the vps trying to open up mysql gave me this:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I cleared this error by stopping apache, rebooting the database server and restarting passenger, which seems to have everything humming along fine again, but this doesn't solve the problem of working out why this happened in the first place.

Which places should I be looking to find the cause of this error? (other than production.log, or a /var/log ), so I can avoid this in future, and seeing that this was so simple to fix, would it be worth automating a reboot of the two servers if these conditions occurred again in future?


This issue happens when MySQL database closes the persistent connection opened by your Rails app. When your Rails app stays idle for a long time (>= connection close time as defined in mysql config) with no requests hitting it, this happens.

You might want to increase the connection expiry time in mysql settings - or write a cron job to keep fetching some dynamic page from the rails app (so that rails keeps the db handle fresh).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.