错误"...cannot load such file -- mysql2/2.0/mysql2 (LoadError)"。在Windows XP上使用Ruby 2.0.0版本时出现。

11

运行rails server命令时,会抛出这个错误。

C:/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require': cannot load
 such file -- mysql2/2.0/mysql2 (LoadError)

我在Windows XP上使用RubyInstaller安装的Ruby 2.0.0

我找出了问题所在,但不知道如何解决。问题是在mysql2-0.3.11-x86-mingw32宝石中没有任何2.0/目录。这是railsGemfile安装的依赖项。

GEM
  remote: https://rubygems.org/
  specs:
     ... many gems here
     mysql2 (0.3.11-x86-mingw32)
     ... many gems here
DEPENDENCIES
  ...
  mysql2
  ...

这就是 mysql2.rb 文件中的内容:

# C:\Ruby200\lib\ruby\gems\2.0.0\gems\mysql2-0.3.11-x86-mingw32\lib\mysql2\mysql2.rb

RUBY_VERSION =~ /(\d+.\d+)/
require "mysql2/#{$1}/mysql2"  # <<-- this is that #2 line that throws an error

很明显它获取当前的Ruby版本号并将其用作到达某个mysql2文件的路径段。实际上,它是mysql2.so文件。由于我使用Ruby 2.0.0,所以路径段为2.0

mysql2/2.0/mysql2

好的,现在让我们看一下 mysql2-0.3.11-x86-mingw32 gem 目录的样子:

dir: C:\Ruby200\lib\ruby\gems\2.0.0\gems\mysql2-0.3.11-x86-mingw32\lib\mysql2\

enter image description here

没有任何2.0/目录。

我知道关于libmysql.dll的问题。我把它放在了C:\Ruby200\bin中,但还是没有用。

我阅读了 RubyInstaller 的创建者在https://dev59.com/oW435IYBdhLWcg3whQc5#5368767上的回答。我尝试过,但并没有帮助。它对 Ruby 1.9.3有效,因为有1.9/目录。但对于Ruby 2.0.0则不行。

如何解决呢?


更新 1:

感谢你的答案。我已经尝试过了,但遗憾的是我收到了ERROR: Failed to build gem native extension.错误:

C:\>gem install mysql2 --platform=ruby
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

    C:/Ruby200/bin/ruby.exe extconf.rb
checking for rb_thread_blocking_region()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/Ruby200/bin/ruby
C:/Ruby200/lib/ruby/2.0.0/mkmf.rb:431:in `try_do': The compiler failed to generate an executable file. (Runtim
eError)
You have to install development tools first.
... other code follows here...

它说:

您首先必须安装开发工具。

但我已经安装了完整的DevKit,RubyInstaller会将其安装。不知道还需要什么。

我已经在mysql2的GitHub页面上发布了一个问题:https://github.com/brianmario/mysql2/issues/364。目前还没有答案。

5个回答

13

我在 Windows 7 x64 上使用 Ruby 2.0.0 和 DevKit 4.7 时也遇到了完全相同的问题。

以下步骤对我有帮助:

  1. gem uninstall mysql2

  2. http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip下载最新的 MySQL 连接器

  3. 将其解压到C:\connector-6.0.2目录下

  4. gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include" --with-mysql-dir="C:\connector-6.0.2"'

或者更简短地:

gem install mysql2 --platform=ruby -- --with-opt-dir="C:\connector-6.0.2"


好的,那行了!但是首先 - 你需要使用 cmd.exe ,而不是powershell!其次 - 有时候你需要在 Gemfile 中强制指定gem版本,例如0.3.13,因为bundler会安装0.2.6-mingw编译版本... - Gobol
1
如果上述方法无效,请使用以下命令: gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include"' - RohitPorwal
1
@RohitPorwal 你的方法对我有用,但是你在命令中漏掉了最后一个单引号,即应该是:gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include"'。 - davej

5
卸载mysql2-0.3.11-x86-mingw32,早于Ruby200的编译gems不起作用,你需要自己编译。如果运行gem uninstall mysql2时只有未编译版本,则卸载mysql2-0.3.11-x86-mingw32 - 已编译版本。 安装gem install mysql2 --platform = ruby即可成功。 按照这个指南安装devkit http://rubyonwindowsguides.github.com/book/ch02-04.html,然后再试一次。

2

当我试图在Windows XP上安装Redmine时,出现了完全相同的问题。

  • 当我尝试执行“rake generate_secret_token”时,会出现“cannot load such file -- mysql2/2.0/mysql2 (LoadError)”的错误提示。
  • 当我尝试使用--platform=ruby重新构建mysql2时,我得到和你一样的错误提示。

目前,我被卡住了......但如果有人能够帮我解决这个问题,那将非常受欢迎。

Antoine


2

从该链接中复制粘贴:https://github.com/brianmario/mysql2/issues/359。这对我有用。


如果在安装gem之后出现错误:

"The specified module could not be found.
C:/Devel/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.3.11/lib/mysql2/mysql2.so"

可以通过将libmysql.dll复制到Ruby的bin文件夹中来解决。


2

我几乎做了和@odiszapc描述的一样的事情。卸载旧的gem,下载mysql-connector-c-noinstall,解压缩,但最后我使用以下命令来构建本地的gem:

gem install mysql2 --platform=ruby -- --with-opt-dir=/c/connector-6.0.2

(如果您使用git-bash,则可以使用类Unix的目录语法。)

请注意,如果您运行bundle install或bundle update,则会再次安装mysql2 gem的mingw版本。 您必须始终执行'gem uninstall'操作... - Zoltan
1
我不得不执行 gem install mysql2 --platform=ruby -- --with-opt-dir=/c/connector-6.0.2 --with-mysql-dir=/c/connector-6.0.2 ... 因为编译失败并且我注意到有引用 ${mysql-dir}/include。 - Dalibor Filus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接