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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)