Rails 3.1 / mysql2 error : “MySQL server has gone away”

前端 未结 2 1259
独厮守ぢ
独厮守ぢ 2020-12-29 15:00

I\'m experiencing trouble upgrading my rails 2.3.14 / ruby 1.8.7 app to 3.1.1/1.9.2 : I have some

(ActiveRecord::StatementInvalid) \"Mysql2::Error: MySQL se         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 15:48

    Had a lot of lost connections - but I couldn't say if they went away due to the following tweak or elsewise :/

    Had to throw the following script into initializers and add a line of configuration to each of my databases in my database.yml like this:

    ...
    flags: <%= 65536 | 131072 %>
    ...
    

    The script looks like this:

    /config/initializers/mysql2.rb

    module ActiveRecord
      class Base
        # Overriding ActiveRecord::Base.mysql2_connection
        # method to allow passing options from database.yml
        #
        # Example of database.yml
        #
        #   login: &login
        #     socket: /tmp/mysql.sock
        #     adapter: mysql2
        #     host: localhost
        #     encoding: utf8
        #     flags: 131072
        #
        # @param [Hash] config hash that you define in your
        #   database.yml
        # @return [Mysql2Adapter] new MySQL adapter object
        #
        def self.mysql2_connection(config)
          config[:username] = 'root' if config[:username].nil?
    
          if Mysql2::Client.const_defined? :FOUND_ROWS
            config[:flags] = config[:flags] ? config[:flags] | Mysql2::Client::FOUND_ROWS : Mysql2::Client::FOUND_ROWS
          end
    
          client = Mysql2::Client.new(config.symbolize_keys)
          options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
          ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
        end
      end
    end
    

提交回复
热议问题