ryuken
ryuken

Reputation: 85

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.

Example: Login as user alex

[alex@gc-instance-1 ~]$ mysql -e 'select user()'
+----------------+
| user()         |
+----------------+
| alex@localhost |
+----------------+
[alex@gc-instance-1 ~]$ su - bob
Password: 
[bob@gc-instance-1 ~]$ mysql -e 'select user()'
+----------------+
| user()         |
+----------------+
| alex@localhost |
+----------------+
[bob@gc-instance-1 ~]$ 

Upvotes: 2

Views: 178

Answers (1)

Evren Yurtesen
Evren Yurtesen

Reputation: 2349

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...

Upvotes: 2

Related Questions