CURRENT_USER() returns the user name and host name that MySQL used to authenticate the current connection, not the value of the USER() or DEFINER clauses.
MySQL’s CURRENT_USER()
function outputs the authenticated account in the form 'user_name'@'host_name'
. It reflects the privileges applied to the session, making it essential for auditing and security checks.
Use SELECT CURRENT_USER();
in any query window.The function takes no arguments and can be aliased like any regular column.
USER()
shows the account you attempted to connect with, while CURRENT_USER()
shows the account actually used after authentication (e.g., when a proxy user maps to another account).
Yes.Embed it in SELECTs or JOINs to capture the executing account alongside order metrics or customer activity for auditing.
This query returns each order’s total and the user who ran the report, useful for logging exports:
SELECT o.id, o.total_amount, CURRENT_USER() AS executed_by
FROM Orders o
WHERE o.order_date >= '2024-01-01';
• Use CURRENT_USER()
in stored procedures to log actor identity.
• Include it in ETL audit tables to trace automated jobs.
• Compare CURRENT_USER()
against expected service accounts to detect privilege drift.
• Confusing CURRENT_USER()
with SESSION_USER()
; MySQL supports only the former.
• Assuming it changes after SET ROLE
; roles affect privileges but not the function’s output.
.
No. Within a DEFINER context, CURRENT_USER()
still returns the session’s authenticated user, not the DEFINER account.
Yes. Use it in WHERE clauses for row-level security, e.g., WHERE created_by = CURRENT_USER()
.
No extra privileges are needed; any authenticated account can call the function.