Rails 4 - Gem::LoadError:为数据库适配器指定了“mysql2",但未加载 gem [英] Rails 4 - Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded

查看:45
本文介绍了Rails 4 - Gem::LoadError:为数据库适配器指定了“mysql2",但未加载 gem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 gemfile 中有:

gem 'mysql2'

我的database.yml如下:

默认:&default适配器:mysql2数据库:<%= ENV['db_name'] %>用户名:<%= ENV['db_user'] %>密码:<%= ENV['db_pass'] %>主机:<%= ENV['db_host'] %>池:32套接字:<%= ENV['socket'] %>发展:<<:*默认生产:<<:*默认

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

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

myproject.com(master)$ rake db:migrate警告:在 Figaro 配置中使用字符串.10000012508 被转换为10000012508".警告:在 Figaro 配置中使用字符串.860526407370038 被转换为860526407370038".耙子中止!Gem::LoadError: 为数据库适配器指定了mysql2",但未加载 gem.将`gem 'mysql2'` 添加到您的 Gemfile(并确保其版本为 ActiveRecord 要求的最低版本)......

为了确保没有错误版本的 mysql2 或其他东西,我做了 bundle clean --force 并运行 bundle installbundle update 再次运行 gem list 时,我看到 mysql2 (0.4.0) 而没有其他版本.

任何想法将不胜感激.

<小时>

解决方案

目前是 Rails 4.1.x 和 4.2.x 的问题,根据此

gem 'rails', '~>4.2.4', git: "git://github.com/rails/rails.git", 分支: '4-2-stable'gem 'tzinfo-data', 平台: [:mingw, :mswin, :x64_mingw] #->导轨 4.1+#D B宝石'mysql2'

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'

解决方案

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'

这篇关于Rails 4 - Gem::LoadError:为数据库适配器指定了“mysql2",但未加载 gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆