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

6

10

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

Simulacrum answered 26/7, 2011 at 6:15 Comment(2)
Are you sure that in migration file its def change instead of def self.up ??Justicz
Yes, that's new 3.1 syntax. Up and down have been eliminated. See edgerails.info/articles/what-s-new-in-edge-rails/2011/05/06/… for details.Simulacrum
E
14

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
Entitle answered 16/9, 2015 at 0:9 Comment(0)
C
4

if anyone here is stuggling with this in 2022 with the new M2 book

"can't activate mysql2 (~> 0.3.10), already activated mysql2-0.5.2"

you have to update this file:

/usr/local/bundle/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/mysql2_adapter.rb:3

enter image description here

and change it to the version of the mysql gem you are using.

Caterina answered 18/11, 2022 at 22:26 Comment(1)
This advice does not have a price. Thanks a lot @matt laoPurgatory
D
3

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."

Dogbane answered 2/8, 2018 at 16:47 Comment(1)
Perfect, adding gem 'mysql2', '< 0.5' worked after trying almost everything! Most important part, the version specification '< 0.5'Sconce
T
2

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)

Township answered 2/8, 2011 at 19:10 Comment(2)
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!Simulacrum
I was having the exact same issue as the op and this fixed it. Thanks Armand.Herbart
B
1

I have the same issue several times with an Redmine implementation.

I changed a gem line place on

RUBYGEM@VERSION/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/mysql2_adapter.rb

In the line 3 change this

gem 'mysql2', '~> 0.3.10'

with this

gem 'mysql2', '~> 0.4.10' (or whatever version you need)

And all works fine

Buchholz answered 1/12, 2021 at 1:17 Comment(0)
D
-1

Pretty old question, so I'm assuming the original poster has already solved the issue. However, in case anyone is coming to this post trying to solve the first issue:

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 most likely occurring because you are not running your migrations via bundle exec. Try running bundle exec rake db:migrate.

Domela answered 9/3, 2012 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.