[Ruby on Rails 4] Rails で MySQL を利用する設定
SQLite から MySQL に移行する手順。
[markdown]
やること。
– MySQL Adapter の mysql2 gem をインストールする。
– config/database.yml の設定を変更する。
– ID, Password を config/settings.local.yml から読み込むよう設定する。
今回は下記のローカル環境を想定。
> [Mac に MySQL 5.7 を brew install して自動起動させる | deadwood](https://www.d-wood.com/blog/2017/02/23_8835.html)
## rails new から始める場合
新規プロジェクトから MySQL を利用したい場合は、`rails new` で指定すれば良い。
“`html
% bundle exec rails new . –skip-test-unit –skip-bundle –database=mysql
“`
> [Rails+MySQLでアプリを作成する導入部分の手順 | EasyRamble](http://easyramble.com/rails-development-flow.html)
デフォルトの SQLite から MySQL へ後から変更する場合、以降の手順となる。
## adapter を変更する
MySQL adapter をインストールする。
> [brianmario/mysql2: A modern, simple and very fast Mysql library for Ruby – binding to libmysql](https://github.com/brianmario/mysql2)
“`ruby:Gemfile
#gem “sqlite3”
gem “mysql2”
“`
mysql2 に変更後、`bundle install` する。
## Rails アプリ用のユーザーを作成する
Rails アプリ用のユーザーを作成する。
`user_name` と `password` を設定する。
> [MySQL用のデータベース設定ファイル(database.yml) – Ruby on Rails入門](http://www.rubylife.jp/rails/model/index2.html)
“`html
% mysql -u root -p
mysql> GRANT ALL PRIVILEGES ON *.* TO user_name@localhost IDENTIFIED BY ‘password’;
mysql> select host,user from mysql.user;
“`
## config/database.yml を編集する
先ほど作成したユーザで接続する設定に書き換える。
> [Ruby on RailsでMySQLを使用する際に必要な作業手順](https://www.virment.com/rails-setup-mysql/)
“`yaml:config/database.yml
default: &default
adapter: mysql2
username: user_name
password: password
host: localhost
pool: 5
reconnect: false
socket: /tmp/mysql.sock
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_general_ci
development:
<<: *default
database: rails_app_development
test:
<<: *default
database: rails_app_test
production:
<<: *default
database: rails_app_production
``` `rake db` でデータベースを作成する。 ```html
% bin/rake db:setup
``` データベースが作成されていることを確認する。 ```html
% mysql -u root -p
mysql> SHOW DATABASES;
“`
確認が取れたら、`RAILS_ENV=production` も実行する。
“`html
% bin/rake db:setup RAILS_ENV=production
“`
### データの移行
このあたりを参考に SQL Inserts を調整。
> [sqlite3-to-mysql/sqlite3-to-mysql at master · athlite/sqlite3-to-mysql](https://github.com/athlite/sqlite3-to-mysql/blob/master/sqlite3-to-mysql)
## ID, Password を rails_config で読み込む
git で管理されている config/database.yml にパスワードが書かれているので、config/settings.local.yml に情報を移し、そこから読み込むように変更する。
> [railsconfig/config: Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other Ruby projects.](https://github.com/railsconfig/config)
使い方はこちらで(省略)。
> [rails 一度gitHubにあげたID・パスワード等を履歴から消し、環境ごとに管理する – Qiita](http://qiita.com/shizuma/items/efcbd2593446015130a6)
“`yaml:config/settings.local.yml
database:
user_name: user_name
password: password
“`
該当箇所を置き換える。
“`yaml:config/database.yml
default: &default
adapter: mysql2
username: <%= Settings.database[:user_name] %>
password: <%= Settings.database[:password] %>
host: localhost
“`
いろいろある。
> * [Managing Environment Configuration Variables in Rails with dotenv, figaro and secrets.yml](https://launchschool.com/blog/managing-environment-configuration-variables-in-rails)
> * [How to use secrets.yml in ruby on rails 4 to store a password in database.yml – Stack Overflow](http://stackoverflow.com/questions/35454814/how-to-use-secrets-yml-in-ruby-on-rails-4-to-store-a-password-in-database-yml)
> * [Use Rails 4 secrets.yml for database config (Example)](https://coderwall.com/p/3c2alg/use-rails-4-secrets-yml-for-database-config)
## 補遺
> * [MySQL: ドットインストール「MySQL入門」のまとめ | deadwood](https://www.d-wood.com/blog/2014/01/22_5327.html)
[/markdown]