“MySQL server has gone away” with Ruby on Rails

后端 未结 11 1280
后悔当初
后悔当初 2020-12-12 19:09

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

相关标签:
11条回答
  • 2020-12-12 19:49

    Try ActiveRecord::Base.connection.verify! in Ruby on Rails 4. Verify pings the server and reconnects if it is not connected.

    0 讨论(0)
  • I had this problem in a Ruby on Rails 3 application, using the mysql2 gem. I copied out the offending query and tried running it in MySQL directly, and I got the same error, "MySQL server has gone away.".

    The query in question was very, very large. A very large insert (+1 MB). The field I was trying to insert into was a TEXT column and their max size is 64 KB. Rather than throwing an errorm, the connection went away.

    I increased the size of the field and got the same thing, so I'm still not sure what the exact issue was. The point is that it was in the database due to some strange query. Anyway!

    0 讨论(0)
  • 2020-12-12 19:55

    First, determine the max_connections in MySQL:

    show variables like "max_connections";
    

    You need to make sure that the number of connections you're making in your Ruby on Rails application is less than the maximum allowed number of connections. Note that extra connections can be coming from your cron jobs, delayed_job processes (each would have the same pool size in your database.yml), etc.

    Monitor the SQL connections as you go through your application, run processes, etc. by doing the following in MySQL:

    show status where variable_name = 'Threads_connected';
    

    You might want to consider closing connections after a Thread finishes execution as database connections do not get closed automatically (I think this is less of an issue with Ruby on Rails 4 applications Reaper):

    Thread.new do
      begin
         # Thread work here
      ensure
         begin
            if (ActiveRecord::Base.connection && ActiveRecord::Base.connection.active?)
               ActiveRecord::Base.connection.close
            end
          rescue
          end
      end
    end
    
    0 讨论(0)
  • 2020-12-12 19:56

    As the other contributors to this thread have said, it is most likely that MySQL server has closed the connection to your Ruby on Rails application because of inactivity. The default timeout is 28800 seconds, or 8 hours.

    set-variable = wait_timeout=86400
    

    Adding this line to your /etc/my.cnf will raise the timeout to 24 hours http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#option_mysqld_wait_timeout.

    Although the documentation doesn't indicate it, a value of 0 may disable the timeout completely, but you would need to experiment as this is just speculation.

    There are however three other situations that I know of that can generate that error. The first is the MySQL server being restarted. This will obviously drop all the connections, but as the MySQL client is passive, and this won't be noticed till you do the next query.

    The second condition is if someone kills your query from the MySQL command line, and this also drops the connection, because it could leave the client in an undefined state.

    The last is if your MySQL server restarts itself due to a fatal internal error. That is, if you are doing a simple query against a table and instantly see 'MySQL has gone away', I'd take a close look at your server's logs to check for hardware error, or database corruption.

    0 讨论(0)
  • 2020-12-12 19:57

    Do you monitor the number of open MySQL connections or threads? What is your mysql.ini settings for max_connections?

    mysql> show status;
    

    Look at Connections, Max_used_connections, Threads_connected, and Threads_created.

    You may need to increase the limits in your MySQL configuration, or perhaps rails is not closing the connection properly*.

    Note: I've only used Ruby on Rails briefly...

    The MySQL documentation for server status is in http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html.

    0 讨论(0)
  • 2020-12-12 20:00

    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!

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