"Cannot load database configuration" error when executing rails and force termination

Asked 2 years ago, Updated 2 years ago, 36 views


Rails and other Rails~-related commands, such as rails show the
Cannot load database configuration:(NoMethodError) undefined method[]'for nil:NilClass
I want to be able to boot up properly because it says an error and then shuts down.

Based on the error statement, database.yml may be suspicious, but
Considering the situation, I doubt the relationship between master.key and credentials.yml.

·7 months of programming experience
·Rails history for half a year (self-taught)

·Local environment = environment started with rails; non-Docker environment.
·Environment built with Docker environment=docker-compose up

(1-3 may not be directly related to this issue)
1. I was trying to deploy the Rails app using AWS ECR and ECS.
2. After Dockerizing the application developed in the local environment, we deployed it to ECS because it can now be started with docker-compose up.
3. I was able to deploy, but I probably got a ActiveSupport::MessageEncryptor::InvalidMessage error in the production environment.
(I haven't recorded it here, and the log is playing, so I can't check it.
The contents of the ECS environment variable RAILS_MASTER_KEY should have matched the master.key in the local environment.)

4. Based on the contents of the error, I thought it was a master.key and credentials.yml encryption problem, and I thought it would be solved if I deleted both and regenerated them, so I deleted them and regenerated them with EDITOR='code —wait'rails credentials:edit.

5. Since then, both the local and Docker environments have been unable to boot due to a title error.

EDITOR='code —wait'rails credentials:edit (command to regenerate master.key and credentials.yml) and
I think the pair matches (you can restore master.key and encrypt credentials.yml), but both rails and docker-compose up failed to start.

But if you hit EDITOR='code—wait'rails credentials:edit, you'll see
For some reason, credentials.yml can be opened.
I don't know why it opens.Is the cause different from the part you see?

I understand that the command allows credentials.yml to open = rails, but is this a mistake in the first place?
I've had ActiveSupport::MessageEncryptor::InvalidMessage for about a day, but I don't know why.

·MacOS catalina 10.15.4

Local Environment

·ruby 2.5.1
·rails 5.2.4.2
·mysql 5.6

Docker Environment

·ruby 2.5.1
·rails 5.2.4.2
·mysql 5.7

From the error message, I thought it was because of the relationship between master.key and credentials.yml, but I thought there were other reasons, so I looked it up.

Before I first deleted master.key and credentials.yml, I was able to create a Docker environment with docker-compose up.
However, since it didn't start after deleting it once, I thought I might need to generate credentials.yml on Docker instead of on the local environment, so I generated it with the command docker-composer run-e EDITOR=vim app bundle exec rails credentials:edit but the result didn't change.

None of them could be started

Perhaps there was someone in Qiita who had encountered exactly the same problem and suggested a solution, but in my case, it was not resolved.
Nill class, unknown migration failed, the problem was held by credentials.yml.enc no matter how many times you tried.

I changed the DB name of the reference source in another Rails app, but I couldn't reproduce the same error, so I don't think database.yml has any problems.

However, one thing that bothered me was that even if I specified the DB of another app, the same error would appear, so I think there is a problem with the app that I am developing this time.

When I looked it up, it seemed to refer to either the config/master.key or the environment variable RAILS_MASTER_KEY, so I think this hypothesis is wrong.

I verified it using the steps below, but the result was the same as above error.
1.exportRAILS_MASTER_KEY="hoge" 2.echo$RAILS_MASTER_KEY➡ 結果Display results: hoge
3. I have confirmed that the master.key is set in the environment variable, so rails➡ は The result remains the same

As far as I can remember, there are two codes that I used to use credentials.yml.
1.database.yml (previously for EC2 to RDS connection) ➡ brCommented out
2.carrierwave.rb (S3 access and secret keys) ➡ 使用in use

However, from the results of the survey, I believe that the above two things are not related and are influenced by master.key and credentials.yml.
The reason is that another Rails app (same version, environment) can boot successfully even if it does not match credentials.yml, and if it does, rails will fail to display the page after startup.

Github Affected Repository

·The link in the teratail below and the questions are the same.
[Rails] rails will cause an error "Cannot load database configuration" and force termination

·Reason for multi-posting
I think I need to solve the problem quickly because of job-changing activities and financial reasons.
Sorry for the inconvenience.Crying

ruby-on-rails ruby

2022-09-30 14:43

1 Answers

I solved myself.
The reason is that I was trying to find the part specified in the credentials.yml that was commented out in the production environment specification field of database.yml.
After deleting the affected part, it started without any problems.

#production:
#   <<:* default
#   database —mynote_production
#   username:<%=Rails.application.credentials.rds[:DATABASE_USERNAME]%>← These guys
#   password:<%=Rails.application.credentials.rds[:DATABASE_PASSWORD]%>← These guys
#   host:<%=Rails.application.credentials.rds[:DATABASE_HOST]%>← These guys


2022-09-30 14:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.