MySQL CURRENT_USER

The CURRENT_USER() function in MySQL is used to return the current user name and host name combination for the MySQL session. It is often employed to retrieve information about the user who is currently connected to the MySQL server. The function provides details in the format ‘user_name’@’host_name’.

Syntax

Here is the syntax for the CURRENT_USER() function:

CURRENT_USER()

No parameters are required for this function, as it simply returns the current user and host information associated with the MySQL session.

Example

Let’s look at an example to illustrate the usage of the CURRENT_USER() function:

-- Example 1: Basic usage
SELECT CURRENT_USER() AS CurrentUser;

This query will return a result set with a single column named CurrentUser containing the current user and host information.

Output:

+----------------+
| CurrentUser    |
+----------------+
| user_name@host |
+----------------+

In this example, “user_name” represents the MySQL username, and “host” represents the host from which the user is connected.

It’s important to note that the CURRENT_USER() function is a convenient way to obtain information about the user without requiring any additional parameters. However, if you only need the user name or host separately, you can use the USER() and CURRENT_HOST() functions, respectively.

-- Example 2: Using USER() and CURRENT_HOST() functions
SELECT USER() AS UserName, CURRENT_HOST() AS CurrentHost;

Output:

+------------+--------------+
| UserName   | CurrentHost  |
+------------+--------------+
| user_name  | host         |
+------------+--------------+

In Example 2, USER() returns the current user name, and CURRENT_HOST() returns the current host name.

In summary, the CURRENT_USER() function in MySQL is a straightforward way to retrieve information about the current user and host combination associated with the active MySQL session.