Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem::LoadError for mysql2 gem, but it's already in Gemfile

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

This error occurred while loading the following files:

active_record/base

This is the error I get on running rails server.

The mysql2 gem has been added to the Gemfile as well.

I've done bundle install, and tried restarting the server but still get the error.

like image 799
Mysoulisinseoul Avatar asked Apr 08 '14 09:04

Mysoulisinseoul


3 Answers

If you have this error when upgrading to rails 4.2.4 (also with rails 4.1.5) try using this version of mysql2:

gem 'mysql2', '~> 0.3.18'

Apparently mysql2 isn't still compatible with newer version of rails because rails 4.2.4 is pretty new as the time of answering this question by me 8 September 2015 so use the above line in your Gem file and run:

bundle install 

You should be good to go

like image 86
Amir Hassan Azimi Avatar answered Oct 10 '22 20:10

Amir Hassan Azimi


It worked for me when I specified a mysql2 gem version before the newest one (0.4.0). For some reason there is a problem with Rails 4.2.4 and that gem 0.4.0. So, to solve the problem I just specified the previous gem released: 0.3.20 and it worked fine for me!

gem 'mysql2', '~> 0.3.20'
bundle install

You can check all the gems versions here: https://rubygems.org/gems/mysql2/versions

like image 45
rom5jp Avatar answered Oct 10 '22 20:10

rom5jp


Change to

gem 'mysql2', '~> 0.3.18'

in your Gemfile.

This thread on the official mysql2 Github says to do this. You need to declare that version number if you're rails version 4.x.x.

https://github.com/brianmario/mysql2/issues/675

Then run bundle update mysql2.

like image 17
suga_shane Avatar answered Oct 10 '22 21:10

suga_shane