can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2 in Rails 3.1

11,439

Solution 1

Active Record has it's own requirements on which versions of mysql2 are compatible. Here's the line of code for Rails 3.1. You must use a version of mysql2 that satisfies these requirements.

Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)

This is saying Rails expects a mysql2 version greater than 0.3.6 and less than 0.4.0, but found version 0.3.2. If you change your Gemfile to request a version in this range then Active Record should be happy. Perhaps

gem 'mysql2', '0.3.6'

Don't forget to update your bundle after changing your Gemfile.

bundle update mysql2

Solution 2

I know this is a very old thread but because it still comes up in Google results a one of the first results I though I would updated it with I had to do work around this issue in newer versions of Rails.

The errors shows as:

Gem::LoadError: Specified 'mysql2' for database adapter, 
but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile 

followed by:

Gem::LoadError: can't activate mysql2 (< 0.5, >= 0.3.13), 
already activated mysql2-0.5.2. Make sure all dependencies are 
added to Gemfile.

I already had the gem added in my Gemfile as shown below:

group :production do
  gem 'mysql2'
end

but I had to update it to prevent from picking up a version outside of the range indicated in the error message. Notice the '< 0.5' below:

group :production do
  gem 'mysql2', '< 0.5'
end

Additionally, different versions of Rails set different min/max version restrictions for the MySQL gem:

From https://github.com/brianmario/mysql2/issues/950#issuecomment-376375844

"Correct, Rails 4.x cannot use mysql2 0.5.x. The actual code would work fine, but Rails 4 has a version restriction in place to prevent use of mysql2 0.5.x. This is intentional, as it allows future changes to the API without silently risking their use in older code not prepared to use the new APIs. There's also nothing magic about the version numbers -- it's totally by coincidence that Rails 4 uses mysql2 0.4 and Rails 5 will be able to use mysql2 0.4 or 0.5."

Solution 3

This also had me pulling out my hair. To only reasonable solution I could get was to switch to the master branch of the mysql2 gem.

gem 'mysql2', :git => 'git://github.com/brianmario/mysql2.git'

After this update my Rails 3.1.0.rc5 application could start with MySQL. (At the time of this post the latest version of the gem was 0.3.6)

Share:
11,439
Mark
Author by

Mark

Updated on June 04, 2022

Comments

  • Mark
    Mark almost 2 years

    I'm just trying to get the most basic of basic shell of a rails app running under 3.1, and I'm getting this weird error when I run bundle exec rake db:migrate-

    Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)
    

    All the posts that I've read here and elsewhere say I should be using the newer mysql2 adaptor for rails 3.1, so I have-

    gem 'mysql2', '0.3.2'
    

    in my gemfile. Some post have suggested using-

    gem 'mysql2', '~> 0.3'
    

    but that gets me the same error. The gem is installed at-

    /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.3.2
    

    It was suggested that I switch up that line in my gemfile again, this time to be-

    gem 'mysql2', '< 0.3'
    

    but when I do that, run bundle install, and then try to run migrations again, I get-

    An error has occurred, all later migrations canceled:
    undefined method `rows' for nil:NilClass
    

    My complete migration file looks like this-

    class CreatePlaces < ActiveRecord::Migration
      def change
        create_table :places do |t|
          t.string :title
          t.string :meta_description
          t.string :permalink, :limit => 60
          t.string :name, :limit => 60
          t.string :address
          t.string :state, :limit => 2
          t.string :region, :limit => 3
          t.float :latitude
          t.float :longitude
          t.text :description
          t.boolean :active, :default => true
    
          t.timestamps
        end
        add_index :places, [:permalink, :state, :region, :latitude, :longitude, :active], :name => 'places_index'
      end
    end
    

    And the full output of running that migration is-

    ==  CreatePlaces: migrating ===================================================
    -- create_table(:places)
       -> 0.0925s
    -- add_index(:places, [:permalink, :state, :region, :latitude, :longitude, :active], {:name=>"places_index"})
       -> 0.1097s
    ==  CreatePlaces: migrated (0.2023s) ==========================================
    
    rake aborted!
    An error has occurred, all later migrations canceled:
    
    undefined method `rows' for nil:NilClass
    

    There are no later migrations, that's the only one, as this is an app that I'm just starting to try to get Rails 3.1 running properly. Dropping the database and recreating it gets me to the same place.

    I am able to access Places from the console-

    ruby-1.9.2-p180 :001 > Place
       (0.3ms)  SHOW TABLES
       (0.1ms)  SHOW TABLES
       (1.1ms)  describe `places`
     => Place(id: integer, title: string, meta_description: string, permalink: string, name: string, address: string, state: string, region: string, latitude: float, longitude: float, description: text, active: boolean, created_at: datetime, updated_at: datetime) 
    

    But when I actually try to do a find or anything on Places, I get the following error-

    Place.find(:all)
    ArgumentError: wrong number of arguments (3 for 2)
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:634:in `select'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/database_statements.rb:9:in `select_all'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:470:in `find_by_sql'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation.rb:111:in `to_a'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:155:in `all'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:105:in `find'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in `find'
        from (irb):2
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:45:in `start'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:8:in `start'
        from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
    

    Anyone have any ideas? I've been digging for like 18 hours now, and just running in circles.

    Thanks, --Mark

  • Mark
    Mark over 12 years
    Armand, thanks for looking into this! When I add that line to my gemfile and run bundle install, I get the following error upon trying to access the app: "git://github.com/brianmario/mysql2.git (at master) is not checked out." Any other ideas would be appreciated!
  • Andrew Hubbs
    Andrew Hubbs over 12 years
    I was having the exact same issue as the op and this fixed it. Thanks Armand.
  • Ndeto
    Ndeto almost 4 years
    Perfect, adding gem 'mysql2', '< 0.5' worked after trying almost everything! Most important part, the version specification '< 0.5'