~/.my.cnf
Create the file in your favourite editor.
vim ~/.my.cnf
Add the following content (adjust for YOUR user.)
[mysql]
user=YOURUSER
password=YOURPASS
host=127.0.0.1
[mysqldump]
user=YOURUSER
password=YOURPASS
host=127.0.0.1
Set the file permissions:
chmod 600 ~/.my.cnf
Now you can login to Mysql using:
mysql
Or dump a database with:
mysqldump --all-databases --single-transaction --events --routines --triggers | gzip > ./all_databases.sql.gz
Bonus
If you provision with Ansible, Chef or Puppet, you can generate this file for each of your server users or deployers using secure vault encryption for transmission to server. Be sure to follow best practice security if you do it this way.