Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

70,815

Solution 1

Just a further update - the solution in the question is correct.

The 4th comment is worth taking note of:

This isn't a bug with mysql2, it's a problem with the requirement in the ActiveRecord adapter: http://github.com/rails/rails/issues/21544. This is fixed in rails master: https://github.com/rails/rails/commit/5da5e3772c32593ecf2f27b8865e81dcbe3af692

I was able to tie Rails 4.2.4 to the 4-2-stable branch and get it working with the latest mysql2:

enter image description here

gem 'rails', '~> 4.2.4', git: "git://github.com/rails/rails.git", branch: '4-2-stable'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] #-> Rails 4.1+

#DB
gem 'mysql2'

Solution 2

Just do:

gem 'mysql2', '~> 0.3.18' this gem works with rails version 4.x.x

if install gem 'mysql2', '~> 0.4.0' it produces gem load error and causes compatibility issues

Solution 3

This issue was addressed here: https://github.com/brianmario/mysql2/issues/950

For Rails 4.x please pin the gem to mysql2 '~> 0.4.0' to avoid the 0.5.x upgrade.

Gemfile:

gem 'rails', '4.2.8'
gem 'mysql2', '~> 0.4.0'

Then run bundle update rails mysql2

I am currently using mysql v 8.0.11

Solution 4

The answer to this particular question relative to when it was posted and the version of Rails being used is that the problem is caused from doing a bundle update and your mysql2 version updates to 0.4.x which has an incompatibility issue with latest Rails ActiveRecord.

Again, please note this is NOT the solution for people using older versions of Rails / ActiveRecord.

The quick solution is to simply specify the mysql2 version in your gemfile as follows:

gem 'mysql2', '0.3.20'

The long solution would be to wait for either an update to ActiveRecord or something in mysql2 to change.

Solution 5

If you are able to upgrade your rails version, then change your Gemfile to this and it will solve the problem without downgrading the mysql2 gem version:

gem 'rails', '4.2.6'
Share:
70,815

Related videos on Youtube

newUserNameHere
Author by

newUserNameHere

SOreadytohelp

Updated on April 23, 2021

Comments

  • newUserNameHere
    newUserNameHere about 3 years

    In my gemfile I have:

    gem 'mysql2'
    

    My database.yml is as follows:

    default: &default
      adapter: mysql2
      database: <%= ENV['db_name'] %>
      username: <%= ENV['db_user'] %>
      password: <%= ENV['db_pass'] %>
      host:     <%= ENV['db_host'] %>
      pool: 32
      socket:   <%= ENV['socket'] %>
    
    development:
      <<: *default
    
    production:
      <<: *default
    

    I've run both bundle update and bundle install and my Gemfile.lock shows mysql2.

    However when I run rake db:migrate I get this on both my computer and on the staging server:

    myproject.com(master)$ rake db:migrate
    WARNING: Use strings for Figaro configuration. 10000012508 was converted to "10000012508".
    WARNING: Use strings for Figaro configuration. 860526407370038 was converted to "860526407370038".
    rake aborted!
    Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
    .....
    

    Just to make sure there wasn't a bad version of mysql2 or something, I did bundle clean --force and ran bundle install and bundle update again and when I run gem list I see mysql2 (0.4.0) and no other versions.

    Any ideas would be most appreciated.


    SOLUTION

    It's currently an issue with Rails 4.1.x and 4.2.x, per this bug report, it will be fixed in the next release of rails 4.2.x (credit to dcorr in comments for the link).

    In the mean time you can fix by doing downgraded to version 0.3.18 of mysql2 by adding this line to your gemfile:

    gem 'mysql2', '~> 0.3.18'
    
    • Pavan
      Pavan over 8 years
      Try bundle update mysql2
    • newUserNameHere
      newUserNameHere over 8 years
      I've tried that as well. Didn't work. From what I see the mysql2 (0.4.0) is the newest version ruby gems will take and it's already installed.
    • newUserNameHere
      newUserNameHere over 8 years
      Here was the solution: gem 'mysql2', '~> 0.3.18' in Gemfile.
    • dcorr
      dcorr over 8 years
      This isn't a bug with mysql2, it's a problem with the requirement in the ActiveRecord adapter: github.com/rails/rails/issues/21544. This is fixed in rails master: github.com/rails/rails/commit/…
    • Varun
      Varun over 8 years
      @Pavan Awsome maahn!! _/_
    • Richard Peck
      Richard Peck over 8 years
      If it's fixed in Rails master, can we pull from the Rails github repo URL to get that ver?
    • OneHoopyFrood
      OneHoopyFrood over 8 years
      Your solution should be placed as an answer to your own question, not an edit.
  • newUserNameHere
    newUserNameHere over 8 years
    +1 for the assist. However I have libmysqlclient-dev already, and the gem is not inside a group statement. What's weird is that it works fine on my other projects, just not this one. Also this is not a new install of my OS I haven't changed anything in my environment for some time. And the same is true of the staging server, where I am seeing the same problems as well.
  • seanriordan08
    seanriordan08 over 8 years
    If speed is a priority, it was a bit faster to bump mysql2 down to '0.3.20' instead of pinning rails '4.2.4' to branch: '4-2-stable'.
  • newBike
    newBike about 8 years
    it's hard to believe that mysql gem has so many stingy buggy problems compares to postgreSQL T__T
  • Richard Peck
    Richard Peck about 8 years
    It's the Windows environment - it's the same with Imagemagick etc. You have to use external dependencies, and since Windows doesn't have a central repo for them, it gets tricky
  • Richard Peck
    Richard Peck about 7 years
    What about Rails 5?
  • garci560
    garci560 almost 7 years
    @RichardPeck I have this problem with Rails 5.0.3
  • Richard Peck
    Richard Peck almost 7 years
    Works with Rails 5.1
  • Am33d
    Am33d almost 6 years
    Clean, simple and concise. Thanks!
  • khalidh
    khalidh over 5 years
    Worked for me @kaleem