发生未处理的低级错误。 'production' 环境缺少 `secret_key _base`

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

我的ruby版本是“2.6.6”,我的mysql版本是“5.7”,我的mysql2版本是0.5.3,我的[=115] =]版本是5.0.7.2,我的Xcode版本是12.5.

我使用 macOS Big Sur(版本 11.4)和文本编辑器“Atom”。

我打算用 HEROKU 的 URL 发布我的 Rails 应用程序(示例。https://[我的应用程序名称].herokuapp.com)。

即使我设置了环境变量,我还是导致了错误“'production' 环境缺少 secret_key _base”。

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"] %>

我通过以下四种方法添加了rails secretGENERETED CODE(我也执行了bundle exec rake secret。)

1. $ export SECRET_KEY_BASE=GENERATED CODE
2. $ heroku config:set SECRET_KEY_BASE=GENERATED CODE
  1. 我将生成的代码添加到 ~/.bash_profile
  export SECRET_KEY_BASE=GENERATED CODE

然后我用esc、“:x”和Enter保存了上面的SECRET_KEY_BASE。和我执行了这个命令$ source ~/.bash_profile.

  1. 我将生成的代码添加到 ./env 文件
DATABASE_USERNAME = ****
DATABASE_PASSWORD = ****
SECRET_KEY_BASE = GENERATED CODE

结束。

我通过以下三种方法验证了在Linux中设置了环境变量:

$ heroku config:get SECRET_KEY_BASE

GENERATED CODE

$ printenv | grep SECRET_KEY_BASE

SECRET_KEY_BASE=GENERATED CODE

$ echo $SECRET_KEY_BASE

GENERATED CODE

结束。

结果Heroku打不开,但是有两条报错信息:

  1. 我执行了这个命令:$ heroku open

但是Heroku没有打开但是显示了下面的信息。

An unhandled lowlevel error occurred. The application logs may have details.
  1. 我执行了这个命令:$ heroku logs 并显示了以下消息。
#<RuntimeError: Missing `secret_key
_base` for 'production' environment, set this value in `config/secrets.yml`>

结束。

根据上面两条消息,即使设置了环境变量,我也打不开Heroku

也许我认为我无法打开 Heroku,因为当前使用的 MySQL 版本‖"5.7"‖ 和 MySQL 使用此命令的版本 $ mysql --version 不匹配。

我执行了这个命令$ mysql --version

$ mysql --version

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

结束。

以下是我用MySQL"5.7".

的证据
$ brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

结束。

无法打开 Heroku 的真正原因是什么?

首先,几个细节:

  • MySQL 与这个错误无关,所以你可以从你的问题中编辑出来。
  • 本地 linux 机器环境中的任何内容都与该错误无关,因此您可以删除对 printenvechoexport 的任何引用。

我的猜测:config/secrets.yml 被 git-忽略

heroku 看不到您的 SECRET_KEY_BASE env 变量的最可能原因是 secrets.yml 文件本身永远不会进入 heroku。

如果 config/secrets.yml 文件从未从您的计算机上传,heroku 将无法找到它。与 DATABASE_URL 不同,密钥库没有从中读取的“默认”环境变量。

如何检查

如果你的项目在github/gitlab/other,你可以在那里检查,如果你找不到config/secrets.yml那么它也不会到达heroku。相反,如果您没有任何其他遥控器,请检查您的 .gitignore 文件(它包含推送时不应上传的文件列表,以某种方式说);它可能包含一行 config/secrets.yml.

另一种检查方法是在 config/secrets.yml 中更改某些内容(甚至是评论)并保存。然后在同一目录中写入 git status:如果它显示“无更改”,那么您的 config/secrets.yml 将被忽略。

做什么

最简单的方法是转到 config/environments/production.rb,然后直接设置您的密钥库:

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

一旦您提交并推送此更改,您就应该准备就绪。