If you are using MySQL/5.6.6 or greater you can use a bundled tool called mysql_config_editor:
The mysql_config_editor
utility [...] enables you to store
authentication credentials in an encrypted login path file named
.mylogin.cnf
. The file location is the %APPDATA%\MySQL
directory
on Windows and the current user's home directory on non-Windows
systems. The file can be read later by MySQL client programs to obtain
authentication credentials for connecting to MySQL Server.
With such tool, you can assign a number of named credentials ("login paths"):
$ mysql_config_editor set --login-path=backup-user --host=localhost --user=backup --password
Enter password:
$ mysql_config_editor print --all
[backup-user]
user = backup
password = *****
host = localhost
... which are can be used later by clients that support the feature (such as the official command-line client or mysqldump):
$ mysql --login-path=backup-user
Welcome to the MySQL monitor. Commands end with ; or \g.
Please note that this doesn't really encrypt passwords (credentials at .mylogin.cnf
are obfuscated only), it just moves them away from your scripts.
Of course, having 5.6.6+ is the main reason of getting «Warning: Using a password on the command line interface can be insecure» in the first place ;-)