5

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

[client] 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 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

10

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

0
2

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.

1
  • 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". Commented Apr 18, 2022 at 16:26
1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.