LoginSignup
12
8

More than 5 years have passed since last update.

Rails5 + heroku チュートリアルやってみた + ClearDB(MySQL)に変更もしてみた

Last updated at Posted at 2016-10-18

OSX、rbenvで Ruby 2.2.3。

チュートリアル完了し、ポスグレで動いているものは ここにおいた

チュートリアル完了し、さらに ClearDB(MySQL) にしたものは ここにおいた

どちらも Heroku & localhost で動かす Rails5 のベースとなるはず。ていうかこれが作りたくてやりました。

とりま 何もよまずに Rails5 いれる

$ mkdir rails5
$ cd rails5
$ bundle init
$ vim Gemfile # gem 'rails' のコメントを外す
$ bundle install --path vendor/bundle # Gemfileを上書きすると言われたらYを押す
$ bundle exec rails new ./
$ bundle exec rails -v 
Rails 5.0.0.1
$ bundle exec rails s

ここまできたらブラウザで http://localhost:3000 で楽しげなページが表示されればOK

heroku のドキュメントをやっと参照する

Getting Started with Rails 5.x on Heroku | Heroku Dev Center

DB を PostgreSQL にする

heroku は標準がポスグレなのでそうせよ。と。

$ vim Gemfile # gem 'sqlite3' 行 gem 'pg' に書き換える。
$ rm Gemfile.lock
$ bundle install

PostgreSQL を用意する

適当に用意すればいいが、ここでは brew を用いた。たぶん brew install postgresql でOK。brew で入れた古いDBがあったようでクリーンアップしておく(すべての既存DBがなくなるので注意)

クリンナップ処理(必要に応じて

$ mv /usr/local/var/postgres /usr/local/var/postgres_old
$ initdb /usr/local/var/postgres -E utf8

PostgreSQL 開始

$ pg_ctl -D /usr/local/var/postgres start # 止めるときは start を stop に

DB 用意

チュートリアルで接続できるようにDBのパスワードは弱いもの(herokuドキュメントのconfigにあわせてある)を使っているので注意。

$  psql -h localhost -n postgres
psql (9.5.4)
Type "help" for help.

postgres=# create role myapp with password 'mypassword';
CREATE ROLE
postgres=# create database myapp_production owner myapp;
CREATE DATABASE
postgres=# create database myapp_development owner myapp;
CREATE DATABASE

接続設定をする

チュートリアルにある内容で config/database.yml を置き換える。

welcome を作る

チュートリアルを進めて各ファイルを作成し、 bundle exec rails slocalhost:3000 にブラウザでアクセスして Hello World が表示されることを確認する。

rails_12factor を使わない

昔はこの名前の gem を使ったらしいがチュートリアルどおり config/environments/production.rb を更新する。

ruby のバージョン指定

特にしない。デプロイ時にWARNがでてしまうので明示的に指定すべきだが共同開発者と相談してバージョン決めてください。

Heroku へデプロイ

$ heroku create
$ git push heroku heroku_tutorial:master # <今のブランチ名>:master とする。詳しくは git push のヘルプを参照
$ heroku open

これで welcome が見えて Hello world がみえるはず!簡単だ!

MySQL に対応させる

config/database.yml

とりあえず rails new . --database=mysql した config/database.yml は以下なので入れ替える。

database.yml
# MySQL. Versions 5.0 and up are supported.
#
# 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.7/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: rails5mysql_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: rails5mysql_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>
#
production:
  <<: *default
  database: rails5mysql_production
  username: rails5mysql
  password: <%= ENV['RAILS5MYSQL_DATABASE_PASSWORD'] %>

Gemfile

pg な行を mysql2 に入れ替えて、rm Gemfile.lockbundle install

DBの用意

コンフィグファイルにあわせてDBを作る。プロダクションのユーザーも作って全権限を与えておく。以下をそのままやるとパスワードは mypassword なんて適当っぷりなので注意。

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.6.22 Homebrew

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database rails5mysql_development;
Query OK, 1 row affected (0.07 sec)

mysql> create database rails5mysql_production;
Query OK, 1 row affected (0.03 sec)
mysql> grant ALL on rails5mysql_production.* to rails5mysql@'%' identified by 'mypassword';
Query OK, 0 rows affected (0.06 sec)

mysql> grant ALL on rails5mysql_production.* to rails5mysql@'localhost' identified by 'mypassword';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

welcome を確かめる

bundle exec rails slocalhost:3000 をブラウザで見えればOK。

heroku を MySQL (ClearDB) いれる

https://devcenter.heroku.com/articles/cleardb をみながらやる。環境変数経由で MySQL への接続情報が渡されるのだが、その方法は mysql://mysql2:// に変更したものを DATABASE_URL という環境変数に指定しないといけない。加えて上記手順でいったんポスグレで動かしてしまうと一度DBのデタッチ処理をしないといけないらしい。まぁもうMySQLにしちゃうんで削除しちゃう。

$ heroku addons:destroy heroku-postgresql:hobby-dev
$ heroku addons:create cleardb:ignite
$ heroku config | grep CLEARDB_DATABASE_URL # これで mysql://.+ を確認
$ heroku config:set DATABASE_URL=mysql2://(上の.+をコピペ)

デプロイ & 確認

以下のようにしてデプロイ、確認できる。

$ git push heroku heroku_mysql:master # <今のブランチ名>:master とする。詳しくは git push のヘルプを参照
$ heroku open
12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8