Posted over 1 year ago. Visible to the public.

Store MySQL passwords for development

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.


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.

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Florian Heinle
Last edit:
over 1 year ago
by Florian Heinle
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Florian Heinle to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more