This page looks best with JavaScript enabled
⚠️

【Rails + MySQL】AWS→herokuの移行

 ·   ·  ☕ 4 分で読めます
✏️

環境

  • Ruby 2.6.6
  • Rails 5.2.4.4
  • Vue 2.6.11
  • MySQL 8.0→5.7

流れ

  • herokuデプロイ用のブランチを作成する
  • 切ったブランチからherokuにデプロイする
  • DNSの設定をする

database.ymlの変更

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  production:
    <<: *default
-   database: hashlog_production
-   username: <%= Rails.application.credentials.production[:database][:username] || 'hashlog' %>
-   password: <%= Rails.application.credentials.production[:database][:password] %>
-   host: database-1.cwywibfxqfpp.ap-northeast-1.rds.amazonaws.com
+   # ここのDB_NAMEなどは何でもいいけど、heroku config:setのときの変数名と対応させる
+   database: <%= ENV['DB_NAME'] %>
+   url: <%= ENV['DB_URL'] %>
+   username: <%= ENV['DB_USERNAME'] %>
+   password: <%= ENV['DB_PASSWORD'] %>
+   host: <%= ENV['DB_HOSTNAME'] %>

デプロイ

1
2
3
4
5
6
7
heroku create hashlog # 名前を指定しなかったらランダムになる
Creating ⬢ hashlog... done
https://hashlog.herokuapp.com/ | https://git.heroku.com/hashlog.git

git push heroku heroku_branch:main # `heroku_branch`はローカルのブランチ名

heroku run rake db:migrate

Getting Started on Heroku with Rails 5.x | Heroku Dev Center
ここからがデプロイ手順。

dumpデータの移行

1
2
3
4
# 本番で
mysqldump -u ユーザー名 -h ホスト名 -p データベース名 > dump.sql
#
scp -i "aws-key.pem" username@1.1.1.1:/var/www/hashlog/dump.sql ~/Desktop

【MySQL】エラー:Warning: A partial dump from... - Qiita

ClearDBだとうまくいかないのでJawsDBにする

本番のdumpファイルをインポートしたところ、こんな感じで一部のテーブルのデータがだめぽ。

[クエリ 14 でエラー] Specified key was too long; max key length is 767 bytes
[クエリ 16 でエラー] Table 'heroku_eb9b292d281ded2.ar_internal_metadata' doesn't exist
[クエリ 17 でエラー] Table 'heroku_eb9b292d281ded2.ar_internal_metadata' doesn't exist
[クエリ 18 でエラー] Table 'heroku_eb9b292d281ded2.ar_internal_metadata' doesn't exist
[クエリ 19 でエラー] Table 'heroku_eb9b292d281ded2.ar_internal_metadata' doesn't exist
[クエリ 54 でエラー] Specified key was too long; max key length is 767 bytes
[クエリ 56 でエラー] Table 'heroku_eb9b292d281ded2.schema_migrations' doesn't exist
[クエリ 57 でエラー] Table 'heroku_eb9b292d281ded2.schema_migrations' doesn't exist
[クエリ 58 でエラー] Table 'heroku_eb9b292d281ded2.schema_migrations' doesn't exist
[クエリ 59 でエラー] Table 'heroku_eb9b292d281ded2.schema_migrations' doesn't exist
[クエリ 74 でエラー] Specified key was too long; max key length is 767 bytes
[クエリ 76 でエラー] Table 'heroku_eb9b292d281ded2.tweets' doesn't exist
[クエリ 77 でエラー] Table 'heroku_eb9b292d281ded2.tweets' doesn't exist
[クエリ 78 でエラー] Table 'heroku_eb9b292d281ded2.tweets' doesn't exist
[クエリ 79 でエラー] Table 'heroku_eb9b292d281ded2.tweets' doesn't exist
[クエリ 80 でエラー] Table 'heroku_eb9b292d281ded2.tweets' doesn't exist
[クエリ 85 でエラー] Specified key was too long; max key length is 767 bytes
[クエリ 87 でエラー] Table 'heroku_eb9b292d281ded2.users' doesn't exist
[クエリ 88 でエラー] Table 'heroku_eb9b292d281ded2.users' doesn't exist
[クエリ 89 でエラー] Table 'heroku_eb9b292d281ded2.users' doesn't exist
[クエリ 90 でエラー] Table 'heroku_eb9b292d281ded2.users' doesn't exist
1
2
3
4
5
6
7
8
DROP TABLE IF EXISTS `schema_migrations`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `schema_migrations` (
  `version` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

utf8とutf8mb4の文字コードの違いによるものなので、いろいろ解決法があるけど、多分一番楽なのはJawsDBにすること。

Heroku(Laravel)で自作アプリをHerokuにデプロイしようとしたらマイグレーションエラーが起きたので、ClearDB(MySQL5.5)からJawsDB(MySQL8.0)に変更した時の備忘録 | LaptrinhX
Rails×React×MySQLなSPAをHerokuにデプロイする手順 - 日々の学びのアウトプットするブログ

DNSの設定

お名前ドットコム

ヘッダーの「DNS」→「ドメインのDNS関連機能設定」→サイドバーの「ネームサーバーの変更」でこの画面に遷移する。
現在はAWSに向けているので、一旦初期化する。

image

DNSの設定はherokuでするので、お名前ドットコムではいじらない。

最初、Google Public DNSで確認したところ、宛先がまだAWSになっている。

image

ドメインにアクセスしたときにこの画面になればよし。
image

image

Automated Certificate Management Failing
1 domain failed validation.

これは後でいい感じになるのでこれでよし。

PointDNS

image
image

「Add Record」から、以下のレコードを追加する。

image
image

お名前.comの「DNSレコード設定」はこんな感じになる。

image

$ git push heroku 192_heroku:main

...
remote:        Removing msgpack (1.3.3)
remote:        Bundle completed (9.92s)
remote:        Cleaning up the bundler cache.
remote: -----> Installing node-v12.16.2-linux-x64
remote: -----> Installing yarn-v1.22.4
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        LoadError: Could not load the 'mysql' Active Record adapter. Ensure that the adapter is spelled correctly in config/database.yml and that you've added the necessary adapter gem to your Gemfile.

Ruby - git push heroku masterができない。LoadError: Could not load the &lsquo;mysql&rsquo; Active Record adapter.|teratail

1
2
3
4
5
2021-01-31T15:52:12.371735+00:00 app[web.1]: I, [2021-01-31T15:52:12.371666 #13]  INFO -- : [b1a8e225-5191-4b78-a72c-fed3c4910f9a] Completed 500 Internal Server Error in 72ms (ActiveRecord: 6.3ms)
2021-01-31T15:52:12.434617+00:00 app[web.1]: F, [2021-01-31T15:52:12.434526 #13] FATAL -- : [b1a8e225-5191-4b78-a72c-fed3c4910f9a]
2021-01-31T15:52:12.434677+00:00 app[web.1]: F, [2021-01-31T15:52:12.434619 #13] FATAL -- : [b1a8e225-5191-4b78-a72c-fed3c4910f9a] ActiveRecord::StatementInvalid (Mysql2::Error: Table 'heroku_eb9b292d281ded2.users' doesn't exist: SHOW FULL FIELDS FROM `users`):
2021-01-31T15:52:12.434724+00:00 app[web.1]: F, [2021-01-31T15:52:12.434679 #13] FATAL -- : [b1a8e225-5191-4b78-a72c-fed3c4910f9a]
2021-01-31T15:52:12.434776+00:00 app[web.1]: F, [2021-01-31T15:52:12.434731 #13] FATAL -- : [b1a8e225-5191-4b78-a72c-fed3c4910f9a]

secret_key_baseを追加する

デフォルトでheroku configに入っている値は違う値なので、rails credentialsのsecret_key_baseをheroku configに設定する。

1
2
3
4
5
6
7
[aiandrox@ip-10-0-11-43 hashlog]$ RAILS_ENV=production rails c
Running via Spring preloader in process 22420
WARNING: Spring is running in production. To fix this make sure the spring gem is only present in `development` and `test` groups in your Gemfile and make sure you always use `bundle install --without development test` in production
Loading production environment (Rails 5.2.4.4)

irb(main):001:0>  Rails.application.credentials.secret_key_base
=> "hogehogehoge" # これをコピー
1
$ heroku config:set SECRET_KEY_BASE="hogehogehoge"

secret_key_baseがどういうふうに扱われているか | oknm.jp

スケジューラの登録

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ heroku addons:create scheduler:standard --app hashlog

Creating scheduler:standard on ⬢ hashlog... free
 To manage scheduled jobs run:
 heroku addons:open scheduler

Created scheduler-trapezoidal-55864
Use heroku addons:docs scheduler to view documentation

$ heroku addons:open scheduler

image.png

UTCなので、AM6:00のタスクはPM21:00に。

初めてHerokuで独自ドメインを公開するあなたへ - Qiita
Custom Domain Names for Apps | Heroku Dev Center
Automated Certificate Management | Heroku Dev Center

貼り付けた画像_2021_02_10_8_35.png

実際の作業内容はこちら。でも、git以外の方がいろいろやった。
herokuに移行 by aiandrox · Pull Request #203 · aiandrox/hashlog

Share on

END
END
@aiandrox

 
目次