【问题标题】:Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0错误“...无法加载此类文件 -- mysql2/2.0/mysql2 (LoadError)”。在带有 Ruby 2.0.0 的 Windows XP 上
【发布时间】:2013-03-14 07:11:44
【问题描述】:

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 机器上使用来自 RubyInstallerRuby 2.0.0

我知道是什么问题,但我不知道如何解决。问题是mysql2-0.3.11-x86-mingw32 gem 中没有任何2.0/ 目录。这是 rails 安装的 gem 作为 Gemfile 的依赖项:

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\

没有任何2.0/ 目录。

我知道关于 libmysql.dll 的问题。我在我的C:\Ruby200\bin 中有它。没用。

我从 RubyInstaller 的创建者那里读到了这个答案https://stackoverflow.com/a/5368767/1114926。我试过但没有帮助。它适用于Ruby 1.9.3,因为有1.9/ 目录。但它不适用于Ruby 2.0.0

如何解决?


UPD 1:

感谢您的answer。我试过了。不幸的是我有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 发布了一个问题。还没有答案。

【问题讨论】:

    标签: ruby-on-rails-3 mysql2 ruby-2.0


    【解决方案1】:

    在带有 Ruby 2.0.0 和 DevKit 4.7 的 Windows 7 x64 上遇到了完全相同的问题。

    以下步骤对我有帮助。

    1. gem 卸载 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编译版本...
    • 如果上述方法不起作用,请使用以下命令:- gem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\connector-6.0.2\lib" -- with-mysql-include="C:\connector-6.0.2\include"
    • @RohitPorwal 你的对我有用,但你缺少命令中的最后一个 ' ,即应该是: gem install mysql2 --platform=ruby -- '--with-mysql-lib ="C:\connector-6.0.2\lib" --with-mysql-include="C:\connector-6.0.2\include"'
    【解决方案2】:

    卸载mysql2-0.3.11-x86-mingw32,在Ruby200 之前编译的gem 将不起作用。你必须自己编译它。 如果您运行 gem uninstall mysql2 并且列出了未编译的版本,则只需卸载 mysql2-0.3.11-x86-mingw32 - 其他编译版本...

    安装 gem install mysql2 --platform=ruby。那行得通。

    按照本指南安装 devkit http://rubyonwindowsguides.github.com/book/ch02-04.html

    然后再试一次。

    【讨论】:

      【解决方案3】:

      当我尝试在 Windows XP 上安装 Redmine 时的行为完全相同

      • 当我尝试执行“rake generate_secret_token”时,我得到“无法加载 这样的文件 -- mysql2/2.0/mysql2 (LoadError)"
      • 当我尝试使用 --platform=ruby 重新构建 mysql2 时,我得到了 和你绿色的信息一样。

      目前,我被困在那里......但如果有人有什么可以让我继续前进,那将是非常受欢迎的。 我也会及时通知

      安托万

      【讨论】:

        【解决方案4】:

        我几乎完成了@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,将会再次安装 mingw 版本的 mysql2 gem。您必须始终“卸载”它...
        • 我必须做 gem install mysql2 --platform=ruby -- --with-opt-dir=/c/connector-6.0.2 --with-mysql-dir=/c/connector -6.0.2 ... 因为编译失败,我注意到有对 ${mysql-dir}/include 的引用。
        【解决方案5】:

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


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

        "找不到指定的模块。
        C:/Devel/Ruby200/lib/ruby/gems/2.0.0/gems/mysql2-0.3.11/lib/mysql2/mysql2.so"

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

        【讨论】:

          猜你喜欢
          • 2017-11-27
          • 2015-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-11
          • 2013-03-15
          相关资源
          最近更新 更多