How to Configure MariaDB Client with Host-Specific Credentials

clientmariadbmariadb-10.2

How can I configure my MariaDB client (mysql) so that I don't have to enter a password to connect, regardless of which host I am connecting to?

I can't just put a password in the [client] section of ~/.my.cnf because that would apply all hosts.

For example, I'd like to be able to connection with mysql -h myhost mydatabase, ie. without specifying username or password. Even better would be the ability to specify a "host alias" instead of the actual (awkwardly long) hostname.

It sounds like this is possible in MySQL with the mysql-config-editor but MariaDB does not have this feature.

Best Answer

In ~/.my.cnf make sections like:

[clientmy_host_name_1]

[clientmy_host_name_2]

Then make an alias (assuming Linux-like OS - depending on shell, you can perhaps put the alias in ~/.bashrc_local):

alias mysql="mysql --defaults-group-suffix=\`hostname --short\`"

Now you can connect without specifying the password to any of the hosts for which you have stored the credentials in ~/.my.cnf. (These would be my_host_name_1 and my_host_name_2 in my example above.)