MySQL SESSION_USER() Explained

In MySQL, SESSION_USER() is a synonym for the USER() function. It returns the current MySQL user name and host name, given when connecting to MySQL.

Syntax

The syntax goes like this:

SESSION_USER()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT SESSION_USER();

Result:

+-----------------+
| SESSION_USER()  |
+-----------------+
| homer@localhost |
+-----------------+

SESSION_USER() vs CURRENT_USER()

There’s also another function called CURRENT_USER() that does a similar thing. However, it doesn’t always return the same result as SESSION_USER().

For example, if I connect to MySQL like this:

mysql -uhomer

Then run SESSION_USER() and CURRENT_USER():

SELECT 
    SESSION_USER(),
    CURRENT_USER;

Result:

+-----------------+--------------+
| SESSION_USER()  | CURRENT_USER |
+-----------------+--------------+
| homer@localhost | @localhost   |
+-----------------+--------------+

Here the client specified a username of homer (as shown by SESSION_USER()), but the server authenticated the client using an anonymous user account (as shown by the CURRENT_USER function).

In this case I connected to MySQL using the homer username but I omitted the password.

No Arguments are Accepted

Passing any arguments to SESSION_USER() results in an error:

SELECT SESSION_USER(123);

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123)' at line 1

The SYSTEM_USER() Function

MySQL has a SYSTEM_USER() function that is also a synonym for USER(). Any of the examples above can be done with all three functions.