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条回答
聊天终结者
2楼-- · 2019-01-12 19:19

As a complete beginner i got confused on how to do this so I just did a trial check and finally got my server working , here is how i got it working .

get into the file of working directory("work") and from there get into the file of the app you created("sample_app")which has mysql 2 installed go to gem file ("Gemfile")edit gem 'mysql2' to gem 'mysql2', '~> 0.3.18'

now go back to cmd and run command "bundle install".

so the directory to edit file taking consideration of above files in brackets should be . "c\work\sample_app\Gemfile"

hope i was of some help .

查看更多
仙女界的扛把子
3楼-- · 2019-01-12 19:20

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

查看更多
淡お忘
4楼-- · 2019-01-12 19:22

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'
查看更多
冷血范
5楼-- · 2019-01-12 19:28

try this:

bundle update mysql2

this command will update your 'mysql2' gem to the latest version (should be 0.3.17 or higher) and start your rails server.

查看更多
叛逆
6楼-- · 2019-01-12 19:35

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'
查看更多
混吃等死
7楼-- · 2019-01-12 19:38

This usually happens when you are missing some mysql packages on your machine. Do you get any errors from gem install mysql2? What OS are you working on?

If on debian or ubuntu try sudo apt-get install libmysqlclient-dev.

Also make sure that the gem is not placed inside a group statement in your Gemfile.

查看更多
登录 后发表回答