简体   繁体   English

如何从rails 4中的外部文件加载数据库配置?

[英]How to load database configuration from an external file in rails 4?

Here is my database configuration file 这是我的数据库配置文件

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5

production:
  <<: *default
  host: localhost
  database: demo_production
  username: demo
  password: demo

I do not want to check-in my database configuration for production environment, I want to load it through an external file 我不想为生产环境签入我的数据库配置,我想通过外部文件加载它

How to do this in rails 4? 如何在rails 4中做到这一点?

You can load database configurations using environment variables just like this: 您可以使用环境变量加载数据库配置,如下所示:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5

production:
  <<: *default
  host: <%= ENV['PROD_DB_HOST'] %>
  database: <%= ENV['PROD_DB'] %>
  username: <%= ENV['PROD_USER'] %>
  password: <%= ENV['PROD_USER_PASS'] %>

If you look at the code, we have embed ruby code in the YML file. 如果你看一下代码,我们在yML文件中嵌入了ruby代码。 When rails executes this, it looks for the environment variables. 当rails执行此操作时,它会查找环境变量。 The environment variables can be set through UNIX shell or through some ruby program or a YML file. 可以通过UNIX shell或某些ruby程序或YML文件设置环境变量。 You have to make sure that the environment variables are set before database.yml is executed. 您必须确保在执行database.yml之前设置环境变量。

Method 1: If you are setting up the environment variables in UNIX, export the variables before you start the rails server. 方法1:如果要在UNIX中设置环境变量,请在启动rails服务器之前导出变量。

export PROD_DB_HOST = "my_host"
export PROD_DB = "my_production_db"
export PROD_USER = "my_prod_user"
export PROD_USER_PASS = "my_password"

Method 2: You could also set them through an external file and load it in the rails application. 方法2:您还可以通过外部文件设置它们并将其加载到rails应用程序中。 Let us see how to set through some ruby program 让我们看看如何设置一些ruby程序

#overrides.rb
# db configuration
ENV['PROD_DB_HOST'] = 'my_host'
ENV['PROD_DB'] = 'my_production_db'
ENV['PROD_USER'] = 'my_prod_user'
ENV['PROD_USER_PASS'] = 'my_password'

Now the final step, how to load the files in rails application? 现在最后一步,如何在rails应用程序中加载文件?

Rails load the configuration files in the following order Rails按以下顺序加载配置文件

  1. config/boot.rb 配置/的boot.rb
  2. config/application.rb 配置/ application.rb中
  3. config/environment.rb 到config / environment.rb
  4. config/environments/production.rb 配置/环境/ production.rb
  5. config/database.yml 配置/ database.yml的

So, you have to load the external config file before rails executes the 'config/database.yml'. 因此,您必须在rails执行'config / database.yml'之前加载外部配置文件。

'boot.rb' and 'application.rb' contains your application configurations. 'boot.rb'和'application.rb'包含您的应用程序配置。 I don't want to touch that. 我不想碰那个。 I prefer loading the the config file in 'environments/.rb'. 我更喜欢在'environments / .rb'中加载配置文件。 Append this line at the end of file. 在文件末尾附加此行。

# File: 'config/environments/production.rb'
# Load external configuration 
load '/etc/prod_env/overrides.rb', :if_exists? => true

path could be anything,i have placed my file in '/etc/prod_env/'. 路径可以是任何东西,我已将我的文件放在'/ etc / prod_env /'中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM