If importing a SQL dump file using the mysql
command line client, if it wasn't created by the superuser and guaranteed to not have been modified, for security it's best practice to not execute it as the root user.
I know this can be done using the -u
and -p
arguments, but is there a way to not have to know the password for the user if you already have the ability to log in as root (e.g. the root password is in /root/.my.cnf)? Like is there some way to log in as root then switch to the particular user, or some other easy way to accomplish this?
1 Answer 1
It appears that this is not directly possible. The common workaround appears to be specifying the password (and other parameters) in your $HOME/.my.cnf
as shown in this SO answer.
If the job is run from a systemd timer/service where $HOME is not in the environment then you can include the file explicitly with mysqldump --defaults-extra-file=/path/to/.my.cnf
. Note that this needs to be the very first argument to mysqldump
.
~/.my.cnf
and use only-u <user>
.su
without having to know their password.