Ruby-on-rails mysql2 gem 的 Gem::LoadError,但它已经在 Gemfile 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22932282/
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 23:31:16  来源:igfitidea点击:

Gem::LoadError for mysql2 gem, but it's already in Gemfile

ruby-on-railsmysql2gemfile

提问by Mysoulisinseoul

Gem::LoadError
Specified 'mysql2' for database adapter, but the gem is not loaded.
Add `gem 'mysql2'` to your Gemfile

This error occurred while loading the following files:

加载以下文件时发生此错误:

active_record/base

This is the error I get on running rails server.

这是我在运行 rails 服务器时遇到的错误。

The mysql2 gem has been added to the Gemfile as well.

I've done bundle install, and tried restarting the server but still get the error.

我已经完成bundle install,并尝试重新启动服务器,但仍然出现错误。

回答by Hassan Azimi

If you have this error when upgrading to rails 4.2.4 (also with rails 4.1.5) try using this version of mysql2:

如果在升级到 rails 4.2.4(也有 rails 4.1.5)时遇到此错误,请尝试使用此版本的 mysql2:

gem 'mysql2', '~> 0.3.18'

Apparently mysql2 isn't still compatible with newer version of rails because rails 4.2.4 is pretty new as the time of answering this question by me 8 September 2015 so use the above line in your Gem file and run:

显然 mysql2 仍然与较新版本的 rails 不兼容,因为在我 2015 年 9 月 8 日回答这个问题时,rails 4.2.4 是相当新的,因此请在您的 Gem 文件中使用以上行并运行:

bundle install 

You should be good to go

你应该很高兴去

回答by rom5jp

It worked for me when I specified a mysql2 gem version before the newest one (0.4.0). For some reason there is a problem with Rails 4.2.4 and that gem 0.4.0. So, to solve the problem I just specified the previous gem released: 0.3.20 and it worked fine for me!

当我在最新版本(0.4.0)之前指定一个 mysql2 gem 版本时,它对我有用。出于某种原因,Rails 4.2.4 和 gem 0.4.0 存在问题。所以,为了解决这个问题,我刚刚指定了之前发布的 gem:0.3.20,它对我来说很好用!

gem 'mysql2', '~> 0.3.20'
bundle install

You can check all the gems versions here: https://rubygems.org/gems/mysql2/versions

您可以在此处查看所有 gems 版本:https: //rubygems.org/gems/mysql2/versions

回答by suga_shane

Change to

改成

gem 'mysql2', '~> 0.3.18'

in your Gemfile.

在你的 Gemfile 中。

This thread on the official mysql2 Github says to do this. You need to declare that version number if you're rails version 4.x.x.

官方mysql2 Github上的这个线程说要这样做。如果您是 rails 版本 4.xx,则需要声明该版本号

https://github.com/brianmario/mysql2/issues/675

https://github.com/brianmario/mysql2/issues/675

Then run bundle update mysql2.

然后运行bundle update mysql2

回答by Prabhakar Undurthi

This issue may occur if you're using newer version of rails > 4

如果您使用较新版本的 rails > 4,则可能会出现此问题

Do these two simple steps, it will work. Open your Gemfile and find the below line

做这两个简单的步骤,它会起作用。打开您的 Gemfile 并找到以下行

gem 'mysql2'

replace that line with a specific mysql version like below

用如下所示的特定 mysql 版本替换该行

gem 'mysql2', '~> 0.3.18'

Now stop the server and run bundle

现在停止服务器并运行 bundle

bundle install

Now restart your server. It should work.

现在重新启动您的服务器。它应该工作。

rails s

回答by valo

I got the same error after an upgrade to Rails 4.1 and I managed to resolve it by updating mysql2. Run this in your rails app folder:

升级到 Rails 4.1 后我遇到了同样的错误,我设法通过更新 mysql2 来解决它。在你的 rails app 文件夹中运行这个:

$ bundle update mysql2

回答by Muaaz salagar

Being Beginner to the ruby i could not figure out the line gem 'mysql2', '~> 0.3.18'

作为 ruby​​ 的初学者,我无法弄清楚 gem 'mysql2', '~> 0.3.18' 这一行

it simply means go to your rails project folder and then there is line for mysql2 it will be like 0.4* so you can change it to gem 'mysql2', '~> 0.3.18'

它只是意味着转到您的 rails 项目文件夹,然后有一行 mysql2 它将类似于 0.4*,因此您可以将其更改为 gem 'mysql2', '~> 0.3.18'

and as we have new definition, we have to rebuild the dependency so to do that simple command as explained on the top bundle install

并且由于我们有了新的定义,我们必须重建依赖项,以便按照顶部包安装中的说明执行该简单命令

回答by sushilprj

It doesn't load mysql2 gem because new version of mysql2(0.4.1) gem unable to load the mysql2_adaptor. This is working for me.

它不会加载 mysql2 gem,因为新版本的 mysql2(0.4.1) gem 无法加载 mysql2_adaptor。这对我有用。

gem 'mysql2', '~> 0.3.13'

and run

并运行

bundle install

回答by Lev Lukomsky

I had the same error and this is because Rails 4.1 requires minimum mysql2 version 0.3.13, and maximum compatible with Windows is version 0.3.11.

我遇到了同样的错误,这是因为 Rails 4.1 要求最低 mysql2 版本为 0.3.13,而与 Windows 兼容的最高版本为 0.3.11。

So I edited file c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\activerecord-4.1.1\lib\active_record\connection_adapters\mysql2_adapter.rband changed line gem 'mysql2', '~> 0.3.13'to gem 'mysql2', '~> 0.3.11', and it works so far.

所以我编辑了文件c:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\activerecord-4.1.1\lib\active_record\connection_adapters\mysql2_adapter.rb并将行更改gem 'mysql2', '~> 0.3.13'gem 'mysql2', '~> 0.3.11',到目前为止它可以工作。

回答by Ich

Here is how I fixed this:

这是我解决这个问题的方法:

 bundle config
 bundle config --delete without
 bundle install --deployment --without development test postgres

Credits: How do you undo bundle install --without

积分: 您如何撤消 bundle install --without

回答by Neeraj Kumar

It doesn't load mysql2 gem because new version of mysql2 (>= 0.4.0) gem unable to load the mysql2_adaptor. Can you try this?

它不会加载 mysql2 gem,因为新版本的 mysql2 (>= 0.4.0) gem 无法加载 mysql2_adaptor。你能试试这个吗?

gem 'mysql2', '~> 0.3.13'

gem 'mysql2', '~> 0.3.13'

Hopefully, it should work.

希望它应该起作用。