MySQL CURRENT_USER() Function
MySQL CURRENT_USER() Function
The MySQL CURRENT_USER() function returns the user_name@host_name account that the MySQL server used to authenticate the current session and check privileges. It reveals the effective privilege account rather than the client-supplied username.
Syntax
CURRENT_USER()
-- or without parentheses:
CURRENT_USER
- Takes zero arguments. Both
CURRENT_USER()andCURRENT_USERreturn aVARCHARstring such as'app_reader@10.0.0.%'or'root@localhost'.
This distinction matters when MySQL matches a wildcard host rule or anonymous account during login, or when executing a SQL SECURITY DEFINER view or stored routine.
Practical Example
When auditing permissions for queries against demo_employees, compare CURRENT_USER() against USER() to see both the authenticated privilege rule and the literal login host.
SELECT
CURRENT_USER() AS authenticated_account,
USER() AS client_login_account,
DATABASE() AS active_schema;
| authenticated_account | client_login_account | active_schema |
|---|---|---|
| app_svc@10.0.% | app_svc@10.0.4.18 | koder_demo |
Common Mistakes & NULL Behavior
- NULL behavior:
CURRENT_USER()accepts no arguments and never returnsNULLin an active session. Passing any value inside the parentheses produces a syntax error. - Confusing
CURRENT_USER()withUSER():USER()returns the exact string your client sent (app_svc@10.0.4.18).CURRENT_USER()returns the matching grant entry inmysql.user(app_svc@10.0.%), which determines what privileges you actually hold. - Views and triggers: Inside a
SQL SECURITY DEFINERview or stored procedure,CURRENT_USER()returns the definer's account, not the caller's account. UseUSER()if you want the caller's login identity. - Role grants: In MySQL 8.0,
CURRENT_USER()still returns the authenticated user account; checkCURRENT_ROLE()to see active roles.
Frequently Asked Questions
Are parentheses required for CURRENT_USER in MySQL?
No. MySQL accepts both CURRENT_USER() and CURRENT_USER as synonyms.
Why does CURRENT_USER() show % in the host part?
Because MySQL matched your connection against a wildcard grant such as 'app_user'@'%' or 'app_user'@'10.0.%' in the mysql.user privilege table.
Which function should I use for an audit log of who ran a query?
Use USER() (or SESSION_USER()) when you want the client's actual IP/host and caller identity, even inside DEFINER views.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.