Posted 7 months 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:

Copy
$ 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:

Copy
[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.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Florian Heinle
Last edit:
7 months 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