错误"...无法加载此类文件 - mysql2/2.0/mysql2(LoadError)".在带有Ruby 2.0.0的Windows XP上

Gre*_*een 11 mysql2 ruby-on-rails-3 ruby-2.0

该命令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)
Run Code Online (Sandbox Code Playgroud)

我使用的红宝石2.0.0RubyInstaller在Windows XP的方块.

我弄清楚问题是什么,但我不知道如何解决它.问题是gem中没有任何2.0/目录mysql2-0.3.11-x86-mingw32.这是railsGemfile安装为其依赖项的gem:

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

这是该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
Run Code Online (Sandbox Code Playgroud)

很明显,它采用当前的Ruby版本号并将其用作路径段来访问某个mysql2文件.实际上它是mysql2.so文件.当我使用Ruby 2.0.0时,路径段是2.0:

mysql2/2.0/mysql2
Run Code Online (Sandbox Code Playgroud)

好的,现在让我们看看mysql2-0.3.11-x86-mingw32gem的目录如何:

dir: C:\Ruby200\lib\ruby\gems\2.0.0\gems\mysql2-0.3.11-x86-mingw32\lib\mysql2\
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

没有任何2.0/目录.

我知道libmysql.dll的问题.我有我的C:\Ruby200\bin.它没有帮助.

我从RubyInstaller的创建者那里读到了这个答案/sf/answers/375813721/.我试过但它没有帮助.它适用于Ruby 1.9.3因为有1.9/目录.但它不起作用Ruby 2.0.0.

怎么解决?


UPD 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...
Run Code Online (Sandbox Code Playgroud)

它说:

您必须先安装开发工具.

但我安装了完整的DevKit,RubyInstaller安装它.无法理解它还需要什么.

我已经向mysql2GitHub页面https://github.com/brianmario/mysql2/issues/364发布了一个问题.还没有答案.

odi*_*apc 13

在使用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"


Dej*_*i S 5

卸载mysql2-0.3.11-x86-mingw32,之前编译的gems Ruby200将无法正常工作.你必须自己编译.如果你运行gem uninstall mysql2并且列出了一个非编译版本,那么只需卸载mysql2-0.3.11-x86-mingw32- 编译后的其他版本......

安装 gem install mysql2 --platform=ruby.那会有用.

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

然后再试一次.