MySQL USER() Function

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 format user_name@host_name for 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() and SYSTEM_USER(). However, it might return different information compared to CURRENT_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, while CURRENT_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() and SESSION_USER() are typically the same, CURRENT_USER() may differ, especially in cases involving GRANT privileges or proxies.

Was this article helpful?