The USER() function returns the current user name and host name for the MySQL connection.
Syntax
USER()
Definition and Usage
- The
USER()function returns the user name and host name in the formatuser_name@host_namefor the MySQL connection. - This function helps identify the user who is currently connected to the MySQL server.
- Note: This function is equivalent to both
SESSION_USER()andSYSTEM_USER(). However, it might return different information compared toCURRENT_USER().
Technical Details
- Works in: From MySQL 4.0
- Note: The value returned by
USER()reflects the actual user who connected to the MySQL server, whileCURRENT_USER()shows the user MySQL used to authenticate the current session.
Examples
Example 1: Retrieve the current user and host
SELECT USER();
Result:
'username@hostname'
This will return the user name and host name of the current MySQL session.
Example 2: Compare USER() and CURRENT_USER()
SELECT USER(), CURRENT_USER();
Result:
'username@hostname' 'authenticated_user@host'
USER()returns the actual user connected.CURRENT_USER()shows the authenticated user, which could be different due to factors like proxy users or privilege settings.
Usage Notes
- If you are troubleshooting connections,
USER()helps determine the client connection's identity. - While
USER()andSESSION_USER()are typically the same,CURRENT_USER()may differ, especially in cases involvingGRANTprivileges or proxies.