在windows下安装mysql2 的问题解决 兰猫

今天向在自己的win7上配置ruby on rails 环境,结果被mysql2 和 gcc 的问题给卡住了

我的环境

 Ruby 1.9.2 and Rails 3.1.0 on Windows7

先看错误提示

bundle install

Errors:

....
Using jquery-rails (1.0.13)
Installing mysql2 (0.3.7) C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/defaults/
operating_system.rb:9:in `block in <top (required)>': The 'mysql2' native gem re
quires installed build tools. (Gem::InstallError)

Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
        from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:141:in `c
all'
....

找了不少方法,有copy mysql2.dll的, 有安装gcc的, 下面是从google翻出来的东西, 经过实践, 完美解决问题

1. Download Development Kit "http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe" from http://rubyinstaller.org/downloads/
2. Followed the instructions given on linkhttps://github.com/oneclick/rubyinstaller/wiki/Development-Kit
3. Extracted the downloaded DevKit.
4. cd <DEVKIT_INSTALL_DIR>
5. run ruby dk.rb init.

C:\Users\ritesh\Downloads\DevKit-tdm-32-4.5.1-20101214-1400-sfx>ruby dk.rb init
[INFO] found RubyInstaller v1.9.2 at C:/Ruby192
[INFO] found RubyInstaller v1.8.7 at C:/Ruby187
 
Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all

of the installed Rubies you want enhanced by the DevKit. 

6. edit the generated config.yml file as per your system available Rubies version if required
7. [optional] ruby dk.rb review to verify the changes you made to it are correct.

8. finally, ruby dk.rb install to DevKit enhance your installed Rubies.


C:\Users\ritesh\Downloads\DevKit-tdm-32-4.5.1-20101214-1400-sfx>ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby192'
[INFO] Installing 'C:/Ruby192/lib/ruby/site_ruby/devkit.rb'
[INFO] Skipping existing gem override for 'C:/Ruby187'
[WARN] Skipping existing DevKit helper library for 'C:/Ruby187'


9. You have successfully installed the DevKit, and now comes the step to install mysql2 gem, most important one for which we took so many steps. Make sure you are using Ruby 1.9.2 version.

10. Run gem install myql2 with parameters that I have passed, path can differ for you so make changes accordingly.

>gem install mysql2 -- '--with-mysql-lib="c:\Program Files\MySQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL Server 5.5\include"'
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed mysql2-0.2.7
1 gem installed
Installing ri documentation for mysql2-0.2.7...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.7...
Enclosing class/module 'mMysql2' for class Client not known


注意,上面的命令中, MySQL Server 5.5是我的mysql版本, 需要视情况个人自己替换。

然而我们没有那么顺利, 上面的命令行出错了, 如下:

C:\Users\Ivan\Downloads\dev>gem install mysql2 -- '--with-mysql-lib="c:\Program
Files\MySQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MyS
QL\MySQL Server 5.5\include"'
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:/Ruby192/bin/ruby.exe extconf.rb --with-mysql-lib="c:\Program Files\My
SQL\MySQL Server 5.5\lib\opt" --with-mysql-include="c:\Program Files\MySQL\MySQL
 Server 5.5\include"
checking for rb_thread_blocking_region()... yes
checking for main() in -llibmysql... no
*** 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:/Ruby192/bin/ruby
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include=${mysql-dir}/include
        --with-mysql-lib=${mysql-dir}/lib
        --with-libmysqllib
        --without-libmysqllib


Gem files will remain installed in C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.
3.7 for inspection.
Results logged to C:/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.3.7/ext/mysql2/ge
m_make.out

 哈哈,不用担心,经验是要分享的,翻遍了google终于发现了有用的命令, 如下

subst X: "C:\Program Files\MySQL\MySQL Server 5.5" 
gem install mysql2 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt 
subst X: /D
如果这里依然出错,

    ERROR: Could not find a valid gem 'mysql2' (>= 0) in any repository
    ERROR: Possible alternatives: mysql2

 建议到google把mysql2.dll 下载下来放入ruby的安装目录下就可以解决问题

如果是想指定版本号的话, 加个参数 -v=0.2.13
gem install mysql2 -v=0.2.13 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt
subst X: /D

安装成功

C:\Users\Ivan\RubymineProjects\eco_apps_test\pet>gem install mysql2 --platform=r
uby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib\opt
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.7
1 gem installed
Installing ri documentation for mysql2-0.3.7...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.3.7...
Enclosing class/module 'mMysql2' for class Client not known

搞定, 收工, 折腾了一个上午.

原文地址:https://www.cnblogs.com/ilazysoft/p/2171209.html