---
title: "Windows7下安装Ruby2的MySQL连接gem"
author: "kingxss"
date: "2014-02-11T12:08:33Z"
tags: []
categories: ["编程语言"]
slug: "windows7-ruby2-mysql-gem"
draft: false
lastmod: "2014-02-11T12:18:55Z"
id: 2014443
url: "https://kingxss.iteye.com/blog/2014443"
---

不得不感慨的是，学习一门语言其实是学习一个技术栈，包括语言的本身、基于该语言的常用组件、基于该语言的基础框架和该语言的常用场景示例。其实这一切都是很有趣的事情，但是学习这些有意思的东西时候肯定不止是看看书籍或文章那么简单，我们需要实践。实践的重要部分就是准备学习环境，对环境的熟悉程度实际上也很大程度的标识着对该技术栈的掌握程度。

        说了一堆废话，其实不就是在Windows7下装了个Ruby2的MySQL的连接gem嘛？但是真的是要感慨一下，被这东西折腾了3个多小时，极大的妨碍了学习Ruby的进程。

 

一 环境说明

     1 安装的环境

           1).Windows7       x64             Ultimate

           2).Mysql5.6         x64             5.6.10.1

           3).Ruby2             x64             2.0.0-p247

           4).DevKit             x64             4.7.2

 

     2 Ruby2.0和DevKit 安装说明

     2.1) 在ruby官网  [http://rubyinstaller.org/downloads/](/admin/blogs/%20http:/rubyinstaller.org/downloads/)  下载Ruby2.0和DevKit的安装包，比如：rubyinstaller-2.0.0-p247-x64.exe和DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe。

     2.2) rubyinstaller-2.0.0-p247-x64.exe直接双击运行，安装好可以在命令行用ruby -v检查是否安装成功，如果显示版本号就对了。

     2.3) 双击下载的7z文件:DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe，指定解压路径，路径中不能有空格。如C:\\DevKit，这个路径就是&lt;DEVKIT\_INSTALL\_DIR&gt;。

```
> cd <DEVKIT_INSTALL_DIR>
> ruby dk.rb init
#生成config.yml，这里会检查将要添加DevKit支持的Ruby列表，只支持通过RubyInstaller安装的Ruby
#如果这里列出的Ruby与你的要求不符，可以手动修改，注意配置参数 “ - C:/Ruby2”中-前后的空格
> ruby dk.rb review  #检查要添加DevKit支持的Ruby列表是否有误，可以略过
> ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby2'
[INFO] Installing 'C:/Ruby2/lib/ruby/site_ruby/devkit.rb'
```

   

   检查是否安装成功：

```
> gem install rdiscount --platform=ruby
Fetching: rdiscount-1.6.8.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
Successfully installed rdiscount-1.6.8
1 gem installed
Installing ri documentation for rdiscount-1.6.8...
Installing RDoc documentation for rdiscount-1.6.8...
```

   如果能安装rdiscount成功说明安装DevKit成功。最后需要将DevKit的&lt;DEVKIT\_INSTALL\_DIR&gt;\\bin加入到环境变量的Path中。

 

   具体参考：[https://github.com/oneclick/rubyinstaller/wiki/development-kit](https://github.com/oneclick/rubyinstaller/wiki/development-kit)   

     

二 mysql和mysql2安装过程

    1. 从mysql官网  [http://dev.mysql.com/downloads/connector/c/](http://dev.mysql.com/downloads/connector/c/%20)  下载mysql-connector-c-noinstall-6.0.2-winx64.zip，该包下的libmysql.dll 文件是MySQL的动态链接库文件，缺少此文件数据库无法正常工作。

          1.1) 这里需要下载6.0版本，将该文件解压到相应目录，例如：c:\\mysql-connector-c 。

          1.2) 从 c:\\mysql-connector-c\\lib 中拷贝libmysql.dll到ruby/bin文件夹

 

     2. 由于mysql-connector-c中的 libmysql.lib 不与mingw64-gcc编译器兼容，所以需要生成mingw64兼容libmysql.lib文件。

         具体原因参考：[https://bugs.ruby-lang.org/issues/8591](https://bugs.ruby-lang.org/issues/8591)

 

         下载需要的工具，主要是gendef.exe，这个应用从 [https://structure-svm-map.googlecode.com/files/svm-map-win.zip](https://structure-svm-map.googlecode.com/files/svm-map-win.zip) 下载。

          2.1) 解压该文件，拷贝gendef.exe到devkit/mingw/bin下。所以拷贝到这里，是因为dlltool.exe也在这里，都放到path里方便。

          2.2) 打开cmd， 进入 C:\\mysql-connector-c\\lib，依次运行如下命令：

```
#生成libmysql.def文件
gendef.exe libmysql.dll

#生成新的lib了
dlltool -v --dllname libmysql.dll --def libmysql.def --output-lib libmysql.lib
```

     

    3. gem安装mysql和mysql2

```
#mysql
gem install mysql --platform=ruby -- --with-opt-dir=C:/mysql-connector-c
#mysql2
gem install mysql2 --platform=ruby -- --with-mysql-dir=C:/mysql-connector-c
```

     安装过程过程中可能会碰到相应的问题，可以参考这里：

mysql安装参数问题：[http://stackoverflow.com/questions/17673612/installing-mysql-ruby-gem-in-windows-fails-using-ruby-2-0-0](http://stackoverflow.com/questions/17673612/installing-mysql-ruby-gem-in-windows-fails-using-ruby-2-0-0)

 

mysql2安装参数问题：[http://stackoverflow.com/questions/19014117/ruby-mysql2-gem-installation-on-windows-7](http://stackoverflow.com/questions/19014117/ruby-mysql2-gem-installation-on-windows-7)

 

编码问题“unable to convert "\\x90" from ASCII-8BIT to UTF-8 for lib/mysql/mysql\_api.so, skipping”：[http://stackoverflow.com/questions/4917859/ruby-error-utf-8-to-ascii](http://stackoverflow.com/questions/4917859/ruby-error-utf-8-to-ascii)