MySQL CURRENT_USER() Explained

In MySQL, CURRENT_USER() is a built-in function that returns the user name and host name combination for the MySQL account that the server used to authenticate the current client.

Syntax

The function can be called with or without the parentheses:

CURRENT_USER
CURRENT_USER()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT CURRENT_USER();

Result:

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

Without Parentheses

As mentioned, the CURRENT_USER() function can be called with or without parentheses.

Here’s an example without parentheses:

SELECT CURRENT_USER;

Result:

+-----------------+
| CURRENT_USER    |
+-----------------+
| homer@localhost |
+-----------------+

Same result.

Anonymous User

Here’s an example of what happens when connected as an anonymous user:

mysql

Run CURRENT_USER:

SELECT CURRENT_USER;

Result:

+--------------+
| CURRENT_USER |
+--------------+
| @localhost   |
+--------------+

CURRENT_USER() vs USER()

The CURRENT_USER() function doesn’t always return the same result as the USER() function (and its synonyms SYSTEM_USER() and SESSION_USER()).

Example:

SELECT 
    CURRENT_USER,
    USER(),
    SYSTEM_USER(),
    SESSION_USER();

Result:

+--------------+-----------------+-----------------+-----------------+
| CURRENT_USER | USER()          | SYSTEM_USER()   | SESSION_USER()  |
+--------------+-----------------+-----------------+-----------------+
| @localhost   | marge@localhost | marge@localhost | marge@localhost |
+--------------+-----------------+-----------------+-----------------+

Here the client specified a username of marge (as shown by USER() and its synonyms), 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 marge username but I omitted the password.

No Arguments are Accepted

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

SELECT CURRENT_USER(1);

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 '1)' at line 1