【发布时间】:2021-11-03 09:32:50
【问题描述】:
我的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'] %>
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 secret的GENERETED CODE(我也执行了bundle exec rake secret。)。
1. $ export SECRET_KEY_BASE=GENERATED CODE
2. $ heroku config:set SECRET_KEY_BASE=GENERATED CODE
- 我将生成的代码添加到
~/.bash_profile。
export SECRET_KEY_BASE=GENERATED CODE
然后我用 esc、“:x”和 Enter 保存了上面的 SECRET_KEY_BASE。然后我执行了这个命令$ source ~/.bash_profile。
- 我将生成的代码添加到
./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没有打开但是有两条错误信息:
- 我执行了这个命令:
$ heroku open。
但是 Heroku 没有打开,但显示了以下消息。
An unhandled lowlevel error occurred. The application logs may have details.
- 我执行了这个命令:
$ 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 ruby-on-rails ruby heroku