首页 > 解决方案 > 发生未处理的低级错误。“生产”环境缺少“secret_key _base”

问题描述

我的ruby版本是“2.6.6”,我的mysql版本是“5.7”,我的mysql2版本是0.5.3,我的rails版本是5.0.7.2,我的Xcode版本是12.5。

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

我计划使用 HEROKU 的 URL 发布我的 Rails 应用程序(例如 https://[My APP Name].herokuapp.com)。

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

数据库.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'] %>

秘密.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"] %>

我通过以下四种方法添加了GENERETED CODEof rails secret(我也执行了。)。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”和“”保存了上面的 SECRET_KEY_BASE。然后Enter我执行了这个命令$ 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 的真正原因是什么?

标签: mysqlruby-on-railsrubyheroku

解决方案


首先,几个细节:

  • MySQL 与此错误无关,因此您可以从您的问题中编辑它。
  • 您本地 linux 机器环境中的任何内容都与该错误无关,因此您可以删除对printenv,echoexport.

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

heroku 看不到您的 SECRET_KEY_BASE 环境变量的最可能原因是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"]

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


推荐阅读