Help us understand the problem. What is going on with this article?

HerokuでMySQLを使うときはDATABASE_URLを書き換える

More than 3 years have passed since last update.

環境

  • OS X El Capitan 10.11.16
  • ruby 2.3.0
  • rails 4.2.5.2

前提

無料枠内でのHerokuの準備とデプロイ(Mac 10 + Rails 4.2 + MySQL 5.6)を参考に
HerokuでMySQLを使うための設定をしていたところ
heroku run rake db:migrateを実行したところで以下のようなエラー発生

$ heroku run rake db:migrate
Running rake db:migrate on ⬢ <アプリ名>... up, run.2205 (Free)
rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)
...

問題と原因

HerokuのaddonでMySQLを追加したはずだが、うまく接続できていなさそう
database adapterがpostgresqlに設定されたままなのが原因か

解決策

そこでHerokuでMySQLを使おうとして詰まったところ - Qiitaを参考に解決を試みたところ、どうやら

1. CLEARDB_DATABASE_URLのmysqlmysql2に書き換えて、
2. DATABASE_URLもpostgresじゃなくてmysql2のURLに変える
必要がありそう

$ heroku config
=== flagmap Config Vars
CLEARDB_DATABASE_URL: mysql://~~~~~ #1の方
DATABASE_URL: postgres://~~~~~ #2の方
#..

以下のようにして1の方は書き換えられた

$ heroku config:set CLEARDB_DATABASE_URL='mysql2://~~~~~'

しかし2の方が上手くいかない、、

$ heroku config:set DATABASE_URL='mysql2://~~~~~'
Setting DATABASE_URL and restarting ⬢ <アプリ名>... !
 ▸    Cannot overwrite attachment values DATABASE_URL.
$ heroku addons:detach DATABASE -a <アプリ名>
Detaching DATABASE to postgresql-cylindrical-92240 from ⬢ mysterious-peak-72912... !
 ▸    Cannot destroy last attachment to billing app for resource postgresql-cylindrical-92240

ググりまくった末、mysql - Heroku and ClearDB error - Stack Overflowで解決できた
addons:detachではなくaddons:destroyでやったら上手くいった

$ heroku addons:destroy heroku-postgresql -a <アプリ名>
 ▸    WARNING: Destructive Action
 ▸    This command will affect the app <アプリ名>
 ▸    To proceed, type flagmap or re-run this command with --confirm
 ▸    <アプリ名>

> <アプリ名>
Destroying postgresql-cylindrical-92240 on ⬢ <アプリ名>... done

最後に再び以下のコマンドで書き換え成功!

heroku config:set DATABASE_URL='mysql2://~~~~~'

herokuの設定を確認すると

$ heroku config
=== flagmap Config Vars
CLEARDB_DATABASE_URL: mysql2://~~~~~
DATABASE_URL: mysql2://~~~~~
...

無事変更できてます

$ heroku run rake db:migrate

これでデプロイ成功しました!

参考にしたサイト

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away