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?

user67641
  • 1,292
  • 2
  • 14
  • 18

3 Answers3

10

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

Paul Doom
  • 841
  • 6
  • 9
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.

Drew Foehn
  • 21
  • 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". – Dario Seidl Apr 18 '22 at 16:26
1

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

Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58