Gem::LoadError for mysql2 gem, but it's alread

2019-01-10 06:11发布

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.

13条回答
The star\"
2楼-- · 2019-01-10 06:36

I have previously installed mysql2 0.4.5 but that was giving me this error so i have installed another version of mysql2 by:

gem install mysql2 --version 0.3.20

Hope this solves your problem.

查看更多
我命由我不由天
3楼-- · 2019-01-10 06:37

I got the same error after an upgrade to Rails 4.1 and I managed to resolve it by updating mysql2. Run this in your rails app folder:

$ bundle update mysql2
查看更多
爷、活的狠高调
4楼-- · 2019-01-10 06:43

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

查看更多
唯我独甜
5楼-- · 2019-01-10 06:47

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

查看更多
【Aperson】
6楼-- · 2019-01-10 06:47

Here is how I fixed this:

 bundle config
 bundle config --delete without
 bundle install --deployment --without development test postgres

Credits: How do you undo bundle install --without

查看更多
何必那么认真
7楼-- · 2019-01-10 06:50

I solved the problem, installing the mysql2 gem local (gem install mysql2, bundle install) and adding the following line to the Gemfile:

gem 'mysql2'

Setting the mysql2 adapter in database.yml

adapter: mysql2

was also important!

查看更多
登录 后发表回答