0

In my rails app I have this configuration for database

  adapter: mysql2
  host: *****
  username: *****
  password: <%= ENV['MYSQL_PW'] %>
  database: *****
  encoding: utf8
  timeout: 5000
  pool: 5

It is working perfectly in the server. But recently there was a bug and I tried to access rails console, but I get this error

 Access denied for user '****' (using password: NO) (Mysql2::Error).

I also I tried to run migration and I get same error again. I don't understand what is the problem here. How can I solve this? Also how can I check if ENV['MYSQL_PW'] is set in the unix environment variable?

Here is my log

$ rake db:migrate
DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in <top (required)> at )
DEPRECATION WARNING: You did not specify a `log_level` in `production.rb`. Currently, the default value for `log_level` is `:info` for the production environment and `:debug` in all other environments. In Rails 5 the default value will be unified to `:debug` across all environments. To preserve the current setting, add the following line to your `production.rb`:

   config.log_level = :info

. (called from block in tsort_each )
rake aborted!
Mysql2::Error: Access denied for user '****' (using password: NO)
asdlfkjlkj
  • 2,258
  • 6
  • 20
  • 28

2 Answers2

0

Don't forget to specify RAILS_ENV when you are running your commands.

By default rails assumes the environment is development, but as I see here you want to run those on production.

Simply do

 RAILS_ENV=production rake db:migrate
 RAILS_ENV=production rails c
 # or alternatively
 rails c -e production

http://guides.rubyonrails.org/command_line.html

mswiszcz
  • 980
  • 1
  • 8
  • 26
0

Try the following:

  1. Edit database.yml password to be a string and not an ENV variable, to identify the problem.
  2. To test the ENV Variable, log in the rails console with rails c and input ENV['YourVariable'] to see if it is set
  3. You can solve this problem by sourcing your bash_profile file if you have Unix system (linux/ubuntu/mac) in the terminal input source ~/.bash_profile
  4. If you have Rails 4 upon you should run the following terminal command spring stop
  5. In database.yml you should include the ENV Variable with the following syntax, beacuse yml needs the .erb syntax. <%= ENV['YOUR VARIABLE'] %> Failing to access environment variables within `database.yml` file
  6. The ENV Variable is Case Sensitive

I had this problems and I was able to solve it, but I keep having them. For this reason I read several discussions: Rails 4.1 environment variables not reloading

Sorry if I was not able to help more

Fabrizio Bertoglio

Community
  • 1
  • 1
Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57