Ruby on Rails で普通にプロジェクトを作成すると sqlite3 が使われるようになっていますが、mysql を使用するにはプロジェクト作成時に -d オプションで mysql を指定します。
1 $ rails new turntable_test -d mysql
2 create
3 create README.rdoc
4 create Rakefile
5 create config.ru
6 create .gitignore
7 create Gemfile
8 create app
9 create app/assets/javascripts/application.js
10 create app/assets/stylesheets/application.css
11 create app/controllers/application_controller.rb
12 create app/helpers/application_helper.rb
13 create app/views/layouts/application.html.erb
14 create app/assets/images/.keep
15 create app/mailers/.keep
16 create app/models/.keep
17 create app/controllers/concerns/.keep
18 create app/models/concerns/.keep
19 create bin
20 create bin/bundle
21 create bin/rails
22 create bin/rake
23 create config
24 create config/routes.rb
25 create config/application.rb
26 create config/environment.rb
27 create config/environments
28 create config/environments/development.rb
29 create config/environments/production.rb
30 create config/environments/test.rb
31 create config/initializers
32 create config/initializers/backtrace_silencers.rb
33 create config/initializers/filter_parameter_logging.rb
34 create config/initializers/inflections.rb
35 create config/initializers/mime_types.rb
36 create config/initializers/secret_token.rb
37 create config/initializers/session_store.rb
38 create config/initializers/wrap_parameters.rb
39 create config/locales
40 create config/locales/en.yml
41 create config/boot.rb
42 create config/database.yml
43 create db
44 create db/seeds.rb
45 create lib
46 create lib/tasks
47 create lib/tasks/.keep
48 create lib/assets
49 create lib/assets/.keep
50 create log
51 create log/.keep
52 create public
53 create public/404.html
54 create public/422.html
55 create public/500.html
56 create public/favicon.ico
57 create public/robots.txt
58 create test/fixtures
59 create test/fixtures/.keep
60 create test/controllers
61 create test/controllers/.keep
62 create test/mailers
63 create test/mailers/.keep
64 create test/models
65 create test/models/.keep
66 create test/helpers
67 create test/helpers/.keep
68 create test/integration
69 create test/integration/.keep
70 create test/test_helper.rb
71 create tmp/cache
72 create tmp/cache/assets
73 create vendor/assets/javascripts
74 create vendor/assets/javascripts/.keep
75 create vendor/assets/stylesheets
76 create vendor/assets/stylesheets/.keep
77 run bundle install
78 Enter your password to install the bundled RubyGems to your system:
79 Fetching gem metadata from https://rubygems.org/...........
80 Fetching gem metadata from https://rubygems.org/..
81 Resolving dependencies...
82 Using rake (10.1.0)
83 Installing i18n (0.6.9)
84 Using minitest (4.7.5)
85 Using multi_json (1.8.2)
86 Using atomic (1.1.14)
87 Using thread_safe (0.1.3)
88 Using tzinfo (0.3.38)
89 Using activesupport (4.0.1)
90 Using builder (3.1.4)
91 Using erubis (2.7.0)
92 Using rack (1.5.2)
93 Using rack-test (0.6.2)
94 Using actionpack (4.0.1)
95 Installing mime-types (1.25.1)
96 Using polyglot (0.3.3)
97 Using treetop (1.4.15)
98 Using mail (2.5.4)
99 Using actionmailer (4.0.1)
100 Using activemodel (4.0.1)
101 Using activerecord-deprecated_finders (1.0.3)
102 Using arel (4.0.1)
103 Using activerecord (4.0.1)
104 Using bundler (1.3.5)
105 Using coffee-script-source (1.6.3)
106 Using execjs (2.0.2)
107 Using coffee-script (2.2.0)
108 Using thor (0.18.1)
109 Using railties (4.0.1)
110 Using coffee-rails (4.0.1)
111 Using hike (1.2.3)
112 Installing jbuilder (1.5.3)
113 Using jquery-rails (3.0.4)
114 Using json (1.8.1)
115 Installing mysql2 (0.3.14)
116 Using tilt (1.4.1)
117 Installing sprockets (2.10.1)
118 Using sprockets-rails (2.0.1)
119 Using rails (4.0.1)
120 Using rdoc (3.12.2)
121 Using sass (3.2.12)
122 Using sass-rails (4.0.1)
123 Using sdoc (0.3.20)
124 Installing turbolinks (2.0.0)
125 Installing uglifier (2.3.2)
126 Your bundle is complete!
127 Use `bundle show [gemname]` to see where a bundled gem is installed.
オプションなしでプロジェクトを作成したときとの違いは、作成時の出力で下記のように示されている通り、mysqlのgemが使用されます。
1 Installing mysql2 (0.3.14)
また、sqlite3は使用されなくなるので、下記のようなsqlite3に関する出力はなくなります。
1 Using sqlite3 (1.3.8)
作成される config/database.yml の差分は下記の通りです。
1 $ diff test_app/config/database.yml turntable_test/config/database.yml
2 1,2c1
3 < # SQLite version 3.x
4 < # gem install sqlite3
5 ---
6 > # MySQL. Versions 4.1 and 5.0 are recommended.
7 4,5c3,10
8 < # Ensure the SQLite 3 gem is defined in your Gemfile
9 < # gem 'sqlite3'
10 ---
11 > # Install the MYSQL driver
12 > # gem install mysql2
13 > #
14 > # Ensure the MySQL gem is defined in your Gemfile
15 > # gem 'mysql2'
16 > #
17 > # And be sure to use new-style password hashing:
18 > # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
19 7,8c12,14
20 < adapter: sqlite3
21 < database: db/development.sqlite3
22 ---
23 > adapter: mysql2
24 > encoding: utf8
25 > database: turntable_test_development
26 10c16,18
27 < timeout: 5000
28 ---
29 > username: root
30 > password:
31 > socket: /tmp/mysql.sock
32 16,17c24,26
33 < adapter: sqlite3
34 < database: db/test.sqlite3
35 ---
36 > adapter: mysql2
37 > encoding: utf8
38 > database: turntable_test_test
39 19c28,30
40 < timeout: 5000
41 ---
42 > username: root
43 > password:
44 > socket: /tmp/mysql.sock
45 22,23c33,35
46 < adapter: sqlite3
47 < database: db/production.sqlite3
48 ---
49 > adapter: mysql2
50 > encoding: utf8
51 > database: turntable_test_production
52 25c37,39
53 < timeout: 5000
54 ---
55 > username: root
56 > password:
57 > socket: /tmp/mysql.sock
sqlite3 の設定が mysql 用の設定に変更されているのがわかります。username, password などは適宜変更します。
binstubsを実行してから、rake でDBを作成します。
1 $ bundle --binstubs
2 $ bin/rake db:create
mysqlにログインしてDBが作成されていることを確認します。
1 mysql> show databases;
2 +
3 | Database |
4 +
5 | information_schema |
6 | mysql |
7 | performance_schema |
8 | test |
9 | turntable_test_development |
10 | turntable_test_test |
11 +
12 6 rows in set (0.00 sec)
動作確認用のサンプルとしてUserModelを作成します。
1 $ bin/rails generate model User name:string email:string
2 invoke active_record
3 create db/migrate/20131211224446_create_users.rb
4 create app/models/user.rb
5 invoke test_unit
6 create test/models/user_test.rb
7 create test/fixtures/users.yml
migrate を実行してDBにテーブルを作成します
1 $ bin/rake db:migrate
2 == CreateUsers: migrating ====================================================
3 -- create_table(:users)
4 -> 0.0110s
5 == CreateUsers: migrated (0.0111s) ===========================================
下記のようにテーブルが作成されていれば成功です。
1 mysql> desc users;
2 +------------+--------------+------+-----+---------+----------------+
3 | Field | Type | Null | Key | Default | Extra |
4 +------------+--------------+------+-----+---------+----------------+
5 | id | int(11) | NO | PRI | NULL | auto_increment |
6 | name | varchar(255) | YES | | NULL | |
7 | email | varchar(255) | YES | | NULL | |
8 | created_at | datetime | YES | | NULL | |
9 | updated_at | datetime | YES | | NULL | |
10 +------------+--------------+------+-----+---------+----------------+
11 5 rows in set (0.00 sec)