[mysql] Is there a way to know your current username in mysql?

I would like to know if there's a way for a mysql query to return the username of the user that issues the query.

Is this possible?

This question is related to mysql

The answer is


Try to run either

SELECT USER();

or

SELECT CURRENT_USER();

It can sometimes be different, USER() will return by which login you attempted to authenticate and CURRENT_USER() will return how you were actually allowed to authenticate.


You can find the current user name with CURRENT_USER() function in MySQL.

for Ex: SELECT CURRENT_USER();

But CURRENT_USER() will not always return the logged in user. So in case you want to have the logged in user, then use SESSION_USER() instead.


You can also use : mysql> select user,host from mysql.user;

+---------------+-------------------------------+
| user          | host                          |
+---------------+-------------------------------+
| fkernel       | %                             |
| nagios        | %                             |
| readonly      | %                             |
| replicant     | %                             |
| reporting     | %                             |
| reporting_ro  | %                             |
| nagios        | xx.xx.xx.xx                 |
| haproxy_root  | xx.xx.xx.xx
| root          | 127.0.0.1                     |
| nagios        | localhost                     |
| root          | localhost                     |
+---------------+-------------------------------+

to print the current user who is using the database

select user();

or

select current_user();

Use this query:

SELECT USER();

Or

SELECT CURRENT_USER;

You can use:

SELECT USER();

or

SELECT CURRENT_USER();

See more here http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user