Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails does not properly read environmental variables in database.yml

My database.yml is set up as such:

development:
  adapter: mysql2
  encoding: utf8
  database: devel
  username: root
  host: localhost
  port: 3306
  timeout: 5000

# ... snip ...

production:
  adapter: mysql2
  encoding: utf8
  database: prod
  username: <%= ENV["DB_USER"] %>
  password: <%= ENV["DB_PASSWORD"] %>
  host: <%= ENV["DB_HOST"] %>
  port: 3306
  timeout: 5000

The issue is, though, despite setting my environmental variables, Rails is not reading these properly. When I start up my Phusion-backed Nginx server on EC2, I get this message:

Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)

I confirmed that Phusion is starting Rails in the production environment; I changed the usernames and hosts in the development and test configuration to unique names, and 'root'@'localhost' always showed up as the error message. In addition, if I hard-code the username and password into the YAML file, the server starts up properly.

I suspect that I'm not setting my environmental variables correctly because MySQL keeps claiming that I'm trying to log in as 'root' without a password. I initially had them exported from the .bashrc file, but after reading this question, I realized that this was not the proper way to do it. After searching around, I happened upon this blog post, so I followed the instructions and created these three files:

/usr/local/etc/env

export RAILS_ENV=production
export DB_PASSWORD= (snip)
export DB_USER=doorbells

/usr/local/etc/ruby_wrapper

#!/bin/sh

source /usr/local/etc/env
exec /home/rooby/.rvm/wrappers/ruby-1.9.3-p362@doorbells-dev/ruby "$@"

/etc/profile.d/env.sh

#!/bin/sh

source /usr/local/etc/env

In my nginx.conf file, I use this line:

passenger_ruby /usr/local/etc/ruby_wrapper;

I've rebooted my server to no avail. Any ideas?

like image 598
kibibyte Avatar asked Jan 15 '13 05:01

kibibyte


People also ask

Can Yaml read environment variable?

Environment variables can be used in the following file types: Markdown and MDX. YAML, including portal configuration files.

How do I see environment variables in rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

What is in database yml rails?

The database. yml is the file where you set up all the information to connect to the database. It differs depending on the kind of DB you use. You can find more information about this in the Rails Guide or any tutorial explaining how to setup a rails project.


2 Answers

I had the very same problem on Ubuntu 12.04.

In the nginx error log (/var/log/nginx/error.log/) I've seen, that

[ ... ]: [App 8509 stderr] /path/to/ruby_wrapper.sh: 2: /path/to/ruby_wrapper.sh: 
[ ... ]: [App 8509 stdout] 
[ ... ]: [App 8509 stderr] source: not found

/bin/sh seems to don't understand the source command. Thus, I've changed it to a simple ., which does the same.

The complete changed ruby_wrapper.sh would look like:

#!/bin/sh

. /usr/local/etc/env
exec /home/rooby/.rvm/wrappers/ruby-1.9.3-p362@doorbells-dev/ruby "$@"

Does this help for you?

like image 162
203 Avatar answered Nov 15 '22 10:11

203


The above solution did not work for me. However, I found the solution on How do I use variables in a YAML file?

My .yml file contained something like:

development:
gmail_username: <%= ENV["GMAIL_USERNAME"] %>
gmail_password: <%= ENV["GMAIL_PASSWORD"] %>

The solution looks like:

template = ERB.new File.new("path/to/config.yml.erb").read
processed = YAML.load template.result(binding)

So when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml as shown above.

like image 27
arpiagar Avatar answered Nov 15 '22 10:11

arpiagar