0

I need to run a command mysql_upgrade which, when ran as root, returns

Looking for 'mysql' as: mysql
Looking for 'mysqlcheck' as: mysqlcheck
Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/var/lib/mysql/mysql.sock' 
mysqlcheck: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect
FATAL ERROR: Upgrade failed

So I decided I need to log in as user mysql. How can I do it?

I set up the password for the user mysql with passwd and try to log in:

$ ssh -l mysql 12.34.56.78
mysql@12.34.56.78's password: 
Permission denied, please try again.

I logged in as root and set shell for mysql with chsh -s /bin/bash mysql. I checked that /etc/ssh/sshd_config's AllowUsers entry contains user mysql and DenyGroups contains only deniedssh; the latter group doesn't contain user mysql:

# getent group deniedssh
deniedssh:x:500:

What can I do to log in as the non-interactive user mysql?

1

Your reasoning is flawed, you do not need to log in on the shell as the mysql user. The authentication failures you see come from mysqld, you need to tell mysql_upgrade which username and password to use to talk to mysql, unix authentication is not involved at all here.

0

This is a common use case for sudo, which allows you to run commands (depending on configuration even a shell) as any user your system administrator has granted you to become (not only root, as most novices might think).

Depending on your sudo configuration you might be able to just do

$ sudo -s -u mysql

as your »normal« user account. You can check which users you are allowed to become by issuing

$ sudo -l

This could look like the following (my local installation and my »normal« user):

$ sudo -l
Matching Defaults entries for aw on swiftsure:
    env_keep+=HOME, env_keep+=XDG_SESSION_COOKIE, env_keep+="XMODIFIERS GTK_IM_MODULE QT_IM_MODULE QT_IM_SWITCHER", insults, tty_tickets, editor=/usr/bin/vim, !lecture

Runas and Command-specific defaults for aw:
    Defaults>root targetpw, env_file=/etc/sudo/root.env

User aw may run the following commands on swiftsure:
    (ALL) ALL
    (ALL) NOPASSWD: /usr/bin/htop, /usr/sbin/iotop, /usr/sbin/powertop
    (ALL) NOPASSWD: /usr/bin/wpa_cli
$

The last lines are the important ones. In this example case, it tells that user aw may run every command (ALL) as every user ((ALL)) and htop, iotop, powertop, and wpa_cli as every user ((ALL)) without needing to give a password (NOPASSWD).

If you're user has an entry (ALL) ALL you're fine and just can issue the above command to become the user mysql. If not, you might want to edit your /etc/sudoers file (visudo) and add a line containing

username      (ALL)=ALL

which would allow you to become every user (after typing username's password) or something like

username      (mysql)=ALL

which would only allow you to become the mysql user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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