Ruby-on-rails Rails 4 - Gem::LoadError:为数据库适配器指定了“mysql2”,但未加载 gem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32457657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:04:19  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-4ruby-on-rails-4.1mysql2ruby-on-rails-4.2

提问by newUserNameHere

In my gemfile I have:

在我的 gemfile 中,我有:

gem 'mysql2'

My database.yml is as follows:

我的database.yml如下:

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 updateand bundle installand my Gemfile.lock shows mysql2.

我已经运行了bundle updatebundle install,我的 Gemfile.lock 显示了 mysql2。

However when I run rake db:migrateI get this on both my computer and on the staging server:

但是,当我运行时,我rake db:migrate在我的计算机和登台服务器上都得到了这个:

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 --forceand ran bundle installand bundle updateagain and when I run gem listI see mysql2 (0.4.0)and no other versions.

只是为了确保没有 mysql2 或其他版本的错误,我做bundle clean --forcebundle installbundle update一次又一次地运行,当我运行时,gem list我看到mysql2 (0.4.0)并且没有其他版本。

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).

它目前是 Rails 4.1.x 和 4.2.x 的一个问题,根据这个错误报告,它将在下一个版本的 rails 4.2.x 中修复(在链接的评论中归功于 dcorr)。

In the mean time you can fix by doing downgraded to version 0.3.18 of mysql2 by adding this line to your gemfile:

同时,您可以通过将以下行添加到您的 gemfile 来降级到 mysql2 的 0.3.18 版来修复:

gem 'mysql2', '~> 0.3.18'

采纳答案by Richard Peck

Just a further update - the solution in the question is correct.

只是进一步更新 - 问题中的解决方案是正确的。

The 4th comment is worth taking note of:

第 4 条评论值得注意:

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

这不是 mysql2 的错误,而是 ActiveRecord 适配器中的要求有问题:http: //github.com/rails/rails/issues/21544。这在 rails master 中已修复:https: //github.com/rails/rails/commit/5da5e3772c32593ecf2f27b8865e81dcbe3af692

I was able to tie Rails 4.2.4to the 4-2-stablebranch and get it working with the latest mysql2:

我能够绑定Rails 4.2.44-2-stable分支并让它与最新的一起工作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'

回答by Kaleem Ullah

Just do:

做就是了:

gem 'mysql2', '~> 0.3.18'this gem works with rails version 4.x.x

gem 'mysql2', '~> 0.3.18'这颗宝石与 rails version 4.x.x

if install gem 'mysql2', '~> 0.4.0'it produces gem load error and causes compatibility issues

如果安装gem 'mysql2', '~> 0.4.0'它会产生 gem 加载错误并导致兼容性问题

回答by Abram

This issue was addressed here: https://github.com/brianmario/mysql2/issues/950

此问题已在此处解决: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.

对于 Rails 4.x,请将 gem 固定到 mysql2 '~> 0.4.0' 以避免 0.5.x 升级。

Gemfile:

宝石档案:

gem 'rails', '4.2.8'
gem 'mysql2', '~> 0.4.0'

Then run bundle update rails mysql2

然后运行 bundle update rails mysql2

I am currently using mysql v 8.0.11

我目前正在使用mysql v 8.0.11

回答by Joshua F. Rountree

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 updateand your mysql2 version updates to 0.4.x which has an incompatibility issue with latest Rails ActiveRecord.

与发布时间和使用的 Rails 版本相关的这个特定问题的答案是,问题是由执行 a 引起的,bundle update并且您的 mysql2 版本更新到 0.4.x,它与最新的 Rails ActiveRecord 存在不兼容问题。

Again, please note this is NOT the solution for people using older versions of Rails / ActiveRecord.

同样,请注意这不是使用旧版本 Rails / ActiveRecord 的人的解决方案。

The quick solution is to simply specify the mysql2version in your gemfile as follows:

快速解决方案是简单地mysql2在您的 gemfile 中指定版本,如下所示:

gem 'mysql2', '0.3.20'

gem 'mysql2', '0.3.20'

The long solution would be to wait for either an update to ActiveRecord or something in mysql2 to change.

长期的解决方案是等待 ActiveRecord 的更新或 mysql2 中的某些内容更改。

回答by Omac

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:

如果您能够升级您的 rails 版本,那么将您的 Gemfile 更改为此,它将解决问题而无需降级 mysql2 gem 版本:

gem 'rails', '4.2.6'

回答by avinash v p

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'

进入工作目录的文件(“work”),然后从那里进入你创建的应用程序的文件(“sample_app”),其中安装了 mysql 2 转到 gem 文件(“Gemfile”)edit gem 'mysql2'to gem 'mysql2', '~> 0.3.18'

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

现在返回 cmd 并运行命令“bundle install”。

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

因此,考虑到括号中的上述文件,编辑文件的目录应该是 . “c\work\sample_app\Gemfile”

hope i was of some help .

希望我有所帮助。

回答by The F

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?

当您的机器上缺少某些 mysql 包时,通常会发生这种情况。你有任何错误gem install mysql2吗?您正在使用什么操作系统?

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

如果在 debian 或 ubuntu 上尝试sudo apt-get install libmysqlclient-dev

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

还要确保 gem 没有放在groupGemfile的语句中。

回答by Mr94

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

有同样的问题,但添加source 'https://rubygems.org'到 gem 文件的顶部解决了问题。

Steps:

脚步:

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

添加source 'https://rubygems.org'到您的 gem 文件中。注释掉mysql2 gem并添加gem 'mysql2', '~> 0.3.18'运行bundle install

回答by mmike

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.

此命令会将您的 'mysql2' gem 更新到最新版本(应该是 0.3.17 或更高版本)并启动您的 Rails 服务器。