mysql2: Mysql2::Error: closed MySQL connection

We have a long-running, fairly active daemon that dies intermittently with this error (max once a day, min every few weeks). We’re on Rails 3.0.10 and mysql2 0.2.11. Here’s the full error:

Mysql2::Error: closed MySQL connection: DELETE FROM `atable` WHERE (id IN (976613))

#<ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection: DELETE FROM `atable` WHERE (id IN (976613))>

Any ideas on what could be going on? Anything I can do to help debug?

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 46 (5 by maintainers)

Most upvoted comments

This might help someone, someday:

If you have persistent database connections and workers/threads that don’t run often enough (say you’ve over-provisioned your web/unicorn workers and some sit idle for a good chuck of the day), there is a chance that when the worker does get work (perhaps during a busier time), it tries to use the database but it has hit mysql’s wait_timeout (default is 8 hours). Mysql may have since disconnected, but since the worker hasn’t been doing anything, it won’t have noticed until it actually needs to use the connection. This will look like a really short time period (since the start of the request now being processed). Using reconnect: true should help with this case too.

i fix by reconnect: true in database.yml

Today is someday. Thank you for posting a summary!