I cannot load my 'mysql2' gem.

别来无恙 提交于 2020-03-25 19:12:33

问题


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!


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/32470903/i-cannot-load-my-mysql2-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!