Mysql – How to make MySQL client read password from thelogin.cnf

my.cnfMySQLmysql-5.6password

I'm trying to make the mysql client connect to a mysql server without requiring the password to be given interactively.
Steps taken:

1) First create a mylogin.cnf file

$ mysql_config_editor set --user=<user> --password --host=<host>
Enter password:

2) File created successfully:

$ ls -la .mylogin.cnf
-rw-------. 1 urmt urmt  136 Dec 19 11:01 .mylogin.cnf
$ mysql_config_editor print --all
[client]
user = <user>
password = *****
host = <host>

3) Connect using mysql client

$ mysql <dbname>
ERROR 1045 (28000): Access denied for user '<user>'@'<host>' (using password: NO)

Is there a default value/configuration somewhere that makes the client ignore the password in mylogin.cnf? The user and host properties were read correctly from the file.

I am able to connect just fine if I provide the password on the command line:

$ mysql -p <dbname>
Enter password: 
Reading table information...
...
mysql> 

MySQL client version is 5.6.22, MySQL Server version is 5.6.22, both on Oracle Linux 6. Client and server are on different hosts.

Thanks

Best Answer

Any way you do this, you're probably going to have a password saved somewhere. Even MySQL 5.6 login-path is easily decryptable by anyone with the motivation. That warning said, this would be an easy solution.

In your environment script (eg ~/.profile or ~/.bashrc), set

alias mysql='mysql -uUser -pPasswd -hHostname'

(putting in your desired User, Passwd, and Hostname, of course.)

After shell relogin, you should be able to simply do

mysql

... which will use your alias and connect to the DB and ignore any passwords in .my.cnf files.

Security note: If there are other users on your server, you might want to chmod 700 your profile script so the password is not as easily accessed by others. Any admin with root or sudo will be able to see it still; no way around that.