Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

The command rails server throws this error.

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)

I use Ruby 2.0.0 from RubyInstaller on Windows XP box.

I figured out what is the problem but I don't know how to solve it. The problem is that there is no any 2.0/ directory in the mysql2-0.3.11-x86-mingw32 gem. This is the gem that rails installs as its dependency from Gemfile:

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

This is what is in that mysql2.rb file:

# C:Ruby200lib
ubygems2.0.0gemsmysql2-0.3.11-x86-mingw32libmysql2mysql2.rb

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

It is obvious that it takes the current Ruby version number and uses it as the path segment to reach some mysql2 file. In fact it is mysql2.so file. As I use Ruby 2.0.0 the path segment is 2.0:

mysql2/2.0/mysql2

Ok, now lets see how that directory of mysql2-0.3.11-x86-mingw32 gem looks like:

dir: C:Ruby200lib
ubygems2.0.0gemsmysql2-0.3.11-x86-mingw32libmysql2

enter image description here

No any 2.0/ directory.

I know the issue about libmysql.dll. I have it in my C:Ruby200in. It doesn't help.

I read this answer https://stackoverflow.com/a/5368767/1114926 from the creator of RubyInstaller. I tried but it didn't help. It works for Ruby 1.9.3 because there is 1.9/ directory. But it doesn't work for Ruby 2.0.0.

How to solve it?


UPD 1:

Thank you for your answer. I've tried. Unfortunately I have ERROR: Failed to build gem native extension. error:

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...

It says:

You have to install development tools first.

But I have full DevKit installed, RubyInstaller installs it. Can't understand what else does it need.

I've already posted an issue to mysql2 GitHub page https://github.com/brianmario/mysql2/issues/364. No answers yet.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Had the absolutely same issue on Windows 7 x64 with Ruby 2.0.0 and DevKit 4.7.

The following steps helped me.

  1. gem uninstall mysql2

  2. Download last MySQL connector from http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip

  3. Extract it to C:connector-6.0.2

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

Or even shorter:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...