Trying to update 640k rows in mySQL gets “Lost connection to MySQL server during query”

前端 未结 2 1792
旧时难觅i
旧时难觅i 2020-12-18 08:34

I\'m trying to execute this line from my seeds.rb file:

ActiveRecord::Base.connection.execute(\"UPDATE bairros SET created_at = (SELECT NOW());\")

相关标签:
2条回答
  • 2020-12-18 09:03

    "Lost connection to MySQL server during query" means that either:
    1. The database crashed while running the query (can be due to restart, table corruption, etc).
    2. The session disconnected due to query timeout (check variable wait_timeout and interactive_timeout depending on your connection type), network problem, someone\something killing your query.

    0 讨论(0)
  • 2020-12-18 09:25

    There is another potential reason for this error, and that is the ActiveRecord connection pool reaper. When enabled, the reaper scans the connection pool for "dead" connections and closes them. In my testing, it appears to be overzealous and closes perfectly alive connections too (generally ones running slightly bigger queries).

    Try clearing reaping_frequency from your DB config (which turns it off) and see if that helps. Scan your codebase for that string, and make sure it's unset (or simply removed!). If you see a line like config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10, know that the || 10 is actually setting a low default of 10s. This pattern was in the rails codebase for a time, until the change was reverted due to it causing various issues, including killing long running queries, but is still recommended by Heroku.

    If disabling the reaper fixes things, I would recommend leaving it disabled. Rails no longer sets it by default, and it seems to cause more problems than it solves.

    I had an error like yours, and that was how I fixed it. Personally, I am keeping it disabled. I wrote about my specific problem in more detail on my blog.

    0 讨论(0)
提交回复
热议问题