MySQL SESSION_USER() Function

The SESSION_USER() function returns the current user name and host name for the MySQL connection.


Syntax

SESSION_USER()

Definition and Usage

  • The SESSION_USER() function provides information about the user name and host name associated with the current MySQL connection.
  • This function is typically used to determine which user is connected to the MySQL session.
  • It returns a string in the format user_name@host_name.

Technical Details

  • Works in: From MySQL 4.0
  • Note: This function is functionally equivalent to SYSTEM_USER() and USER(), but SESSION_USER() specifically relates to the current session.

Examples

Example 1: Retrieve the current session user and host name

SELECT SESSION_USER();

Result:

'username@hostname'

This will return the user name and host name associated with the current session.

Example 2: Use with other session details

SELECT SESSION_USER(), NOW();

Result:

'username@hostname'    2024-12-26 12:34:56

This example returns both the session user and the current timestamp.


Usage Notes

  • If you are interested in the authenticated user that initiated the connection to MySQL, SESSION_USER() is useful.
  • It may return different results if a user has connected with different privileges or hosts for different sessions.

Was this article helpful?