背景
我有一个包含各种变量的env文件,该变量取决于我的Rails应用程序,包括RAILS_ENV,该文件已初始化为在此文件中进行开发。我还database.yml
为我的环境定义了数据库连接的文件(如下)。当我运行rails控制台时,一切看起来都应该正确。检查Rails.configuration.database_configuration[Rails.env]
返回以下内容:
{"adapter"=>"postgresql",
"encoding"=>"utf8",
"database"=>"dev",
"username"=>"rails",
"password"=>"***",
"host"=>"localhost",
"pool"=>5,
"timeout"=>5000}
但是,当我尝试访问该应用程序时,出现502并记录了以下错误:
机架应用程序对象中出现ActiveRecord :: NoDatabaseError异常(FATAL:数据库“ prod”不存在。
显然,错误消息是准确的,但不代表我期望的。
我假设这是旅客/阿帕奇问题,因为运行rails console一切都很好。
注意:我已经在其他帖子中看到了这一点,所以没有-没有DATABASE_URL
环境变量浮在上覆盖来自的内容database.yml
。
我尝试过的
.bashrc
:乘客应该为用户apache的运行源bashrc的运行方式,而后者又被设置为获取相关的env文件。/etc/sysconfig/httpd
:我尝试过直接从Apache配置手动获取文件,并在脚本运行时通过将env转储到文件中进行验证,以确保将正确的值写入到env中,但是这也不会更改损坏的行为。数据库
development:
adapter: postgresql
encoding: utf8
database: dev
username: rails
password: <%= ENV['RAILS_DB_PWD'] %>
host: <%= ENV['RAILS_DB_HOST'] %>
pool: 5
timeout: 5000
# 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:
adapter: postgresql
encoding: utf8
database: test
username: rails
password: <%= ENV['RAILS_DB_PWD'] %>
host: <%= ENV['RAILS_DB_HOST'] %>
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: prod
username: rails
password: <%= ENV['RAILS_DB_PWD'] %>
host: <%= ENV['RAILS_DB_HOST'] %>
port: 5432
pool: 5
timeout: 5000
更好的方法是只使用DATABASE_URL
env var。
如果您同时拥有config / database.yml和
ENV['DATABASE_URL']
set,那么Rails会将配置合并在一起。
Rails指南:配置数据库
common: &common
adapter: postgresql
encoding: utf8
template: template0 # Required for UTF8 encoding
pool: 5
timeout: 5000
development:
<<: *common
database: dev
test:
<<: *common
database: test
production:
<<: *common
database: prod
我通常建议您避免在中指定数据库用户名和密码database.yml
。使用ENV vars Luke!当您处于中间状态时,最好采用约定而不是配置方法,而不要引入单独的var。
测试应用程序是否正确配置所有配置的一个试金石是,是否可以在不损害任何凭证的情况下,随时将代码库设为开源。https://12factor.net/config
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句