前提・実現したいこと
こちらの記事を参考にECSにrailsアプリをデプロイをしようとしています。
記事のECSにSSHログインした後、Railsのコンテナに入ってデータベースを作成する際に
rails db:create
を実行したところ以下のエラーが出ました。
発生している問題・エラーメッセージ
Unknown MySQL server host 'db' (-2) Couldn't create 'app_production' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:238:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:206:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:126:in `create' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:476:in `each' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:475:in `each' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:184:in `create_current' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `block in require' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `require' /app/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' /app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)
該当のソースコード
docker
1version: "2" 2services: 3 app: 4 build: 5 context: . 6 dockerfile: ./docker/rails/Dockerfile 7 command: bundle exec puma -C config/puma.rb 8 ports: 9 - "3000:3000" 10 volumes: 11 - .:/app 12 - /var/tmp 13 - sockets:/app/tmp/sockets 14 tty: true 15 depends_on: 16 - db 17 extends: 18 file: ./docker/mysql/password.yml 19 service: password 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 nginx: 32 build: 33 context: . 34 dockerfile: ./docker/nginx/Dockerfile 35 ports: 36 - "80:80" 37 volumes: 38 - sockets:/app/tmp/sockets 39 depends_on: 40 - app 41 42volumes: 43 db_data: 44 sockets: 45
database.yml
1# MySQL. Versions 5.5.8 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: utf8mb4 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: 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/credentials.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 https://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'] %> 55
試したこと
Dockerボリュームを一度削除してもう一度行ってみましたが同じエラーになりました。
解決法を教えていただけたら幸いです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー