Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

混江龙づ霸主 提交于 2019-11-27 17:05:29
Richard Peck

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:

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'

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

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.

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

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'

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 .

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.

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

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.

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