Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set rails environment variables in database.yml file?

On my local machine, I want to set environment variables for all of the sensitive information in the database.yml file. My file looks like this:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: <%= ENV['DATABASE_USERNAME'] %> 
  password: <%= ENV['DATABASE_PASSWORD'] %>
  socket: <%= ENV['SOCKET'] %>

development:
  <<: *default
  database: <%= ENV['DEVELOPMENT_DATABASE'] %>

test:
  <<: *default
  database: #JetStreamIQ-2_0_test

production:
  <<: *default
  database: <%= ENV['PRODUCTION_DATABASE'] %>
  username: <%= ENV['DATABASE_USERNAME'] %> 
  password: <%= ENV['DATABASE_PASSWORD'] %>

I thought that I could just set these environment variables in my .bashrc file, but that doesn't seem to be working. My .bashrc file looks like this:

export DATABASE_USERNAME="root"
export DATABASE_PASSWORD="*****"
export SOCKET="/var/run/mysqld/mysqld.sock"
export DEVELOPMENT_DATABASE="shoppe_development"
export PRODUCTION_DATABASE="#"

When I run my server with

rails s

I get an error that says:

Access denied for user 'root'@'localhost' (using password: YES)

I understand that there is a problem with the database username and password because of the way I've configured my database.yml file, but I'm just not sure what it is.

Is there something big I'm missing here? Any help would be much appreciated.

Thanks!

like image 628
Mark Avatar asked Apr 29 '16 22:04

Mark


2 Answers

I'd like to offer a couple of tips here.

  1. You can run database.yml through erb to test what you're getting. For example, erb config/database.yml. This will help you determine what the problem may be.

  2. You can find out every environment variable set in your shell with the set command. This will allow you to confirm that the environment variables you're expecting are being set (in .bashrc).

Hope this is helpful.

like image 94
Patrick Narkinsky Avatar answered Oct 29 '22 09:10

Patrick Narkinsky


It turns out that after I edited my bashrc file, I needed to exit out of that terminal session and open another terminal for the changes to be finalized :/ After I did that my application started perfectly.

like image 34
Mark Avatar answered Oct 29 '22 09:10

Mark