MySQL SYSTEM_USER() Explained

In MySQL, SYSTEM_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:

SYSTEM_USER()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT SYSTEM_USER();

Result:

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

SYSTEM_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 SYSTEM_USER().

For example, if I connect to MySQL like this:

mysql -uhomer

Then run SYSTEM_USER() and CURRENT_USER():

SELECT 
    SYSTEM_USER(),
    CURRENT_USER;

Result:

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

Here the client specified a username of homer (as shown by SYSTEM_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 SYSTEM_USER() results in an error:

SELECT SYSTEM_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 SESSION_USER() Function

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