<<目的>>
・Rails アプリケーションからMySQLへ接続する。

<<ソフトおよびバージョン>>
・Ruby on Rails 3.2.13
・MySQL 5.5.27 (xampp 1.8.1)
・mysql-connector-c 6.0.2


<<やりかた>>
・Gemfileの編集
 ・アプリケーションディレクトリの直下にあるGemfileをテキストエディタで開く。
 ・以下の行を追加。
   gem 'mysql2'


・各種コマンドの実行
 ・コマンドプロンプトを管理者で起動し、アプリケーションディレクトリに移動し、
  以下を実行。
   gem install mysql2
   gem install activerecord-mysql2-adapter
   bundle install


・configの編集
 ・アプリケーションディレクトリのconfigディレクトリにあるdatabase.ymlを
  テキストエディタで開く。
 ・以下のように編集。
  development:
    adapter: mysql2
    encoding: utf8
    database: blog_development
    pool: 5
    username: root
    password:
    socket: /tmp/mysql.sock

    
・rakeの実行
 ・コマンドプロンプトを管理者で起動し、アプリケーションディレクトリに移動し、
  以下を実行。
   rake db:create
   
 ・以下のようなエラーが発生する。
 
rake aborted!
Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the
client library is 5.5.27.
c:/work/ruby/rails/blog/config/application.rb:7:in `<top (required)>'
c:/work/ruby/rails/blog/Rakefile:5:in `<top (required)>'
(See full trace by running task with --trace)


・mysql-connector-c のインストール
  ・http://www-jp.mysql.com/downloads/connector/c/から
   mysql-connector-c-6.0.2-noinstall-win32
    をダウンロード。適当なディレクトリに解凍。
 ・解凍ディレクトリのlibディレクトリにある、libmysql.dllを
  Rubyのインストールディレクトリのbinディレクトリにコピー。


・rakeの再実行
 ・コマンドプロンプトを管理者で起動し、アプリケーションディレクトリに移動し、
  以下を再実行。
   rake db:create
    エラーが発生しないことを確認。


・mysqlの確認
 ・phpmyadminなどでmysqlに接続し、blog_developmentが作成されていることを確認。

Installing json (1.7.7)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension
.

C:/usr/local/Ruby/bin/ruby.exe extconf.rb
creating Makefile

make
generating generator-i386-mingw32.def
compiling generator.c
In file included from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/ruby.h:1381:0,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby.h:32,
from ../fbuffer/fbuffer.h:5,
from generator.c:1:
c:/usr/local/Ruby/include/ruby-1.9.1/ruby/missing.h:41:8: error: redefinition of
'struct timespec'
In file included from c:\usr\local\devkit\mingw\bin\../lib/gcc/i686-w64-mingw32/
4.7.2/../../../../i686-w64-mingw32/include/process.h:12:0,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/win32.h:62,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/defines.h:223,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/ruby.h:67,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby.h:32,
from ../fbuffer/fbuffer.h:5,
from generator.c:1:
c:\usr\local\devkit\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/../../../../i686
-w64-mingw32/include/sys/types.h:89:8: note: originally defined here
In file included from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/ruby.h:1381:0,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby.h:32,
from ../fbuffer/fbuffer.h:5,
from generator.c:1:
c:/usr/local/Ruby/include/ruby-1.9.1/ruby/missing.h:48:8: error: redefinition of
'struct timezone'
In file included from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/win32.h:63:0,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/defines.h:223,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby/ruby.h:67,
from c:/usr/local/Ruby/include/ruby-1.9.1/ruby.h:32,
from ../fbuffer/fbuffer.h:5,
from generator.c:1:
c:\usr\local\devkit\mingw\bin\../lib/gcc/i686-w64-mingw32/4.7.2/../../../../i686
-w64-mingw32/include/time.h:260:8: note: originally defined here
make: *** [generator.o] Error 1


Gem files will remain installed in C:/usr/local/Ruby/lib/ruby/gems/1.9.1/gems/js
on-1.7.7 for inspection.
Results logged to C:/usr/local/Ruby/lib/ruby/gems/1.9.1/gems/json-1.7.7/ext/json
/ext/generator/gem_make.out
An error occurred while installing json (1.7.7), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.7'` succeeds before bundling.

<<目的>>
・Ruby on Rails アプリケーションを付属のサーバー環境で実行する。


<<ソフトおよびバージョン>>
・Ruby 1.9.3
・Ruby on Rails 3.2.13
・Windows 7


<<やりかた>>
・Ruby のインストール
 ・http://rubyinstaller.org/downloads/ から
   RubyInstallers Ruby 1.9.3-p392
をダウンロード。


 ・インストーラの指示に従いインストール。
  オプションの指定では「Ruby の実行ファイルへ環境変数PATHを設定する」に
  チェックをつけておく。チェックし忘れた場合は手動でPATHを設定すること。


・DEVELOPMENT KIT のインストール
 ・http://rubyinstaller.org/downloads/ から
   DEVELOPMENT KIT DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe
をダウンロード。任意のディレクトリに解凍。
  ※解凍時、途中で止まっているように見えることがあるが、解凍は進んでいるので
   最後まで待つこと。
 ・コマンドプロンプトを管理者で起動し、解凍ディレクトリに移動し、以下を実行。
   ruby dk.rb init
ruby dk.rb install


・Ruby on Rails のインストール
 ・コマンドプロンプトを管理者で起動し、以下を実行。
   gem install rails
※実行時、途中で止まっているように見えることがあるが、実行は進んでいるので
   最後まで待つこと。
  ※途中で
    unable to convert ...
file '...' not found
などと表示されることがあるが無視してかまわない。


・Rails アプリケーションの生成
・コマンドプロンプトを管理者で起動し、アプリケーションを作成したいディレクトリ
  に移動し、以下を実行。
   rails new xxx
※xxxはアプリケーション名。
 ・この時、以下のようなエラーになるはずである。エラーにならなければ以下を
  スキップしてかまわない。


rails 実行時のエラー


 ・rubyのインストールディレクトリのinclude/ruby-1.9.1/rubyディレクトリにある
   missing.hをテキストエディタで開き、2つの構造体timespec, timezoneを
コメントアウトする。

 ・再度、以下のコマンドを実行し、エラーがなくなることを確認する。
   rails new xxx
※Overwrite ... ? と聞かれたら、"a"と入力し、すべて上書きすること。


・Rails アプリケーションの起動
 ・コマンドプロンプトを管理者で起動し、アプリケーションディレクトリに移動し、
  以下を実行。
   rails server -b localhost


・動作確認
 ・以下のURLにアクセスして正しく表示されることを確認。
   http://localhost:3000/