Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:… – Ibrahim Mar 8 '10 at 0:30
See… – Zoredache Mar 8 '10 at 0:31
And… – Zoredache Mar 8 '10 at 0:33
Yeah bleh, apparently my search-fu is lacking. – Ibrahim Mar 8 '10 at 0:51
up vote 4 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 '14 at 22:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.