Using strictly MySQL you can use the --login-path option
Read options from the named login path in the .mylogin.cnf login path file. A “login path” is an option group containing options that specify which MySQL server to connect to and which account to authenticate as. To create or modify a login path file, use the mysql_config_editor utility. See Section 6.6.7, “mysql_config_editor — MySQL Configuration Utility”.
MariaDB did not implement this argument, but you can use --defaults-group-suffix if you want to have a unique file centralizing all of your connections Let's say you have the following ~/.my.cnf
[client-foo] host = foo user = foo_user password = "foo_pass" [client-bar] host = bar user = bar_user password = "bar_pass"
You can then connect to foo with
mariadb --defaults-group-suffix=-foo
Please note that the syntax client- prefix in the group name is important. Also, if you want to have a file per connection, you can use --defaults-extra-file
Be aware, there are more aspects and differences between these two; everything is in the docs.
How can I add multiple accounts/credentials to different MySQL/MariaDB servers to be used with mysql command? Similar to .ssh/config and ssh.
You will not have the same level of customizability ~/.ssh/config offers but in the case you described it will work just fine.