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?
- Hmm, strangely, I found something promising through google rather than serverfault's own search: stackoverflow.com/questions/601995/…Ibrahim– Ibrahim2010-03-08 00:30:34 +00:00Commented Mar 8, 2010 at 0:30
- 1See serverfault.com/questions/110738/…Zoredache– Zoredache2010-03-08 00:31:41 +00:00Commented Mar 8, 2010 at 0:31
- 1And serverfault.com/questions/56341/…Zoredache– Zoredache2010-03-08 00:33:05 +00:00Commented Mar 8, 2010 at 0:33
- Yeah bleh, apparently my search-fu is lacking.Ibrahim– Ibrahim2010-03-08 00:51:23 +00:00Commented Mar 8, 2010 at 0:51
Add a comment |
1 Answer
Create a ~/.my.cnf file for the user running mysql. It should contain the following:
[client] 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– Ibrahim2010-03-08 00:53:08 +00:00Commented Mar 8, 2010 at 0:53
- 3But 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– Cerin2014-04-30 22:27:34 +00:00Commented Apr 30, 2014 at 22:27