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?


1 Answer 1


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

user = root
password = yourpassword
  • 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
    Commented Mar 8, 2010 at 0:53
  • 3
    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
    Commented Apr 30, 2014 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .