質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Capistrano

Rubyで書かれたサーバオーケストレーションで、複数のサーバでスクリプトを実行する際に用いられます。主な使用用途はWebアプリケーションのデプロイメントです。 アプリケーションのバージョンアップ自動化、およびデータベースの変更などもできます。

Q&A

解決済

1回答

2268閲覧

【Rails×AWS】capistranoデプロイ時の「 NoMethodError: Cannot load database configuration」の解決方法がわかりません

black_pasta1001

総合スコア19

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Capistrano

Rubyで書かれたサーバオーケストレーションで、複数のサーバでスクリプトを実行する際に用いられます。主な使用用途はWebアプリケーションのデプロイメントです。 アプリケーションのバージョンアップ自動化、およびデータベースの変更などもできます。

0グッド

0クリップ

投稿2021/12/12 12:15

編集2021/12/13 12:44

railsでポートフォリオ作成中の初学者です。
capistranoでec2上に自動デプロイを実装しようとしていますが掲題のエラーが発生し困っています。

https://qiita.com/gyu_outputs/items/b123ef229842d857ff39
こちらの記事の流れに沿って実行していました。
ec2ターミナル上でuncorn+nginxでの手動デプロイは上手くいっていました。

#エラーコード

$ bundle exec cap production deploy deploy:migrating 01 $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate 01 rake aborted! 01 NoMethodError: Cannot load database configuration: 01 undefined method `[]' for nil:NilClass 01 (erb):64:in `<main>' 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/railties-6.0.4/lib/rails/application/configur… 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/activerecord-6.0.4/lib/active_record/railtie.… 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>' 01 /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `load' 01 /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `<main>' 01 01 Caused by: 01 NoMethodError: undefined method `[]' for nil:NilClass 01 (erb):64:in `<main>' 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/railties-6.0.4/lib/rails/application/configur… 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/activerecord-6.0.4/lib/active_record/railtie.… 01 /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>' 01 /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `load' 01 /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `<main>' 01 Tasks: TOP => db:migrate => db:load_config 01 (See full trace by running task with --trace) #<Thread:0x00007fcc1c1d2988 /Users/hoshiyuunari/Desktop/new_app/selftalk_app/vendor/bundle/ruby/3.0.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true): /Users/hoshiyuunari/Desktop/new_app/selftalk_app/vendor/bundle/ruby/3.0.0/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as ec2-user@18.178.238.98: rake exit status: 1 (SSHKit::Runner::ExecuteError) rake stdout: Nothing written rake stderr: rake aborted! NoMethodError: Cannot load database configuration: undefined method `[]' for nil:NilClass (erb):64:in `<main>' /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/railties-6.0.4/lib/rails/application/configuration.rb:228:in `database_configuration' /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/activerecord-6.0.4/lib/active_record/railtie.rb:39:in `block (3 levels) in <class:Railtie>' /var/www/selftalk_app/shared/bundle/ruby/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>' /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `load' /home/ec2-user/.rbenv/versions/3.0.2/bin/bundle:23:in `<main>'

#database.yml

# MySQL. Versions 5.5.8 and up are supported. # # Install the MySQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> # username: root # password: # socket: /tmp/mysql.sock development: adapter: mysql2 encoding: utf8 reconnect: false database: selftalk_app_development pool: 5 username: hoge password: hoge123 host: localhost # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: adapter: mysql2 encoding: utf8 reconnect: false database: selftalk_app_test pool: 5 username: hoge password: hoge123 host: localhost # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix environment variable when you boot # the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: <%= Rails.application.credentials.db[:database] %> username: <%= Rails.application.credentials.db[:username] %> password: <%= Rails.application.credentials.db[:password] %> socket: <%= Rails.application.credentials.db[:socket] %>

database: <%= Rails.application.credentials.db[:database] %>にはselftalk_appが設定されています。

#試したこと
mysqlのデータベースを確認したところ、

mysql> show databases; +--------------------------+ | Database | +--------------------------+ | information_schema | | mysql | | performance_schema | | selftalk_app_development | | selftalk_app_test | | sys | +--------------------------+

のように環境変数で設定した「selftalk_app」がなかったので下記の通り実行しました。

$ rails db:create RAILS_ENV=production $ rails db:migrate RAILS_ENV=production mysql> show databases; +--------------------------+ | Database | +--------------------------+ | information_schema | | mysql | | performance_schema | | selftalk_app | | selftalk_app_development | | selftalk_app_test | | sys | +--------------------------+

この後もデプロイ時に同様のエラーが発生してしまいました。

#バージョン

ruby '3.0.2' rails '6.0.4' mysql '8.0.26' capistrano '3.16.0'

その他に回答いただくにあたって追加で必要な情報等ございましたらご教示いただきたく存じます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
掲題のエラーはdatabase.ymlのproduction以降の環境変数の中身を参照できない時に発生するようです。
つまり、master.keyが見つからないのでcredentials.ymlを読み込めませんという状態です。

//config/deploy.rb set :linked_files, %w(config/master.key) //この一文を追加

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

投稿2021/12/14 12:12

black_pasta1001

総合スコア19

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問