Rails数据库密码作为环境变量

时间:2021-05-16 23:04:56

This works:

这有效:

production:
  adapter: mysql2
  encoding: utf8
  host: localhost
  database: myapp
  username: myapp
  password: jksdfUIJsdf

Then on terminal touch tmp/restart.txt.

然后在终端上触摸tmp / restart.txt。

This does not work:

这不起作用:

production:
  adapter: mysql2
  encoding: utf8
  host: localhost
  database: myapp
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Then on terminal

然后在终端上

export MYAPP_DATABASE_PASSWORD=jksdfUIJsdf
touch tmp/restart.txt

So if I set the password as plaintext in database.yml file then my application works properly but if I set the password as environment variable with export command, then my application does not work because it gives error password missing. I am using mysql database. How to solve this?

因此,如果我在database.yml文件中将密码设置为明文,那么我的应用程序正常工作但如果我使用export命令将密码设置为环境变量,那么我的应用程序不起作用,因为它会丢失错误密码。我正在使用mysql数据库。怎么解决这个?

1 个解决方案

#1


1  

For Rails app configuration, I use figaro gem.

对于Rails app配置,我使用figaro gem。

Add the below line to your Gemfile and do bundle.

将以下行添加到您的Gemfile并进行捆绑。

gem "figaro"

After that run the below command:

之后运行以下命令:

bundle exec figaro install

This will create config/application.yml and also will add it to the .gitignore file.

这将创建config / application.yml,并将其添加到.gitignore文件中。

Now inside config/application.yml enter the credentials.

现在在config / application.yml里面输入凭据。

# config/application.yml

MYAPP_DATABASE_PASSWORD: "2954"

Visit the Github page for more info.

访问Github页面了解更多信息。

#1


1  

For Rails app configuration, I use figaro gem.

对于Rails app配置,我使用figaro gem。

Add the below line to your Gemfile and do bundle.

将以下行添加到您的Gemfile并进行捆绑。

gem "figaro"

After that run the below command:

之后运行以下命令:

bundle exec figaro install

This will create config/application.yml and also will add it to the .gitignore file.

这将创建config / application.yml,并将其添加到.gitignore文件中。

Now inside config/application.yml enter the credentials.

现在在config / application.yml里面输入凭据。

# config/application.yml

MYAPP_DATABASE_PASSWORD: "2954"

Visit the Github page for more info.

访问Github页面了解更多信息。