Rails 4 - Gem::LoadError: Specified 'mysql2

2019-01-12 19:07发布

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'

9条回答
Summer. ? 凉城
2楼-- · 2019-01-12 19:40

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.

查看更多
Explosion°爆炸
3楼-- · 2019-01-12 19:41

Had same issue but adding source 'https://rubygems.org' to the top of gem file solved issue.

Steps:

add source 'https://rubygems.org' to your gem file. Comment out mysql2 gem and add gem 'mysql2', '~> 0.3.18' Run bundle install

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-12 19:43

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

查看更多
登录 后发表回答