How CURRENT_ROLE() Works in MySQL

In MySQL, CURRENT_ROLE() is a built-in function that returns the current active roles for the current session, separated by commas, or NONE if there are none. The current role determines our access privileges.

Syntax

The syntax goes like this:

CURRENT_ROLE()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT CURRENT_ROLE();

Result:

+----------------+
| CURRENT_ROLE() |
+----------------+
| NULL           |
+----------------+

In my case, there’s no current role and so NULL is returned.

For the purposes of this example, I ran the above code when logged in as 'bart'@'localhost'.

Let’s now use an administrator account to create a couple of roles:

CREATE ROLE analyst;
CREATE ROLE developer;

And grant the user access to them:

GRANT 'analyst', 'developer' TO 'bart'@'localhost';
SET DEFAULT ROLE ALL TO 'bart'@'localhost';

Now, let’s log in as 'bart'@'localhost' and select CURRENT_ROLE() again:

SELECT CURRENT_ROLE();

Result:

+-------------------------------+
| CURRENT_ROLE()                |
+-------------------------------+
| `analyst`@`%`,`developer`@`%` |
+-------------------------------+

Now, the roles we added are the current roles. That’s because the administrator added them as default roles for the 'bart'@'localhost' user.

No Arguments are Accepted

Passing arguments to CURRENT_ROLE() results in an error:

SELECT CURRENT_ROLE(20);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CURRENT_ROLE'