Site Loader

最近想要開始玩玩Ruby on Rails,花了一番時間才把他跟我原本既有的AppServ環境整合。
雖然網路上大部分的訊息都透露RoR對Windows系統不是很友善,
但我還是希望可以在平常用的PC上作一些簡單的測試和開發,所以…..折騰了不少時間。

以下是我的PC環境:
Windows XP 32-bit
AppServ 2.5.10 (PHP 5.2.6, Apache 2.2.8, MySQL 5.0.51b)
Ruby on Rails (RailsInstaller 2.1: Ruby 1.9.3, Rails 3.2)


= gem MySQL module =

要安裝ruby的mysql module,
首先必須把C:\Windows\system32\libmySQL.dll
複製到C:\RailsInstaller\Ruby1.9.3\bin\,然後在cmd下指令:

C:\>gem install mysql

成功以後會有下列訊息:

Fetching: mysql-2.8.1-x86-mingw32.gem (100%)
Successfully installed mysql-2.8.1-x86-mingw32
1 gem installed
Installing ri documentation for mysql-2.8.1-x86-mingw32...
Installing RDoc documentation for mysql-2.8.1-x86-mingw32...

如果使用以下參數就不會在本機產生文件:

C:\>gem install mysql --no-rdoc --no-ri

基本上這樣子就已經可以跟本機的MySQL連線了,我們進入 irb 做測試:

C:\>irb
irb(main):001:0> require 'mysql'
=> true
irb(main):002:0> conn = Mysql.new('localhost', 'admin', 'admin', 'test')
=> #
irb(main):003:0> rs = conn.query('select * from test')
=> #
irb(main):004:0> rs.each_hash { |h| puts h['id'] }
...data...
irb(main):005:0> conn.close

= gem MySQL2 module =

但是要安裝比較新也比較快速的mysql2 module就沒那麼容易了,
當你嘗試用同樣的命令做安裝:

gem install mysql2

看似正常的安裝完以後會給你一個版本相容的提示:

Fetching: mysql2-0.3.11-x86-mingw32.gem (100%)
=========================================================================
You've installed the binary version of mysql2. It was built using MySQL
Connector/C version 6.0.2. It's recommended to use the exact same version
to avoid potential issues.

At the time of building this gem, the necessary DLL files where available
in the following download:

http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick

And put lib\libmysql.dll file in your Ruby bin directory, for example
C:\Ruby\bin
=========================================================================
Successfully installed mysql2-0.3.11-x86-mingw32
1 gem installed
Installing ri documentation for mysql2-0.3.11-x86-mingw32...
Installing RDoc documentation for mysql2-0.3.11-x86-mingw32...

而之後進入 irb 做測試的時候,系統就會出現錯誤了,
告訴你這個 binary module 與 library 的版本不合:

irb(main):001:0> require 'mysql2'
RuntimeError: Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the client library is 5.0.51a.

若我們按照提示所說的,下載mysql connector 6.0.2的lib回來使用,
則會在與資料庫連線的時候出錯:

irb(main):002:0> require 'mysql2'
=> true
irb(main):003:0> Mysql2::Client.new(:host => "localhost", :username => "admin", :password => "admin")
Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
from (irb):2:in `new'
from (irb):2
from C:/RailsInstaller/Ruby1.9.3/bin/irb:12:in `main

從網路上資料研判,應該是mysql connector 6.0.2無法支援連線到5.0的db所造成的。
這時候只好用compile的方式來產生mysql2 module,
但是因為 AppServ 中的 mysql 沒有 header file,所以也無法編譯成功:

C:\>gem install mysql2 --platform=ruby -- --with-mysql-dir=C:\AppServ\mysql --with-mysql-lib=C:\Windows\system32
Fetching: mysql2-0.3.11.gem (100%)
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:/RailsInstaller/Ruby1.9.3/bin/ruby.exe extconf.rb --with-mysql-dir=C:\
AppServ\mysql --with-mysql-lib=C:\Windows\system32
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for main() in -llibmysql... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. please check your installation of mysql and try again.
-----
*** extconf.rb failed ***

於是我們到MySQL官網下載 MySQL 5.0的zip版本(mysql-noinstall-5.0.51b-win32.zip),解壓縮以後再重新 compile,這次就可以啦!(別忘紀要把 mysql-5.0.51b-win32\lib\opt\libmysql.dll 複製到 ruby\bin 底下,基本上與system32\下的應該是同一版本)

C:\>gem install mysql2 --platform=ruby -- --with-mysql-dir=C:\AppServ\mysql-5.0.51b-win32 --with-mysql-lib=C:\AppServ\mysql-5.0.51b-win32\lib\opt
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysql2-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...

到 irb 做測試:

irb(main):024:0> require 'mysql2'
=> false
irb(main):025:0> conn = Mysql2::Client.new(:host => "localhost", :username => "admin", :password => "admin", :database=>"test")
=> #
irb(main):026:0> result = conn.query("select id from test")
=> #
irb(main):027:0> result.each do |row|
irb(main):028:1* puts row
irb(main):029:1> end
...data...

大功告成!

若要在project裡面使用mysql,記得要在Gemfile新增module:

gem 'mysql2', '0.3.11'

也要修改database.yml中的adapter設定:

development:
adapter: mysql2
database: test
username: admin
password: admin
pool: 5
timeout: 5000

然後再執行:

C:\AppServ\www\ruby\test> bundle install
C:\AppServ\www\ruby\test> bundle exec rake db:migrate

就可以囉!

延伸閱讀:
[RoR] AppServ 2.5.10 (Apache 2.2.8) 整合 Ruby on Rails (1.9.3)

參考資料:
https://github.com/brianmario/mysql2/issues/256
http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/
http://stackoverflow.com/questions/5411551/what-the-difference-between-mysql-and-mysql2-gem
http://stackoverflow.com/questions/8740868/mysql2-gem-compiled-for-wrong-mysql-client-library
http://stackoverflow.com/questions/5367563/unable-to-install-mysql2-gem-on-windows-7
http://stackoverflow.com/questions/6588674/what-does-bundle-exec-rake-dbmigrate-mean
http://rubylearning.com/blog/2007/05/14/ruby-mysql-tutorial/

Post Author: starshine