MySQL Passwordless Login

August 12th, 2008 by Leandro Morgado

MySQL allows user specific options to be placed in a user options file under your home directory. This is handy if you use automated scripts or want to avoid typing in your MySQL user’s password everytime you login in. To do this, create the ~/.my.cnf with the options below:

[client]
user=mysqlusername
password=mysqlpassword

Then make sure no one else on the system can read this file. It’s essential for security as it contains your password in clear text!

$ chmod 600 ~/.my.cnf
$ ls -l ~/.my.cnf
-rw------- 1 alice alice 36 Aug 12 17:49 /home/alice/.my.cnf

Now you can just type mysql to login. Note that this will work for other MySQL clients like mysqldump. And you can always override the .my.cnf settings in the command line. For example:

$ mysql -ubob -pbobspassword

You can add other parameters to this file. For a full reference check out:
http://dev.mysql.com/doc/refman/5.1/en/option-files.html

Leave a Reply

You must be logged in to post a comment.