2

I have read other questions similar to this one. I feel that my situation is different enough to ask again for extra possible problem.

I have a Ruby on Rails app all setup. I have set up other rails projects before, so I am somewhat familiar with doing rails. My specific problem with this error:
Access denied for user 'root'@'localhost' (using password: YES)

Here is my database.yml file

default: &default
  adapter: mysql2
  host: localhost
  username: cmsapp
  password: <%= ENV['CMSAPP_PASSWORD'] %>     <-- this line is not working
  port: 3306
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: cms_dev
  socket: /var/run/mysqld/mysqld.sock 

production:
  <<: *default
  database: cms_prod

The problem I am having is: it seem to me that the password is not getting parsed or pulled from the ENV['CMSAPP_PASSWORD'] environment variable. I have double/triple check the password on the mysql db. If I put the literal password on the password: line,(which I am not supposed to do) it does connect. So I do know that the password is correctly set in mysql.

Ruby errors on this line:

connect user, pass, host, port, database, socket, flags

in the console at bottom of the page, I type in pass to see whats in the variable, and I get "<%= ENV[CMSAPP_PASSWORD] %>". if I type just 'ENV[CMSAPP_PASSWORD]' I get the correct password. So I know the right password is there, but I believe its sending the literal "<%= ENV[CMSAPP_PASSWORD] %>". So even though the correct password is in the environment variable, I still get the error: Access denied for user 'root'@'localhost' (using password: YES)

How do I troubleshoot weather the password is being properly extracted from the environment variable? Or how to figure out why it is not reading the environment variable?

Update 2016-06-09
I have switched back to the original mysql gem and all works as it is supposed to. I don't recommend anyone use the mysql2 gem. It DOES NOT have the database.yml file use environment variables. This is a security issue. I will not be using mysql2 gem until it can pull environment variables properly.

Thanks for any help.

detrix42
  • 107
  • 12
  • 1
    To troubleshoot, you can do `Rails.logger.info "mysql password: ENV['CMSAPP_PASSWORD']"` anywhere in your code and then check the logs if the password is printed there. If you find it's empty, double check that the *user* that runs the rails server actually has the env var defined (if it differs from the users you are logged as when testing the var in the console). – Matouš Borák Jun 05 '16 at 08:41
  • Do you already have `cms_dev` and `cms_prod` DB's created? Does `cmsapp` has access to create database in MySQL? Even if you've `cmsapp` user created, with correct password in MySQL, doing `rake db:create` with correct `ENV[CMSAPP_PASSWORD]` will ask for `root` users password if `cmsapp` does not have access to create DB. – Utsav Kesharwani Jun 05 '16 at 09:04
  • @UtsavKesharwani that is a big yes, I did double check to be sure databases are there. As I mentioned, if I put the literal password in the password: line, I do connect. – detrix42 Jun 05 '16 at 09:26
  • @BoraMa I have tried the logger in the Application.rb, and the application_controller.rb, and in my main_controller.rb. It seems that at the applicaition.rb point, Rails.logger is nil. Am I putting it in the wrong place? – detrix42 Jun 05 '16 at 09:29
  • @BoraMa got the logger to work. ENV['CMSAPP_PASSWORD'] outputted proper password. – detrix42 Jun 05 '16 at 09:35
  • 2
    How are you setting the `ENV['CMSAPP_PASSWORD']`? Also, I'm a bit doubtful if we're looking at it correctly. The error says: `Access denied for user 'root'@'localhost'`. Why is it prompting for `root` user? – Utsav Kesharwani Jun 05 '16 at 09:42
  • @UtsavKesharwani the CMSAPP is a different app, but having same problem. Not wanting to show my root password, figured the CMSAPP would work for showing whats going on. Other app, I am trying to not put the root password ind database.yml. I have the environment variables set in the /etc/bash.bashrc file. In the console window (bottom of the page with the error) I can dump the entire ENV hash, and MYSQLROOT_PASSWORD is there, and is the correct password. This is whats confusing me. – detrix42 Jun 05 '16 at 10:26
  • it seems to me that the database.yml file is not being parsed by ruby. I am using the mysql2 gem. Maybe I should go with mysql gem instead. – detrix42 Jun 05 '16 at 10:32
  • Did you try to restart webserver? – Johny Jun 05 '16 at 10:42
  • @Johny sure have. and restarted puma-manager several times – detrix42 Jun 05 '16 at 10:53
  • I think what you've done is correct. See: http://stackoverflow.com/a/8686483/2036529. The issue could be with some other conflicting gems, ex: https://github.com/laserlemon/figaro/issues/70. May be you could look in that direction, by removing non-critical gems one by one. – Utsav Kesharwani Jun 05 '16 at 10:54

0 Answers0