On your local system that only hosts non-critical development data and only you have access to, you can store MySQL's root password in your home directory so you can use mysql
commands without prompt for passwords, i.e. when doing batch processing.
Of course, this has security implications. The password must be stored in plain text, so this method is out of the question if there's any confidential data in your databases. Assuming diligent screen-locking, an encrypted hard disk and correct permissions set on your credential file, it should be reasonably secure for a development workstation.
Storing the password
As a regular user (not root
), create a file in your home directory: $HOME/.my.cnf
:
$ touch $HOME/.my.cnf
$ chmod 400 $HOME/.my.cnf
$ cat >> $HOME/.my.cnf <<EOF
[client]
user=root
password=YOURMYSQLROOTPASSWORDHERE
EOF
Now you should be able to just type mysql
and be logged in as MySQL's root user.
This works for different MySQL commands, too, e.g. mysqldump
, just add another paragraph to the $HOME/.my.cnf
file:
[client]
user=root
password=YOURMYSQLROOTPASSWORDHERE
[mysqldump]
user=root
password=YOURMYSQLROOTPASSWORDHERE
Opening a shell for a different user, using a different password, still works with mysql -uUSERNAME -pPASSWORD
.
MariaDB
The same method works for MariaDB, too, but in version 10.0.34 that comes with Ubuntu 16.04, you could instead just run mysql
as the system user root
, e.g. with sudo mysql
.