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?

3 Answers 3


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


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.

  • This won't work unless you have read permissions on the root my.cnf file. It you don't, it fails with "Could not open required defaults file: /root/.my.cnf Fatal error in defaults handling. Program aborted". Apr 18, 2022 at 16:26

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .