0

I am a beginner at Rails and when I typed in 'rails server' in Terminal, I received this error:

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

I am using OSX Yosemite 10.10.5. I've tried installing it:

gem install mysql2

It still gave me the same error. I see that mysql2-0.4.0 is installed. Please help, thank you!

Javagaming
  • 13
  • 2

2 Answers2

0

There is a bug in Rails 4.2.4 and previous, with the newly released 0.4.0 version of the mysql2 gem -- one part of Rails will accidentally refuse to use the newly released 0.4.0 version of mysql2.

The issue is reported here, although without a lot of details:

https://github.com/rails/rails/issues/21544

Until a new version of Rails is released that fixes this one way or another, add this to your Gemfile, specifying that mysql2 0.4.0 won't work:

 # mysql 0.4.0 does not work with Rails 4.2.4
 # https://github.com/rails/rails/issues/21544
 gem 'mysql2', '>= 0.3.13', '< 0.4.0'

You previously probably just have gem 'mysql2' in your Gemfile -- add the version constraints as above, so it knows 0.4.0 won't work. Add the comments so you know why you did it, and can remove it later when no longer neccesary (probably whenever Rails 4.2.5 comes out).

Edit the Gemfile in your app like above, and then run bundle update mysql2 in your app directory, so your app will be using a mysql2 gem version 0.3.x again, as current Rails version wants.

When Rails 4.2.5 or later comes out and you upgrade to it, you will probably want to go back to your Gemfile and remove the version requirement specification for mysql2, return it to saying just gem 'mysql2' again. So your app will be willing to use the newer mysql2 0.4.0 gem, once Rails is willing to do so too.

jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • Should I install previous versions of mysql2? Will that work for now? – Javagaming Sep 09 '15 at 12:32
  • Yes, but the way you should do that is by following my instructions to edit your Gemfile, and then run `bundle update mysql2`. I will edit my answer to be more clear. Simply installing an earlier version of mysql2 with `gem install` won't work, you need to edit your Gemfile and run `bundle update` so your app will be using the earlier version. And then you don't need to manually run `gem install` at all. – jrochkind Sep 09 '15 at 12:53
  • If it works for you, please feel free to accept my answer! – jrochkind Sep 09 '15 at 15:20
  • Thank you! I will try it out and see. – Javagaming Sep 09 '15 at 16:09
  • I received this message after following the instruction----- You are trying to install in deployment mode after changing your Gemfile. Run `bundle install` elsewhere and add the updated Gemfile.lock to version control. If this is a development machine, remove the Gemfile freeze by running `bundle install --no-deployment`. You have added to the Gemfile: * mysql2 (< 0.4.0, >= 0.3.13) You have deleted from the Gemfile: * mysql2 ------- When I typed in 'rails server' on terminal, I still got the same Gem::Load Error message. – Javagaming Sep 09 '15 at 23:12
  • What does it mean by Run 'bundle install' elsewhere and add the updated Gemfile.lock to version control? How do I know if it is a development machine or not? Thanks! – Javagaming Sep 10 '15 at 00:56
  • At some point in the past you ran `bundle install --deployment`. Bundler remembered that in the .bundle file, and won't let you change your gem dependencies now. You probably didn't mean to do that. Follow it's advice and run `bundle install --no-deployment` like it suggests. – jrochkind Sep 11 '15 at 05:52
-1

add gem 'mysql2' to your Gemfile to specify the gems you want to use in your project

run bundle install which will install all gems, specified by Gemfile

run rails s should work fine

Tim Kretschmer
  • 2,272
  • 1
  • 22
  • 35