MySQL CURRENT_USER() Function

The CURRENT_USER() function returns the user name and host name for the MySQL account that the server used to authenticate the current client connection.

This function is particularly useful for determining the user account under which the connection is made, including any privileges or restrictions that might be associated with that account.


Syntax

CURRENT_USER()

Definition and Usage

  • The CURRENT_USER() function returns the user and host from the MySQL authentication process.
  • This value represents the credentials used for authentication at the server level, not the user who is currently connected to the database.

Return Value

  • The result is a string in the format user_name@host_name, where user_name is the MySQL user, and host_name is the host from which the user is connected.

Technical Details

  • Works in: From MySQL 4.0
  • Character Set: The result is returned as a string in the UTF8 character set.

Examples

Example 1: Get the Current User

This will return the user name and host name for the MySQL account that the server used to authenticate the current client:

SELECT CURRENT_USER();

Example Output:

'username@localhost'

Example 2: Get the Current User After Connecting with Specific Credentials

When connecting to MySQL with a specific username and host, the CURRENT_USER() function will return those credentials:

-- Assuming you connected with 'root@localhost'
SELECT CURRENT_USER();

Example Output:

'root@localhost'

Comparison with USER() Function

  • The CURRENT_USER() function reflects the credentials used for authentication.
  • The USER() function returns the credentials of the current client, which may differ if you are logged in with a different user account after authentication.

For example:

  • USER() might return 'user1@localhost' if you are logged in as user1.
  • CURRENT_USER() might return 'admin@localhost' if you initially authenticated as admin.

Was this article helpful?