herokuでアプリを構築する際に、herokuはデフォルトだとDBをpostgresqlを使用することになってしまいます。MySQLを使いたい場合、herokuのaddonにClearDBを追加して herokuのconfigのDATABASE_URLを書き換える必要があります。
その際に、DATABASE_URLがなぜか変更できない事象にハマったのでメモしておきます。
(前は普通に変更できたのになぁ…)
まずは設定の確認
1 2 3 4 5 6 |
$ heroku config CLEARDB_DATABASE_URL: mysql://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true DATABASE_URL: postgres://zebafrrglyziec:1FNOnIbpN3tAfD2xkvW6hUldkl@ec2-54-83-12-22.compute-1.amazonaws.com:5432/dch5l0q66h0o4d LANG: en_US.UTF-8 RACK_ENV: production RAILS_ENV: production |
現時点でDATABASE_URLはpostgresqlになっています。これをCLEARDB_DATABASE_URLの値に設定し直します。
※ “mysql:”の部分を”mysql2:”に変えることを忘れないで下さい。
1 2 3 |
$ heroku config:set DATABASE_URL='mysql2://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true' Setting config vars and restarting hogehoge-app... !!! ▸ Cannot overwrite attachment values DATABASE_URL. |
この時に上記のエラーが発生して変更を受け付けてくれない…
結論として、いったんDATABASEの設定を消さなければならないのですが、heroku-postgresqlをaddonに入れないといけないらしい
1 2 3 4 5 |
$ heroku addons:attach heroku-postgresql -a hogehoge-app Your version of git is 2.1.2. Which has serious security vulnerabilities. More information here: https://blog.heroku.com/archives/2014/12/23/update_your_git_clients_on_windows_and_os_x Attaching postgresql-deep-28339 to hogehoge-app... done Setting HEROKU_POSTGRESQL_PURPLE vars and restarting hogehoge-app ... done, v7 |
そして、DATABASEの設定を削除します。
1 2 3 4 5 |
$ heroku addons:detach DATABASE -a hogehoge-app Your version of git is 2.1.2. Which has serious security vulnerabilities. More information here: https://blog.heroku.com/archives/2014/12/23/update_your_git_clients_on_windows_and_os_x Removing DATABASE attachment to postgresql-deep-28339 from hogehoge-app... done Unsetting DATABASE vars and restarting hogehgoe-app... done, v8 |
そして設定したいDATABASE_URLを設定します
1 2 3 |
$ heroku config:set DATABASE_URL='mysql2://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true' Setting config vars and restarting hogehoge-app... done DATABASE_URL: mysql2://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true |
これで設定変更完了
1 2 3 4 5 6 7 |
$ heroku config --app hogehoge-app CLEARDB_DATABASE_URL: mysql://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true DATABASE_URL: mysql2://b52366143f57c8:e8f53525@us-cdbr-iron-east-03.cleardb.net/heroku_0c42da1aed580d8?reconnect=true HEROKU_POSTGRESQL_PURPLE_URL: postgres://zcbafrrglyziec:1FNOnIbpN3tAfD2xkvW6hUldkl@ec2-54-83-12-22.compute-1.amazonaws.com:5432/dch5l0q66h0o4d LANG: en_US.UTF-8 RACK_ENV: production RAILS_ENV: production |
今思えば、heroku-postgresqlを入れれば普通に変更できたのかもしれない。
今度実験してみる
[ 2016/07/02 追記 ]
DATABASEの設定を削除した時に以下のエラーが発生した時がありました。
1 2 3 |
$ heroku addons:detach DATABASE -a hogehoge-app Detaching DATABASE to postgresql-solid-90854 from ⬢ hogehgoe-app ! ▸ Cannot destroy last attachment to billing app for resource postgresql-solid-90854 |
普通にheroku-postgresqlを削除したら普通にDATABASE_URLを設定できます。
途中で、アプリ名の入力を求められるので入力してあげると削除されます。
1 2 3 4 5 6 7 8 9 10 11 |
$ heroku addons:destroy heroku-postgresql --app hogehoge-app Your version of git is 2.1.2. Which has serious security vulnerabilities. More information here: https://blog.heroku.com/archives/2014/12/23/update_your_git_clients_on_windows_and_os_x ! WARNING: Destructive Action ! This command will affect the app: hogehoge-app ! To proceed, type "hogehoge-app" or re-run this command with --confirm hogehoge-app > hogehoge-app Destroying postgresql-solid-90854 on hogehoge-app... done, (free) Removing vars for DATABASE from hogehoge-app and restarting... done, v7 |
この後にDATABASE_URLの設定を行って下さい。
2016年12月7日 at 12:14 PM
[現時点でDATABASE_URLはpostgresqlになっています。これをCLEARDB_DATABASE_URLの値に設定し直します。
※ “mysql:”の部分を”mysql2:”に変えることを忘れないで下さい。]
この部分ですが、なぜmysql2に変更する必要があるかを教えていただけますでしょうか?
2016年12月7日 at 12:23 PM
LEEさん、コメントありがとうございます。
mysql2にする理由は、railsとmysqlへの接続をするアダプターがmysql2というクライアントを使用しているからです。
もし環境が別なら別途指定する必要があります。
大体の方がRailsでmysqlを使う場合は”mysql2″を使用していますので、記事ではそのような記述をしています。
http://qiita.com/pchatsu/items/a7f53da2e57ae4aca065