Rails3でMySQL

Rails3にしてからMySQLへの接続ができなくなっていたので調べてみた。
取りあえず、動くようになったのでメモです。


database.ymlを変更

adapterをmysqlからmysql2に変更。

development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000
  adapter: mysql2
  database: test1_development
  username: hedgehog
  password: hogehoge
  host: localhost
  encoding: utf8

Gamfileにmysql2を追加

gem 'mysql2'

bundleを実行

$ bundle
Fetching source index for http://rubygems.org/
/Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler.rb:207: warning: Insecure world writable dir /usr/local in PATH, mode 040777
Using rake (0.9.2.2) 
Using multi_json (1.0.3) 
Using activesupport (3.1.1) 
Using builder (3.0.0) 
Using i18n (0.6.0) 
Using activemodel (3.1.1) 
Using erubis (2.7.0) 
Using rack (1.3.5) 
Using rack-cache (1.1) 
Using rack-mount (0.8.3) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.0.3) 
Using actionpack (3.1.1) 
Using mime-types (1.17.2) 
Using polyglot (0.3.3) 
Using treetop (1.4.10) 
Using mail (2.3.0) 
Using actionmailer (3.1.1) 
Using arel (2.2.1) 
Using tzinfo (0.3.30) 
Using activerecord (3.1.1) 
Using activeresource (3.1.1) 
Using ansi (1.4.0) 
Using bundler (1.0.12) 
Using coffee-script-source (1.1.2) 
Using execjs (1.2.9) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using json (1.6.1) 
Using rdoc (3.11) 
Using thor (0.14.6) 
Using railties (3.1.1) 
Using coffee-rails (3.1.1) 
Using jquery-rails (1.0.16) 
Installing mysql2 (0.3.7) with native extensions 
Using rails (3.1.1) 
Using sass (3.1.10) 
Using sass-rails (3.1.4) 
Using sqlite3 (1.3.4) 
Using turn (0.8.3) 
Using uglifier (1.0.4) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

scaffoldするとエラーになる

/Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2.rb:9:in `require': dlopen(/Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2.rb:9:in `<top (required)>'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:in `require'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:in `each'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:in `block in require'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:in `each'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:in `require'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler.rb:120:in `require'
	from /Users/kuru/Documents/00_Projects/_dev01/01.atotok_webapp/study/test1/config/application.rb:7:in `<top (required)>'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.1/lib/rails/commands.rb:21:in `require'
	from /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.1/lib/rails/commands.rb:21:in `<top (required)>'
	from script/rails:6:in `require'
	from script/rails:6:in `<main>'

ライブラリの install name を書き換える

$ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle

db:migrateするとエラーなる

$ rake db:migrate
/Users/kuru/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /usr/local in PATH, mode 040777

rake aborted!
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

database.ymlを修正

database.ymlのhostをlocalhostから127.0.0.1に修正し、rake db:migrateすると正常に実行できた

修正前のdatabase.yml
  host: loaclhost
修正後のdatabase.yml
  host: 127.0.0.1

開発環境

Mac OS X 10.7.2 Lion
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin11.0.0]
Rails 3.1.1





Atotok note Rails Wiki