运行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/
目录。这是rails
从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://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。目前还没有答案。