60

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?

xenoterracide
  • 2,921
  • 5
  • 31
  • 33

3 Answers3

83

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
Derek Downey
  • 23,568
  • 11
  • 79
  • 104
11

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 mysql, mysqladmin and mysqldump.

the
  • 343
  • 1
  • 3
  • 12
10

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

PerroVerd
  • 246
  • 2
  • 4