Ruby on Rails で普通にプロジェクトを作成すると sqlite3 が使われるようになっていますが、mysql を使用するにはプロジェクト作成時に -d オプションで mysql を指定します。
1 $ rails new turntable_test -d mysql 2 create 3 create README.rdo c 4 create Rakefile 5 create config.ru 6 create .gitignore 7 create Gemfile 8 create app 9 create app/assets /javascrip ts/applica tion.js 10 create app/assets /styleshee ts/applica tion.css 11 create app/contro llers/appl ication_co ntroller.r b 12 create app/helper s/applicat ion_helper .rb 13 create app/views/ layouts/ap plication. html.erb 14 create app/assets /images/.k eep 15 create app/mailer s/.keep 16 create app/models /.keep 17 create app/contro llers/conc erns/.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/rou tes.rb 25 create config/app lication.r b 26 create config/env ironment.r b 27 create config/env ironments 28 create config/env ironments/ developmen t.rb 29 create config/env ironments/ production .rb 30 create config/env ironments/ test.rb 31 create config/ini tializers 32 create config/ini tializers/ backtrace_ silencers. rb 33 create config/ini tializers/ filter_par ameter_log ging.rb 34 create config/ini tializers/ inflection s.rb 35 create config/ini tializers/ mime_types .rb 36 create config/ini tializers/ secret_tok en.rb 37 create config/ini tializers/ session_st ore.rb 38 create config/ini tializers/ wrap_param eters.rb 39 create config/loc ales 40 create config/loc ales/en.ym l 41 create config/boo t.rb 42 create config/dat abase.yml 43 create db 44 create db/seeds.r b 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/fav icon.ico 57 create public/rob ots.txt 58 create test/fixtu res 59 create test/fixtu res/.keep 60 create test/contr ollers 61 create test/contr ollers/.ke ep 62 create test/maile rs 63 create test/maile rs/.keep 64 create test/model s 65 create test/model s/.keep 66 create test/helpe rs 67 create test/helpe rs/.keep 68 create test/integ ration 69 create test/integ ration/.ke ep 70 create test/test_ helper.rb 71 create tmp/cache 72 create tmp/cache/ assets 73 create vendor/ass ets/javasc ripts 74 create vendor/ass ets/javasc ripts/.kee p 75 create vendor/ass ets/styles heets 76 create vendor/ass ets/styles heets/.kee p 77 run bundle install 78 Enter your password to install the bundled RubyGems to your system: 79 Fetching gem metadata from https://ru bygems.org /......... .. 80 Fetching gem metadata from https://ru bygems.org /.. 81 Resolving dependenci es... 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_saf e (0.1.3) 88 Using tzinfo (0.3.38) 89 Using activesupp ort (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 actionmail er (4.0.1) 100 Using activemode l (4.0.1) 101 Using activereco rd-depreca ted_finder s (1.0.3) 102 Using arel (4.0.1) 103 Using activereco rd (4.0.1) 104 Using bundler (1.3.5) 105 Using coffee-scr ipt-source (1.6.3) 106 Using execjs (2.0.2) 107 Using coffee-scr ipt (2.2.0) 108 Using thor (0.18.1) 109 Using railties (4.0.1) 110 Using coffee-rai ls (4.0.1) 111 Using hike (1.2.3) 112 Installing jbuilder (1.5.3) 113 Using jquery-rai ls (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 Installingmysql2 (0.3.14)
また、sqlite3は使用されなくなるので、下記のようなsqlite3に関する出力はなくなります。
1 Using sqlite3 (1.3.8)
作成される config/dat
1 $ diff test_app/config/data base.yml turntable_ test/confi g/database .yml 2 1,2c1 3 < # SQLite version 3.x 4 < # gem install sqlite3 5 --- 6 > # MySQL. Versions 4.1 and 5.0 are recommende d. 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/refma n/5.0/en/o ld-client. html 19 7,8c12,14 20 < adapter: sqlite3 21 < database: db/develop ment.sqlit e3 22 --- 23 > adapter: mysql2 24 > encoding: utf8 25 > database: turntable_ test_devel opment 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.sq lite3 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/product ion.sqlite 3 48 --- 49 > adapter: mysql2 50 > encoding: utf8 51 > database: turntable_ test_produ ction 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 --binstubs2 $ bin/rake db:create
mysqlにログインしてDBが作成されていることを確認します。
1 mysql> show databases; 2 +---------------------- ------+ 3 | Database | 4 +------------ ---------- ------+ 5 | informatio n_schema | 6 | mysql | 7 | performanc e_schema | 8 | test | 9 | turntable_ test_devel opment | 10 | turntable_ test_test | 11 +------------ ---------- ------+ 12 6 rows in set (0.00 sec)
動作確認用のサンプルとしてUserModelを作成します。
1 $ bin/rails generate model User name:string email:stri ng 2 invoke active_rec ord 3 create db/migrate /201312112 24446_crea te_users.r b 4 create app/models /user.rb 5 invoke test_unit 6 create test/model s/user_tes t.rb 7 create test/fixtu res/users. yml
migrate を実行してDBにテーブルを作成します
1 $ bin/rake db:migrate2 == CreateUser s: migrating ========== ========== ========== ========== ========== == 3 -- create_tab le(:users) 4 -> 0.0110s 5 == CreateUser s: migrated (0.0111s) ========== ========== ========== ========== ===
下記のようにテーブルが作成されていれば成功です。
1 mysql> desc users; 2 +------------+------ --------+- -----+---- -+-------- -+-------- --------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------- ---+------ --------+- -----+---- -+-------- -+-------- --------+ 5 | id | int(11) | NO | PRI | NULL | auto_incre ment | 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)
posted by
akanuma
on Wed 11 Dec 2013
at 06:29