ActiveRecord mysql2连接报错问题

报错信息

ActiveRecord::StatementInvalid (Mysql2::Error: Lost connection to MySQL server during query: ........

解决方式,在Rails项目里加入以下文件

# config/initializers/connection_fix.rb

Rails.application.config.after_initialize do
  config  = ActiveRecord::Base.configurations[Rails.env] ||
              Rails.application.config.database_configuration[Rails.env]

  adapter = config['adapter']

  if adapter == "mysql2"
   module ActiveRecord::ConnectionAdapters
     class Mysql2Adapter
       alias_method :execute_without_retry, :execute

       def execute(*args)
         execute_without_retry(*args)
       rescue ActiveRecord::StatementInvalid => e
         if e.message =~ /server has gone away/i ||
            e.message =~ /Lost connection to MySQL server during query/i
           reconnect!
           retry
         else
           raise e
         end
       end
     end
   end
  end
end

发表于 2019.04.24