Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a /root/.my.cnf file which stores the mysql root user's password:

password = "my password"

When I log in as system root and enter mysql, I get a passwordless login:

myuser@local:$ sudo su
root@local:$ mysql

But when I try to do the same just using sudo, I get access denied:

myuser@local:$ sudo mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

How can I get sudo mysql to log me in as the mysql root user, without entering a password?

share|improve this question
up vote 4 down vote accepted

sudo -i mysql should also work - That should run mysql under a fresh interactive shell.

share|improve this answer

The command sudo su -c "mysql" will have the desired effect.

share|improve this answer

If you are locked down and the only command you can enter as sudo is mysqldump (non-interactive) you might be looking to add the .my.cnf option by the --defaults-file param.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.