Search code examples
mysqlruby-on-railsrubyheroku

An unhandled lowlevel error occurred. Missing `secret_key _base` for 'production' environment


My ruby ​​version is "2.6.6", my mysql version is "5.7", my mysql2 version is 0.5.3, my rails version is 5.0.7.2, and my Xcode version is 12.5.

I use macOS Big Sur(version 11.4) and the text editor "Atom".

I'm planning to release my Rails application with the URL of HEROKU(example. https://[My APP Name].herokuapp.com).

I caused the error "Missing secret_key _base for 'production' environment" even though I set up the environment variable.

database.yml

# MySQL. Versions 5.0 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:
#   http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: <%= ENV.fetch("DATABASE_USERNAME") %>
  password: <%= ENV.fetch("DATABASE_PASSWORD") %>
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: ****_development

# 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:
  <<: *default
  database: ****_test

# As with config/secrets.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 http://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:
  adapter: postgresql
  encoding: unicode
  pool: 5
  database: ****_production
  username: ****
  password: <%= ENV['****_DATABASE_PASSWORD'] %>

secrets.yml

# Be sure to restart your server when you modify this file.

# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!

# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rails secret` to generate a secure secret key.

# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.

development:
  secret_key_base: ****

test:
  secret_key_base: ****

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

I added the GENERETED CODE of rails secret(I also executed bundle exec rake secret.) by the four following methods.

1. $ export SECRET_KEY_BASE=GENERATED CODE
2. $ heroku config:set SECRET_KEY_BASE=GENERATED CODE
  1. I added the GENERATED CODE to ~/.bash_profile.
  export SECRET_KEY_BASE=GENERATED CODE

And then I saved the above SECRET_KEY_BASE with esc, ":x" and Enter.And I executed this command$ source ~/.bash_profile.

  1. I added the GENERATED CODE to ./env file
DATABASE_USERNAME = ****
DATABASE_PASSWORD = ****
SECRET_KEY_BASE = GENERATED CODE

end.

I verified that the environment variable is set in Linux by the three following methods:

$ heroku config:get SECRET_KEY_BASE

GENERATED CODE

or

$ printenv | grep SECRET_KEY_BASE

SECRET_KEY_BASE=GENERATED CODE

and

$ echo $SECRET_KEY_BASE

GENERATED CODE

end.

As a result, Heroku didn't open but there are two messages of the error:

  1. I executed this command:$ heroku open.

But Heroku didn't open but the following message was displayed.

An unhandled lowlevel error occurred. The application logs may have details.
  1. I executed this command:$ heroku logs and the following message was displayed.
#<RuntimeError: Missing `secret_key
_base` for 'production' environment, set this value in `config/secrets.yml`>

end.

I can't open Heroku ,even though I set up the environment variable ,according to the two above messages.

Maybe I think that I can't open Heroku because MySQL version currently in use "5.7" and MySQL version with this command $ mysql --version don't match.

I executed this command$ mysql --version.

$ mysql --version

mysql  Ver 8.0.23 for osx10.16 on x86_64 (Homebrew)

end.

The following is the evidence that I use MySQL"5.7".

$ brew services start [email protected]
==> Successfully started `[email protected]` (label: [email protected])

end.

What's the true cause why I can't open Heroku?


Solution

  • First, a couple of details:

    • MySQL has nothing to do with this error, so you can edit that out from your question.
    • Nothing in your local linux machine environment has nothing to do with the error, so you can remove any reference to printenv, echo and export.

    My guess: config/secrets.yml is git-ignored

    The most likely reason heroku does not see your SECRET_KEY_BASE env variable is that the secrets.yml file itself never goes to heroku.

    If the config/secrets.yml file is never uploaded from your computer, heroku has no way to find it. Unlike DATABASE_URL, the secret key base does not have a "default" environment variable from which it is read.

    How to check

    If your project is in github/gitlab/other, you can check there, if you cannot find config/secrets.yml then it's not going to get to heroku either. If instead you don't have any other remote, check your .gitignore file (it contains the list of files that should not be uploaded when you push, to say it in some way); it probably includes a line saying config/secrets.yml.

    Another way to check this is to change something (even a comment) in config/secrets.yml, and save. Then write git status in the same directory: if it says "no changes", then you have your config/secrets.yml ignored.

    What to do

    The easiest way is to go to config/environments/production.rb, and set your secret key base directly:

    # somehwere inside the configure block
    config.secret_key_base = ENV["SECRET_KEY_BASE"]
    

    Once you commit and push this change, you should be all set.