这个是 db 的配置文件
default: &default
adapter: mysql2
encoding: utf8
host: localhost
database: test_db
username: root
password: root
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
test:
<<: *default
production:
<<: *default
host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
这个是我 ~/.zshrc
的配置
export DATABASE_HOST=0.0.0.0
export DATABASE_USERNAME=hahaha
export DATABASE_PASSWORD=fuckfuck
然后 RAILS_ENV=production rails c
> ActiveRecord::Base.configurations
{
"default" => {
"adapter" => "mysql2",
"encoding" => "utf8",
"host" => "localhost",
"database" => "test_db",
"username" => "root",
"password" => "root",
"pool" => 5
},
"development" => {
"adapter" => "mysql2",
"encoding" => "utf8",
"host" => "localhost",
"database" => "test_db",
"username" => "root",
"password" => "root",
"pool" => 5
},
"test" => {
"adapter" => "mysql2",
"encoding" => "utf8",
"host" => "localhost",
"database" => "test_db",
"username" => "root",
"password" => "root",
"pool" => 5
},
"production" => {
"adapter" => "mysql2",
"encoding" => "utf8",
"host" => nil,
"database" => "test_db",
"username" => nil,
"password" => nil,
"pool" => 5
}
}
我的 .zshrc 中明明是有配置环境变量的,也执行过 source ~/.zshrc
了可是就是读不到啊。
另外 如果使用 RAILS_ENV=production DATABASE_HOST=xxxx rails c
的方式去启动是可以读到的。这个问题在 mac 和 ubuntu 上都存在。