【问题标题】:Ruby on Rails - cannot load such file -- mysql2/2.2/mysql2 (LoadError)Ruby on Rails - 无法加载此类文件 -- mysql2/2.2/mysql2 (LoadError)
【发布时间】:2015-05-09 15:25:11
【问题描述】:

我在 Rails 项目中使用 SQLite,但当我听说 Heroku 不支持 SQLite 时,我切换到了 MYSQL。

切换数据库后,我启动了 Rails 服务器“rails s”,但出现以下错误:

    C:\Sites\simple_cms>rails server
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2:in `require'
: cannot load such file -- mysql2/2.2/mysql2 (LoadError)
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2
:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `r
equire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `<
top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `bloc
k (2 levels) in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `bloc
k in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler.rb:134:in `require'
        from C:/Sites/simple_cms/config/application.rb:7:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `block in server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `tap'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:39:in `run_command!'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top
 (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

这是我的 gemfile:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use mysql as the database for Active Record
gem 'mysql2'

我彻底搜索,找到了一些答案,但没有奏效。

我将libmysql.dll 文件从Program Files/MySQL Server 5.6 粘贴到Ruby/bin,但仍然没有运气。我执行了以下命令:

gem uninstall mysql2

gem install mysql2 --platform=ruby

我按照本教程进行操作,但仍然遇到同样的错误:Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

如果有人能帮助我,我将不胜感激,以便我可以将其部署到 Heroku。

谢谢。

【问题讨论】:

  • 遇到同样的错误 - 修复后会报告。

标签: mysql ruby-on-rails sqlite heroku


【解决方案1】:

问题是mysql2 gem 没有安装其本机依赖项。在之前的 Ruby 版本中,这会触发 unable to build native gem extension 错误:

Ruby 2.2 中发生了一些变化,因此您可以安装 gem 而无需构建原生扩展。然而,这并不意味着问题已解决 - 因此您必须确保安装具有适当依赖项的 gem。

Tutorial:

  1. Download C-Connector from MYSQL(注意您的 Ruby 版本——如果您使用 64 位,请下载 64 位版本)

  2. 解压/安装 MYSQL C-Connector 文件夹到你的硬盘(路径中不要有空格)

  3. 运行以下命令:gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'

  4. 这应该安装 gem 和它需要的文件

  5. 然后你可以从C:/your-mysql-connector-path复制libmysql.dll -> c:/your-ruby-install/bin`

本次安装与上一次的区别在于,本次应该提到正在安装本机 gem 扩展 (This could take a while),这表明 Ruby 正在查看该库。

这一次,如果它安装正确,它应该适合你。

--

如果您仍然看到错误,则必须卸载系统上所有现有版本的 mysql2 gem:

如果遇到错误,只需输入gem uninstall mysql2,选择all,然后重复上述步骤即可。

【讨论】:

  • 尝试使用此方法,但它会返回:无效选项:--with-mysql-dir=C:
  • 您只需按照我的回答将路径封装在引号中
  • 无论引号如何,我都会得到“无效选项”。总的来说,答案也根本不会改变。
  • 可以发截图吗?
  • 我也有无效选项错误。屏幕截图:postimg.org/image/vb1rsm713 谢谢!
【解决方案2】:

在上面的第 3 步中,我不得不使用额外的一对 '-' 并且路径周围没有引号。

gem install mysql2 -- --with-mysql-dir=C:\mysql-connector-dir

我正在使用 Win 7

【讨论】:

    【解决方案3】:

    从 Ruby 2.1.5 更新到 2.2.1 后,我遇到了同样的问题。问题似乎是 Rails 使用的 mysql2 gem 绑定到 libmysql。显然它不支持 Ruby 2.2 分支,只支持 2.0 和 2.1。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,所以我改用 RubyInstaller。我不再有问题了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        • 2013-03-14
        • 2020-10-12
        • 2018-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多