MySQL Client Environment Variables for USER and DB on Linux

MySQL

I see that http://dev.mysql.com/doc/refman/5.7/en/environment-variables.html has an option for setting the USER on Windows, but is it possible on Linux? You can do it via the MYSQL_TEST_LOGIN_FILE, but it's a bit convoluted, and requires setting up files with just the right permissions and one for every user you want to have access. Also, is there an environment variable for what database to start using?

Best Answer

Not that I know of, but you can make a file called my.cnf in a directory that you specify with the MYSQL_HOME environment variable. For example say you have ~/mysql_home/db/my.cnf:

[mysql]
user = username
password= ****
host = hostname
db = databasename

Then you can call the client like

MYSQL_HOME=~/mysql_home/db/ mysql

and it will use those options. Please keep in mind that your password will be readable by whomever can read that file and it isn't encrypted!