Pat C Pat C - 2 months ago 6
Linux Question

How to get mysql to use the current effective linux user as the default user in mysql?

If I login to a linux system as user alex, then change to user bob with su - bob, then as bob run mysql with no username arg specified, mysql will report (select user();) the user is alex.

I know I can do mysql -u bob to change the user, however I'm wondering if there's any way to configure mysql to use the current effective user (user reported by whoami) who ran the command mysql, bob in this example, to be used as the default when no username arg is specified to mysql? Thanks for any input.

Answer

Doesn't it work to create a .my.cnf file in the home directory of bob and put

[client]
user=bob

Alternatively you can also put password. Eg.

[client]
user=bob
password=1234

Then change the file permissions to read/write by owner (600)

Also explained at MySQL Manual

This way, you can just write 'mysql' and login without entering the password also.

PS. strangely in my system it uses the username of the whoever I became using 'su -' I am not sure why it is different in your machine. Manual says it defaults to unix username...