LoginSignup
0
4

heroku 登録から(rails mysql)デプロイまで

Last updated at Posted at 2020-01-15

##ローカルでは動作確認済みのRailsアプリを、heroku初デプロイ##

###環境###

  • heroku
  • MacBookpro
  • ruby 2.6.3
  • bundler 2.0.2
  • rails 6.0.1
  • Mysql2 0.5.3
  • GitHub にて git clone(もしくは push) してある状態

<今後の時短のために記録> 今回Qiita自己用記録も含め、10時間かかった様子。
他でデプロイに14日かけてうまく行かず...原因がMVCではない事を再認識できた。知識不足、書籍等を活用する予定。
heroku1日で出来るのは有難い。


①heroku にアカウント登録
https://jp.heroku.com/

<<AWS と違い、基本ローカルからコマンド 入力 >>

②config/routes.rbのindex編集

Rails.application.routes.draw do
resources :blogs 
root 'home#top'   #トップページ:homeコントローラのtopアクションに設定

gem mysql (の確認。今回はローカルから記入済みなので、もし変更したらbundle install)

$ git add . 
$ git commit  
$ git push origin master   (GitHubへ)

###次回以降のapp作成時は ④$ heroku login へ###


③herokuのインストール

~~$ brew tap heroku/brew && brew install heroku~~ ここ保留

$ heroku -v(ersion)    確認

###④$ heroku login のため sshキー設定 <④は省略内容にはなる為 他サイト参考必須>###

(ローカルの.ssh に移動)
$ ssh-keygen -t rsa

:作成したいキー名_rsa
ls確認。

$ eval "$(ssh-agent -s)"

cd デプロイ先の階層に戻る

$ heroku keys:add ~/.ssh/作成した_rsa.pub

........SSH key... done  で成功。
$ heroku keys で確認可 *キーはherokuのHPでも確認可


⑤ 

$ heroku create  (herokuのHPで作成したアプリ名)**

or

$ git remote heroku (herokuのHPで作成したいアプリ名)

Name (作成したいアプリ名) is already taken。。。 で作成された

<<< $ git remote -v

origin
 origin git@github.com:。。。。。。。

(GitHubのしかないため、リモート:herokuを設定。アプリ名を指定しないとherokuリモートは自動で
作成される様子。)

$ heroku git:remote -a (作成したアプリ名)

set git remote heroku to https://git.heroku.com/(作成したアプリ名)  と出る

$ git remote -v で確認。
heroku
origin

>>>

⑥ DB設定
< [公式heroku参考] https://elements.heroku.com/addons/cleardb

$ heroku create cleardb:ignite

 Please verify your account to install this add-on plan (please enter a credit card) For more
 ▸    information, see https://devcenter.heroku.com/categories/billing Verify now at
 ▸    https://heroku.com/verify

heroku hpにてクレカの登録

2回目アプリデプロイ時、
Name must start with a letter, end with a letter or digit and can only
▸ contain lowercase letters, digits, and dashes.

と出た為、HPでアドオン DBFree設定した。 = ⑦へ


⑦環境変数の設定

$ heroku config ($ git config --list)で内容確認)


> CLEARDB_DATABASE_URL: mysql://<ユーザー名>:<パスワード>@<ホスト名>/<データベース名>?reconnect=true
$ heroku config:add DB_NAME='<データベース名>'
$ heroku config:add DB_USERNAME='<ユーザー名>'
$ heroku config:add DB_PASSWORD='<パスワード>'
$ heroku config:add DB_HOSTNAME='<ホスト名>'
$ heroku config:add DB_PORT='3306'
$ heroku config:add DATABASE_URL='mysql2://<ユーザー名>:<パスワード>@<ホスト名>/<データベース名>?reconnect=true'

$ heroku config で設定内容 再確認

###⑧ config/environments/production.rb を記入###

config/environments/production.rb
config.assets.compile = true
config.assets.initialize_on_precompile=false

###<herokuへpush>###

$ git add .
$ git commit 


$ git push heroku master

###⑨database.yml 設定###

階層:config/database.yml(production:変更)

production:
  <<: *default
  database: [データベース名]
  username: [ユーザ名]
  password: <%= ENV['DATABASE_PASSWORD'] %>

###(10) herokuのDBのマイグレーション###

$ heroku rake db:migrate

$ heroku open

remote: Verifying deploy... done.  成功。

[上のURLの方。ターミナ下方のURLではない。
https://(各自)...herokuapp.com/  (← これ => deployed to Heroku ]

<urlに接続>

###<ログを表示>##

$ heroku logs    
(heroku logs --tail)
↑ 見ても原因が良くわからない為 ↓で今回はエラー解決した

###$ heroku run rails console の方が分かる###


<urlに接続したが、エラーindex.htmlに繋がる。>

スクリーンショット 2020-01-16 3.19.59.png

ここからエラー内容3回目

$ heroku restart 一応。
(firebaseデプロイ時は時差があった、が変わらず。)
$ heroku logs --tail

内容が分からず、

$ heroku run rails console  で原因が分かりやすい。

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. (LoadError)

ここで、Gemfile に合わせ mysql → mysql2 に変更した。

##デプロイ成功##
②config/routes.rbで指定したindex.htmlが表示された。



###ここから↓ git push heroku master成功までの###
###実際のエラー対処記載 一部を参考に残す。###
⑨ ←ここでエラー内容1回目

Could not load the 'mysql' A....database.yml も確認しろと出る。

 database.yml 確認修正↓

⑦の $ heroku config:add DATABASE_URL='mysql2://<ユーザー名>:<パスワード>@<ホスト名>/<データベース名>?reconnect=true'

を Gemfile に合わせ mysql → mysql2 に最終的に変更。かつ bundle install

エラー内容2回目

...
Could not detect rake tasks
remote:  !     ensure you can run `$ bundle exec rake -P` ...URI::InvalidURIError: bad URI(is not URI?): mysql2:....
$ rbenv -v
rbenv 1.1.2
$ ruby -v
ruby '2.6.4'

$ gem install bundler -v 2.0.2
$ bundle install( + update)
$ rbenv -v が1.1.2のままの為、
$ rm Gemfile.lock
$ bundle install でGemfile.lock 再 bundler -v 2.0.2へ。
(add .  commit )
$ git push heroku master
変わらず同エラー。
今回は公式サイトを参考に、 ruby '2.6.3'に下げる。
$ rbenv install 2.6.3
$ rbenv local 2.6.3
(Gemfileも変更。$ bundle install $ rbenv rehash)
変わらず同エラー。
$ heroku config 設定ミスか確認。
$ heroku config:add DATABASE_URL='mysql://...をmysql2:...
に変更。
変わらず同エラー。
<直接な解決なのかは不明だが、今回は、$ heroku create アプリ名を指定しないで再度やり直す。このエラーは解決>
$ heroku create
$ git remote で自動herokuリモートが作成された。
$ git push heroku master

bad URI(is not URI?): mysql2:...エラーは解決したが、
新たなエラー1回目

remote:  !
remote:  !     Precompiling assets failed.

Precompil なので、

config/application.rbに
config.assets.initialize_on_precompile = true を追記。
config/environments/production.rbの
onfig.assets.compile = false を trueに。

変わらず。
$ RAILS_ENV=production (bundle exec) rake assets:precompile 
変わらず。

webpacker.yml .gitignore の編集で解決した。
webpacker.yml 内に
production:
<<: *default
compile: true がある為と思われる。全て知識不足が原因。


実際には③で 小さいエラーが出て bundle update install 等をプラス作業した
その後、push。

<基本だが一応残す>

rails -v
ruby -v
mysql -v

。。。ローカルの諸々の確認はした前提の内容。

③で 

...Warning: heroku update available from 7.35.0 to 7.35.1.....

$ heroku uodate を実行。
$ heroku -v 確認

⑨ mysql2 エラー

($ brew update && brew upgrade ruby​​-build できなかった)


GitHub 接続確認
$ ssh -T github git@github.com 
他サーバーにて設定変更してしまった為今回 再設定。


#参考にしたサイト#

mysqlを使ったRailsアプリをHerokuにデプロイする流れ

追えなくなってしまいましたが、その他、多々多々参考にさせて頂きました。

herokuチュートリアル
一部コマンドが公式の方が新しい様子です...

0
4
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
0
4