Lesson

MySQL CURRENT_USER() Function

10 min reading
Free Course

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() and CURRENT_USER return a VARCHAR string 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 returns NULL in an active session. Passing any value inside the parentheses produces a syntax error.
  • Confusing CURRENT_USER() with USER(): USER() returns the exact string your client sent (app_svc@10.0.4.18). CURRENT_USER() returns the matching grant entry in mysql.user (app_svc@10.0.%), which determines what privileges you actually hold.
  • Views and triggers: Inside a SQL SECURITY DEFINER view or stored procedure, CURRENT_USER() returns the definer's account, not the caller's account. Use USER() if you want the caller's login identity.
  • Role grants: In MySQL 8.0, CURRENT_USER() still returns the authenticated user account; check CURRENT_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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum