Take the 2-minute tour ×

I'm writing a bash script to back up my databases. Most are postgresql, and in postgres there's a way to avoid having to authenticate by creating a ~/.pgpass file which contains the postgres password. I put this in root's home directory and made it chmod 0600, so that root could dump the postgres databases without having to authenticate. Now I want to do something similar for mysql, although I only have one mysql database. How can I do this? I don't want to specify the password on the command line for mysqldump because this is part of a script that might be somewhat visible to other users. Is there a better way (i.e. built in to mysql) to do this than make a file that only root can read and then read that to get the mysql password, and then use that in the bash script as a variable?

share|improve this question
Hmm, strangely, I found something promising through google rather than serverfault's own search: stackoverflow.com/questions/601995/… –  Ibrahim Mar 8 '10 at 0:30
See serverfault.com/questions/110738/… –  Zoredache Mar 8 '10 at 0:31
And serverfault.com/questions/56341/… –  Zoredache Mar 8 '10 at 0:33
Yeah bleh, apparently my search-fu is lacking. –  Ibrahim Mar 8 '10 at 0:51

1 Answer 1

up vote 3 down vote accepted

Create a ~/.my.cnf file for the user running mysql. It should contain the following:

user = root
password = yourpassword
share|improve this answer
One thing I guess to add is that if running the script using sudo, you need to use -H to make sure that it uses root's home directory. But I think that problem shouldn't be an issue in a root cron job, correct? –  Ibrahim Mar 8 '10 at 0:53
But how is that handled for multiple accounts? That'll work for localhost, but what if I want to store my login for a remote server? –  Cerin Apr 30 at 22:27

Your Answer


