前提・実現したいこと
Ruby on Railsで作成中のアプリの開発環境をDockerに移行したところ、CapistranoでのEC2へのデプロイの際、エラーが発生しました。
Docker導入前まではデプロイできていました。また、Dockerコンテナ上の開発環境では正常に起動しています。
手が出ない状況です。
恐れ入りますが、開発環境をDockerに移行したあとの、CapistranoでのEC2へのデプロイ方法についてご教示いただけませんでしょうか。
発生している問題・エラーメッセージ
bundle exec cap production deploy
を実行したところ、deploy:migrating
のところでMysql2::Error::ConnectionError: Unknown MySQL server host 'db' (2)
というエラーが発生しています。
↓発生したエラーメッセージ
deploy:migrating 01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate 01 rake aborted! 01 Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (2) 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:238:in `migrate' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:86:in `block (3 levels) in <main>' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:84:in `each' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:84:in `block (2 levels) in <main>' 01 /var/www/petput/shared/bundle/ruby/2.6.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>' 01 /home/ec2-user/.rbenv/versions/2.6.5/bin/bundle:23:in `load' 01 /home/ec2-user/.rbenv/versions/2.6.5/bin/bundle:23:in `<main>' 01 Tasks: TOP => db:migrate 01 (See full trace by running task with --trace) #<Thread:0x00007f926e224090@/Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true): Traceback (most recent call last): 13: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute' 12: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `run' 11: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `instance_exec' 10: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-rails-1.6.1/lib/capistrano/tasks/migrations.rake:23:in `block (3 levels) in <top (required)>' 9: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:92:in `within' 8: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-rails-1.6.1/lib/capistrano/tasks/migrations.rake:24:in `block (4 levels) in <top (required)>' 7: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:100:in `with' 6: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/capistrano-rails-1.6.1/lib/capistrano/tasks/migrations.rake:25:in `block (5 levels) in <top (required)>' 5: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:80:in `execute' 4: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute' 3: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `tap' 2: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute' 1: from /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:170:in `execute_command' /Users/kawashimamitsuhiro/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sshkit-1.21.2/lib/sshkit/command.rb:97:in `exit_status=': rake exit status: 1 (SSHKit::Command::Failed) rake stdout: Nothing written rake stderr: rake aborted! Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (2) # 同様のエラーコードが繰り返される
該当のソースコード
Unknown MySQL server host 'db' (2)
とあることから、docker-compose.ymlやdatabase.ymlのhostの指定部分が原因だと推定しています。
dockercompose
1version: "3" 2 3services: 4 db: 5 image: mysql:5.6.47 6 environment: 7 MYSQL_USER: root 8 MYSQL_ROOT_PASSWORD: password 9 ports: 10 - "4306:3306" 11 volumes: 12 - ./db/mysql/volumes:/var/lib/mysql 13 14 web: 15 build: . 16 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 17 volumes: 18 - .:/petput 19 - gem_data:/usr/local/bundle 20 ports: 21 - 3000:3000 22 depends_on: 23 - db 24 tty: true 25 stdin_open: true 26 27volumes: 28 gem_data:
database
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: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: password 18 socket: /tmp/mysql.sock 19 host: db 20 21development: 22 <<: *default 23 database: petput2_development 24 25# Warning: The database defined as "test" will be erased and 26# re-generated from your development database when you run "rake". 27# Do not set this db to the same as development or production. 28test: 29 <<: *default 30 database: petput2_test 31 32# As with config/credentials.yml, you never want to store sensitive information, 33# like your database password, in your source code. If your source code is 34# ever seen by anyone, they now have access to your database. 35# 36# Instead, provide the password as a unix environment variable when you boot 37# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 38# for a full rundown on how to provide these environment variables in a 39# production deployment. 40# 41# On Heroku and other platform providers, you may have a full connection URL 42# available as an environment variable. For example: 43# 44# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 45# 46# You can use this database configuration with: 47# 48# production: 49# url: <%= ENV['DATABASE_URL'] %> 50# 51production: 52 <<: *default 53 database: petput2_production 54 username: root 55 password: <%= ENV['DATABASE_PASSWORD'] %> 56 socket: /var/lib/mysql/mysql.sock
試したこと
本番環境のMySQL設定を指定しなおしたら解決できるかと考え、以下のようにhost: localhost
としましたが、解決できませんでした。
database
1production: 2 <<: *default 3 database: petput2_production 4 username: root 5 password: <%= ENV['DATABASE_PASSWORD'] %> 6 socket: /var/lib/mysql/mysql.sock 7 host: localhost
↓この記事に従い
https://qiita.com/mitsu1208g/questions/0659fc82b58cf25fab79
database.ymlのdefaultで、以下の2つをコメントアウトしてみましたが、解決できませんでした。
database
1# password: password 2# host: db
↓こちらの記事も見たのですが、複雑で実行できておりません。
https://teratail.com/questions/321052
開発環境
macOS Big Sur バージョン 11.2.3
Ruby 2.6.5
Rails 6.0.3.4
MySQL 5.6.47
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。