Introduction
Hello Ninjas! Before beginning this article, make sure you know all about creating a role in MySQL and the MySQL SHOW GRANTS Command. If you didn't know all these, read MySQL Manage Roles | Part -1. In this article, we will use some examples and key concepts from it. Here we will learn all of the different operations we can perform after creating the MySQL roles, such as setting default roles, setting active roles etc.
Let us begin with setting default roles in MySQL.
Setting default roles
In MySQL Manage Roles | Part -1, we had three types of users that needed access to the Banking Management System Database. All three types of users were granted different roles according to their needs. Now If a user 'BMS_CLIENT1' of type 'CLIENT', which has read-only access to the database, connects to the MySQL server and tries to access the database 'BMS', it will be denied by MySQL and will produce the following message.
ERROR 1045 (28000): Access denied for user 'BMS_CLIENT1'@'localhost' (using password: YES) |
It happened because we did not make the role active. So if we check which roles are active on our MySQL Workbench with the help of the SELECT Current_role(); command, it will display the following output.

Thus it is evident that we do not have any active roles on our MySQL server, and that is why the user 'BMS_CLIENT1' is denied by the server to access the database 'BMS'.
We use the SET DEFAULT ROLE command in MySQL to make the role active.
Let us see how we can do it.
Example
Suppose we need the user 'BMS_CLIENT1' to access the database and read the information present in the database, then we need to make it default with the help of the following command.
SET DEFAULT ROLE ALL TO bms_client1@localhost; |
After executing this query, all of its assigned privileges will get activated whenever this user connects to the MySQL server. Hence, it will be able to access the database easily.
So this was how we could set default roles to a particular user to exercise all the granted privileges.
In the next section, we will see how to set active roles in MySQL.