Getting “Access denied for user 'root'@

2019-06-25 22:55发布

问题:

I deployed my Rails app and am getting 500 Errors on all pages. My production.log isn't showing anything (which is a problem), but I did a 'script/console production' and tried to run a simple query (User.find :first) and it throws this:

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

My database.yml file definitely has a password there and it's correct.

So, that plus no errors being logged to my production.log file is making me wonder what's up.

Any ideas where I'd start looking or what the issue could be?

Also, for what it's worth, I'm running Passenger on Apache.

UPDATE: Here's my database.yml file contents

development:
  adapter: mysql
  encoding: utf8
  database: website_development
  username: root
  password: secretz
  socket: /tmp/mysql.sock

test:
  adapter: mysql
  encoding: utf8
  database: website_test
  username: root
  password: secretz
  socket: /tmp/mysql.sock

production:
  adapter: mysql
  encoding: utf8
  database: website_production
  username: ttp_mysql
  password: secretz
  socket: /var/run/mysqld/mysqld.sock

NEW UPDATE: I changed the mysql user so it wasn't running in root, but now I'm still getting the "Access denied for 'root'@'localhost'" bit...even though in production mode it shouldn't be running as 'root' at all.

Really really confused now.

回答1:

So I found the issue. I had added some files to my /models folder for doing some database migrations from an old database.

In those files I had "ActiveRecord::Base.establish_connection" to connect to the database and somehow those were overriding what was in my database.yml file (even though I wasn't calling those files directly).

Either way...removing those solved the issue.

Thanks for taking the time to try to figure this out!



回答2:

The fact that it's trying to use the root account coupled with the fact that you're not seeing anything in your production.log file seems to suggest that the application isn't running in production mode.

To easily check if this is true or not, try adding the following line to the environment.rb file (near the top), restart the app, and see if you're still getting the same error:

ENV["RAILS_ENV"] = "production" 

Note: For obvious reasons, this is not good practice, so even if this works, I suggest finding out the root cause and removing this line from the file. However, seeing as you're seeing issues on your production server, I'm hoping this answer will at least get you (back) up and running while you continue to diagnose why it wasn't running in production mode in the first place.



回答3:

I had the same issue and realized that I hadn't restarted my rails server. Give that a try.



回答4:

I had same issue too. I closed all the terminals and restarted the server. It works fine!