Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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 have the following in my ~/.my.cnf

[client]
password="somepass"

but this is not the password I use for every user@host/database I connect to. Is there some way to specify in the config different passwords for different things so I don't have to type them in?

share|improve this question
    
[User @DTest answered a question like this in May of this year][1] [1]: dba.stackexchange.com/questions/2820/… – RolandoMySQLDBA Jul 15 '11 at 19:32
up vote 27 down vote accepted

As I answered here, you can add a section for each user/host/db you connect to using the syntax in your ~/.my.cnf:

[clienthost1]   # Note: client + host1
user=myuser
password=mypass
database=dbname
host=server.location.com

Once this is in your user's .my.cnf, you can utilize it by doing this on a command line:

$ mysql --defaults-group-suffix=host1
share|improve this answer
    
As promised, +1 !!! – RolandoMySQLDBA Aug 23 '11 at 15:42
    
Could you explain this --defaults-group-suffix? – Otheus Oct 8 '15 at 14:09
    
Nice. It took me a while to figure the rest out from here but for an mysql Cron backup, I can now use mysqldump --defaults-group-suffix=host1 -P 3306 -h 111.0.0.xxx --ssl -u db_usr db_name > /home/myaccount/backups/db_name_$(echo $(date '+\%Y\%m\%d').sql.gz) to create a gzipped backup. Thank you! – user1324409 Dec 30 '15 at 18:48
    
Note: if you put this in a global my.cnf, such as /etc/mysql/my.cnf, but have a user-defined .my.cnf with [client] defined therein, the latter will override settings in the global file! boo. – Otheus Apr 5 at 8:24

Put clear passwords in text files is not recommended since mysql 5.6.6.

You can use mysql_config_editor to save passwords encrypted also to provide different passwords for different connections https://dev.mysql.com/doc/refman/5.6/en/mysql-config-editor.html

share|improve this answer
1  
Security by obscurity... – Federico Razzoli Jun 18 '15 at 14:04
    
+1 for mentioning mysql_config_editor – RolandoMySQLDBA Jun 18 '15 at 16:26
    
Until this program is (1) backported to previous versions, (2) allows automated password deployment (ie, not forcing passwords to be read on TTY), I do not recommend this method. – Otheus Apr 5 at 8:16

The other answer is correct. Unfortunately mysqladmin doesn't support --defaults-group-suffix (at least not the version I'm using).

Hence I resorted to using --defaults-file=HOST.cnf instead, which works for both mysql and mysqladmin (and mysqldump).

share|improve this answer
1  
Adding this to my .bash_profile made it even easier: alias my-host='mysql --defaults-file=HOST.cnf' – spyle Oct 20 '15 at 13:59

Your Answer

 
discard

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.