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
お名前.comのVPSでRails3を動かす - (1) rubyのバージョンアップ
お名前.comのVPS
http://www.onamae.com/server/vps/
CentOS release 5.3 (Final)
メモリ: 基本512MB 最大1GB
お名前.comのVPSで動作しているrubyとRailsのバージョンを確認する。
-bash-3.2$ ruby -v ruby 1.8.5 (2006-08-25) [x86_64-linux] -bash-3.2$ rails -v Rails 2.1.1
ruby ruby 1.8.5とRails 2.1.1が動いている。
これでもRailsを動かすことは出来るが、せっかくなのでRails3を動かしたいので
rubyは1.9に変更し、Rails3をインストールすることにする。
ruby 1.9.3-p0にバージョンアップする
最新版のrubyを ftp://ftp.ruby-lang.org からダウンロードする。
現時点の最新版は、ruby 1.9.3-p0
-bash-3.2$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz -bash-3.2$ tar xfvz ruby-1.9.3-p0.tar.gz
rubyのバージョンが、1.9.3に変わっていることを確認
$ ruby -v ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
お名前.comのVPSでRails3を動かす - (2) railsをインストール
Rails3をインストール
$ su # gem install rails
Rails3がインストールされたがワーニングがる。
取りあえず、後で対応することにする。
gem install railsでのワーニング
It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby.
Railsのバージョンを確認すると3.1.1に変わっている<<<
[root@atotok work]# rails -v
Rails 3.1.1
rails new test1を実行するとプロジェクトは作成されるがエラーになってしまう。
/usr/local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>': It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby.yamlをインストール
yamlをhttp://pyyaml.org/wiki/LibYAMLからダウンロード
$ wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz $ tar xvfz yaml-0.1.4.tar.gzyamlをインストール
$ cd yaml-0.1.4 $ ./configure $ make $ su # make installrubyを再インストール
# cd ruby-1.9.3-p0 # make clean # ./configure # make # make installこれでrails newしてもYAMLのエラーは出なくなっったが、別のエラーが
statement.c:52: 警告: implicit declaration of function ‘sqlite3_prepare_v2’ statement.c: In function ‘bind_param’: statement.c:268: error: ‘sqlite3_int64’ undeclared (first use in this function) statement.c:268: error: (Each undeclared identifier is reported only once statement.c:268: error: for each function it appears in.) statement.c:268: error: expected expression before ‘long’ make: *** [statement.o] エラー 1 Gem files will remain installed in /usr/local/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.4 for inspection. Results logged to /usr/local/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.4/ext/sqlite3/gem_make.out An error occured while installing sqlite3 (1.3.4), and Bundler cannot continue. Make sure that `gem install sqlite3 -v '1.3.4'` succeeds before bundling.
お名前.comのVPSでRails3を動かす - (3) sqliteをアップデート
お名前.comのVPSでRails3を動かす - (4) アプリケーションを実行するとエラー
rails sでアプリケーションを実行するとエラーになってしまう。
# rails s /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.9/lib/execjs/runtimes.rb:47:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.9/lib/execjs.rb:5:in `<module:ExecJS>' from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.9/lib/execjs.rb:4:in `<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.1.1/lib/coffee-rails.rb:1:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.1.1/lib/coffee-rails.rb:1:in `<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require' from /var/www/vhosts/00_default/user/work/test5/config/application.rb:7:in `<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:52:in `require' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:52:in `block in <top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:49:in `tap' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:49:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
execjsとtherubyracerをインストール
プロジェクトのGemfileに下記の行を追加
gem 'execjs' gem 'therubyracer'
インストール
# bundle install
この状態でrails sを実行すると正常に起動
rails s => Booting WEBrick => Rails 3.1.1 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2011-11-06 21:33:21] INFO WEBrick 1.3.1 [2011-11-06 21:33:21] INFO ruby 1.9.3 (2011-10-30) [x86_64-linux] [2011-11-06 21:33:21] INFO WEBrick::HTTPServer#start: pid=11268 port=3000 [2011-11-06 21:33:24] INFO going to shutdown ... [2011-11-06 21:33:24] INFO WEBrick::HTTPServer#start done.
これで、rails s でアプリケーションが正常に起動できるようになった。