前提・実現したいこと
heroku run db:migrateを通したい
本番環境(heroku)でdbを用いるサイトへ遷移したい。
発生している問題・エラーメッセージ
rails aborted! Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (25) /app/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' /app/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate' /app/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <main>' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke' /app/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<main>' /app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /app/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require' /app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency' /app/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require' /app/bin/rails:4:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
該当のソースコード
config/database.yml
config/database.yml
1# MySQL. Versions 5.1.10 and up are supported. 2# 3# Install the MySQL driver 4# gem install mysql2 5# 6# Ensure the MySQL gem is defined in your Gemfile 7# gem 'mysql2' 8# 9# And be sure to use new-style password hashing: 10# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html 11# 12default: &default 13 adapter: mysql2 14 encoding: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> 18 host: db 19 20development: 21 <<: *default 22 database: app_development 23 24# Warning: The database defined as "test" will be erased and 25# re-generated from your development database when you run "rake". 26# Do not set this db to the same as development or production. 27test: 28 <<: *default 29 database: app_test 30 31# As with config/secrets.yml, you never want to store sensitive information, 32# like your database password, in your source code. If your source code is 33# ever seen by anyone, they now have access to your database. 34# 35# Instead, provide the password as a unix environment variable when you boot 36# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 37# for a full rundown on how to provide these environment variables in a 38# production deployment. 39# 40# On Heroku and other platform providers, you may have a full connection URL 41# available as an environment variable. For example: 42# 43# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 44# 45# You can use this database configuration with: 46# 47# production: 48# url: <%= ENV['DATABASE_URL'] %> 49# 50production: 51 <<: *default 52 database: app_production 53 username: app 54 password: <%= ENV['APP_DATABASE_PASSWORD'] %>
docker-compose.yml
docker
1version: "2" 2services: 3 app: 4 build: 5 context: . 6 dockerfile: ./docker/rails/Dockerfile 7 # command: bundle exec unicorn -p 3000 -c /app/config/unicorn.rb 8 command: bundle exec rails s -p 3000 -b '0.0.0.0' 9 ports: 10 - "3000:3000" 11 volumes: 12 - /var/tmp 13 - .:/app 14 depends_on: 15 - db 16 extends: 17 file: ./docker/mysql/password.yml 18 service: password 19 20 db: 21 build: 22 context: . 23 dockerfile: ./docker/mysql/Dockerfile 24 ports: 25 - "3306:3306" 26 volumes: 27 - db_data:/var/lib/mysql 28 extends: 29 file: ./docker/mysql/password.yml 30 service: password 31 32 nginx: 33 build: 34 context: . 35 dockerfile: ./docker/nginx/Dockerfile 36 ports: 37 - "80:80" 38 volumes_from: 39 - app 40 41volumes: 42 db_data: 43
試したこと
database.yml
のhost: db
をhost: localhost
に変える系
https://teratail.com/questions/244445
https://www.tutorialfor.com/questions-79268.htm
→変化なし
2. heroku logs -t
で本番環境のログを出力
サインアップ、ログインページにいくとMysql2::Error::ConnectionError (Unknown MySQL server host 'db' (0)):
を吐かれる
補足情報(FW/ツールのバージョンなど)
docker
mysql
ruby '2.6.3'
rails '5.2.3'
- 開発環境で動くのに、本番環境で動かない。
- ページはトップページとログインページとサインアップページのみ。
- ログインにdeviseを使用しており、本番環境でログインとサインアップのページを遷移する際に動かなくなる。
- deviseではtwitterのOAuth認証も追加しており、APIキーが外部から見えないように
gem 'dotenv-rails'
を使っている。APIキーを記した.env
ファイルは.gitignore
で隠蔽をしている。最初はAPI-KEYが本番環境に反映されていないのが原因だと考えていたが、heroku config:set ~~~
で本番環境にもAPI-KEYを反映させているので、その線よりはエラーの名称からしてdatabase.yml
やdocker-compose.yml
の設定を疑う方がベターだと考えた。しかし、どこが悪いのか見当もつかない。 - herokuはデフォルトでmysqlをサポートしていない。しかし、herokuに
heroku addons:create cleardb:ignite
でcleardbを追加しているため、mysqlだから動かないというわけでもない。
分かる方いたらご教授頂けると幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。