Subscribed unsubscribe Subscribe Subscribe

RailsでMySQLを使う

 Ruby on Rails で普通にプロジェクトを作成すると sqlite3 が使われるようになっていますが、mysql を使用するにはプロジェクト作成時に -d オプションで mysql を指定します。

$ rails new turntable_test -d mysql
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
         run  bundle install
Enter your password to install the bundled RubyGems to your system: 
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.0) 
Installing i18n (0.6.9) 
Using minitest (4.7.5) 
Using multi_json (1.8.2) 
Using atomic (1.1.14) 
Using thread_safe (0.1.3) 
Using tzinfo (0.3.38) 
Using activesupport (4.0.1) 
Using builder (3.1.4) 
Using erubis (2.7.0) 
Using rack (1.5.2) 
Using rack-test (0.6.2) 
Using actionpack (4.0.1) 
Installing mime-types (1.25.1) 
Using polyglot (0.3.3) 
Using treetop (1.4.15) 
Using mail (2.5.4) 
Using actionmailer (4.0.1) 
Using activemodel (4.0.1) 
Using activerecord-deprecated_finders (1.0.3) 
Using arel (4.0.1) 
Using activerecord (4.0.1) 
Using bundler (1.3.5) 
Using coffee-script-source (1.6.3) 
Using execjs (2.0.2) 
Using coffee-script (2.2.0) 
Using thor (0.18.1) 
Using railties (4.0.1) 
Using coffee-rails (4.0.1) 
Using hike (1.2.3) 
Installing jbuilder (1.5.3) 
Using jquery-rails (3.0.4) 
Using json (1.8.1) 
Installing mysql2 (0.3.14) 
Using tilt (1.4.1) 
Installing sprockets (2.10.1) 
Using sprockets-rails (2.0.1) 
Using rails (4.0.1) 
Using rdoc (3.12.2) 
Using sass (3.2.12) 
Using sass-rails (4.0.1) 
Using sdoc (0.3.20) 
Installing turbolinks (2.0.0) 
Installing uglifier (2.3.2) 
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

 オプションなしでプロジェクトを作成したときとの違いは、作成時の出力で下記のように示されている通り、mysqlのgemが使用されます。

Installing mysql2 (0.3.14)

 また、sqlite3は使用されなくなるので、下記のようなsqlite3に関する出力はなくなります。

Using sqlite3 (1.3.8)

 作成される config/database.yml の差分は下記の通りです。

$ diff test_app/config/database.yml turntable_test/config/database.yml 
1,2c1
< # SQLite version 3.x
< #   gem install sqlite3
---
> # MySQL.  Versions 4.1 and 5.0 are recommended.
4,5c3,10
< #   Ensure the SQLite 3 gem is defined in your Gemfile
< #   gem 'sqlite3'
---
> # Install the MYSQL driver
> #   gem install mysql2
> #
> # Ensure the MySQL gem is defined in your Gemfile
> #   gem 'mysql2'
> #
> # And be sure to use new-style password hashing:
> #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
7,8c12,14
<   adapter: sqlite3
<   database: db/development.sqlite3
---
>   adapter: mysql2
>   encoding: utf8
>   database: turntable_test_development
10c16,18
<   timeout: 5000
---
>   username: root
>   password:
>   socket: /tmp/mysql.sock
16,17c24,26
<   adapter: sqlite3
<   database: db/test.sqlite3
---
>   adapter: mysql2
>   encoding: utf8
>   database: turntable_test_test
19c28,30
<   timeout: 5000
---
>   username: root
>   password:
>   socket: /tmp/mysql.sock
22,23c33,35
<   adapter: sqlite3
<   database: db/production.sqlite3
---
>   adapter: mysql2
>   encoding: utf8
>   database: turntable_test_production
25c37,39
<   timeout: 5000
---
>   username: root
>   password:
>   socket: /tmp/mysql.sock

 sqlite3 の設定が mysql 用の設定に変更されているのがわかります。username, password などは適宜変更します。
 binstubsを実行してから、rake でDBを作成します。

$ bundle --binstubs
$ bin/rake db:create

 mysqlにログインしてDBが作成されていることを確認します。

mysql> show databases;
+----------------------------+
| Database                   |
+----------------------------+
| information_schema         |
| mysql                      |
| performance_schema         |
| test                       |
| turntable_test_development |
| turntable_test_test        |
+----------------------------+
6 rows in set (0.00 sec)

 動作確認用のサンプルとしてUserModelを作成します。

$ bin/rails generate model User name:string email:string
      invoke  active_record
      create    db/migrate/20131211224446_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

 migrate を実行してDBにテーブルを作成します

$ bin/rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0110s
==  CreateUsers: migrated (0.0111s) ===========================================

 下記のようにテーブルが作成されていれば成功です。

mysql> desc users;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | YES  |     | NULL    |                |
| email      | varchar(255) | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)