LoginSignup
0
0

More than 3 years have passed since last update.

HerokuデプロイでMySQLのConnectionErrorが出たときの対処

Posted at

状況

heroku上にアプリを作成するまでは完了していて、URLにアクセスしたら「Welcome to your new app!」と出て、
herokuへgitでプッシュまでしたのですが、最後にデータベースを作成しようとしました。しかし、ここでConnectionError。

$ heroku run rails db:migrate
:1033:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke'
/app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require'
/app/bin/rails:9:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

解決した方法

色々なサイトをみているうちに、DATABASE_URLが関係あるということがわかったので、見てみる。

$ heroku config | grep CLEARDB_DATABASE_URL
=> CLEARDB_DATABASE_URL=mysql://~省略

「mysql://」を「mysql2://」に変える必要がある!
gemではmysql2を使っていたので対応させる必要があった。

なので上書きします

$ heroku config:set DATABASE_URL=mysql2://~省略
Setting DATABASE_URL and restarting  アプリ名... done, v9
DATABASE_URL: mysql2://~省略

これで上書きされました。
そして再度データベース作成をしてみます

$ heroku run rails db:migrate
...省略
Migrating to CreateIntros (20200518235947)
== 20200518235947 CreateIntros: migrating =====================================
-- create_table(:intros)
   (10.1ms)  CREATE TABLE `intros` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL)
   -> 0.0106s
== 20200518235947 CreateIntros: migrated (0.0106s) ============================

これで正常にDBが作成され、公開できます。

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