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'
その他に回答いただくにあたって追加で必要な情報等ございましたらご教示いただきたく存じます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。