railsでmysqlを使ったのでメモ

  • mysqlいれる
  • bundlerを最新版にして並列処理の機能を使ってみる
  • railsmysqlを使う

mysql入れる

brewmysqlを入れてパスワードを設定.

$ brew install mysql
$ mysql_secure_installation

bundlerを最新版にする

まずはgem自体を最新にする

$ gem update --system

下のコマンドだとArealdy upto dateと言われる.

$ gem update bundler

のでこっち

$ gem install bundler --pre

railsmysqlを使う

作りたいプロジェクトのディレクトリを作ってBundlerで初期化.

$ mkdir projectname
$ cd projectname
$ bundle init

作られたGemfileのコメントを外す.今回はrails4は使わないのでバージョンの指定をする.

Gemfile

# gem "rails"

gem "rails", "3.2.13"

Bundlerでrailsを入れる.この時Bundlerの並列処理を試す.

$ bundle install -j4

速くなったかな?

railsプロジェクトで初期化.mysqlを使う

$ bundle exec rails new . --skip-bundle -d mysql

config/database.ymlで初期設定をする.この設定のdevelopment,test,productionそれぞれのpassword,user,databaseを自分の設定にする.

database.yml

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: projectname_development
  pool: 5
  username: usr
  password: passwd
  socket: /tmp/mysql.sock

database.ymlを編集後$ bundle exec rake db:migrateをしとく. コレでDBが作られる

Gemfileに追記して新しいGemを入れる
Gemfile

gem 'twitter-bootstrap-rails'
gem 'simple_form'
group :development do
   gem 'pry'
   gem 'sextant'
   gem 'better_errors'
   gem 'binding_of_caller'
end

編集し終わったら$ bundle install -j4

twitter-bootstrap-ralis,simple_formを使えるように.設定は公式をまねた.

bundle exec rails g bootstrap:install static
bundle exec rails g bootstrap:layout application fluid
bundle exec rails g simple_form:install --bootstrap

scaffoldでMVCつくる.

bundle exec rails g scaffold place name:string info:string star:integer

その後$ bundle exec rake db:migrateする.
サーバ実行動かして終了.

bundle exec rails s

参考URL

HomebrewでMySQL 5.6をインストール。開発用my.cnfもさらす
gem update --system したら gem update bundler もね!
Bundlerで並列処理??bundle installを爆速で処理する方法。
seyhunak/twitter-bootstrap-rails
plataformatec/simple_form
RailsのDBをMySQLにする方法
MySQL用のデータベース設定ファイル(database.yml)