次に、Railsのプロジェクトを作成してみます。
RailsからMySQLを使用する場合、
# 使用するデータベースとしてmysqlを指定 $ rails new sample --database=mysql
ちなみに、データベースを指定しない場合はSQLiteになります。
プロジェクトが作成されたら、config/database.ymlを編集して(Host, User, Passwordを指定)、 実際に起動してみます
# サーバー起動 $ cd sample $ rails server
するとこんなエラーが。。。。
/Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2.rb:9:in `require': dlopen(/Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError) Referenced from: /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle Reason: image not found - /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2.rb:9:in `' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:68:in `require' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:68:in `block (2 levels) in require' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:66:in `each' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:66:in `block in require' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:55:in `each' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler/runtime.rb:55:in `require' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/bundler-1.2.2/lib/bundler.rb:128:in `require' from /Users/yusuke/website/RAILS/sample/config/application.rb:7:in ` ' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:53:in `require' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:53:in `block in ' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:50:in `tap' from /Users/yusuke/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands.rb:50:in ` ' from script/rails:6:in `require' from script/rails:6:in ` '
そこで、(http://stackoverflow.com/questions/4546698/library-not-loaded-libmysqlclient-16-dylib-error-when-trying-to-run-rails-servを参考に)以下のコマンドを実行
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dy(....と、その前に,DBのmigrateをしてみます詳しくは http://railsdoc.com/references/rake%20db:migrate )もう一度サーバーを起動してみます。
rake db:migrateこれで、実際にDBにつながって、schema_migrations テーブルが作成されました!
# サーバー起動 $ rails server => Booting WEBrick => Rails 3.2.9 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2012-12-03 17:05:14] INFO WEBrick 1.3.1 [2012-12-03 17:05:14] INFO ruby 1.9.3 (2011-10-30) [x86_64-darwin11.4.2] [2012-12-03 17:05:14] INFO WEBrick::HTTPServer#start: pid=71027 port=3000
起動できました!! http://localhost:3000/ にアクセスしてみて下さい。
<備考...>
サブドメイン切ったりなどで、VirtualHost でrailsサーバーを複数たちあげたい場合は、その手前にApacheをはさめばいいのかな??
<VirtualHost *:80> ProxyPreserveHost On ServerName www.hogehoge.com ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ </VirtualHost>例えばこんな感じで。。。そこらへんまだ詳しくないです。