Read more

Store MySQL passwords for development

Florian Heinle
March 07, 2018Software engineer at makandra GmbH

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.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

Posted by Florian Heinle to makandra dev (2018-03-07 13:26)