Table of contents
1.
Introduction
2.
Setting default roles
2.1.
Example
3.
Setting active roles
4.
Revoking Privilege From A Role
5.
Remove A Role
6.
Copy Privileges From User Accounts
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

MySQL Manage Roles | Part - 2

Author Ankit Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Setting active roles

A user account can modify the current user's effective privileges within the current session by specifying which granted roles are active.

If we wish to have no active roles, we can execute the following query.

SET ROLE NONE;

On the contrary, If we wish to set all the granted roles active, we can use the following command.

SET ROLE ALL;

We can use the following query to set active roles to the default roles made by executing the SET DEFAULT ROLE command.

SET ROLE DEFAULT;

If we wish to take active roles, particularly to some of them, we can write their name with the necessary commands in the following manner.

SET ROLE

         [Granted role1, Granted role2…..];

This was all about setting active roles. In the following section, we will learn how to revoke any privilege from the role.

Revoking Privilege From A Role

It may sometimes happen that the admin needs to remove a particular privilege from the role to meet the task's requirements. So Instead of creating a new role with non-omitted privileges, we can directly revoke the privilege from the role in MySQL.

Suppose we need to revoke the Insert and Delete privileges from the BMS_ANALYST role, then we can execute the following query.

REVOKE INSERT, DELETE

On BMS.*

FROM BMS_ANALYST;

And not only this we can regrant the privileges if needed in the following manner.

GRANT INSERT, DELETE

On BMS.*

FOR BMS_ANALYST;

Let us now see how we can remove a role in MySQL.

Remove A Role

MySQL also offers a way to remove a role from it with the help of DROP Command. We can do it in the following syntax.

DROP ROLE role_name;

For example, if we wish to remove the BMS_ANALYST role, it could be done with the following command.

DROP ROLE BMS_ANALYST;

This was how we could remove a role. Let us now see how we can copy privileges from other user accounts.

Copy Privileges From User Accounts

We may need some of the privileges for a newly created user account. To accomplish this task, firstly, we need to create a new user, and then we can execute the following commands to copy privileges.

GRANT bms_analyst1@localhost 

TO bms_analyst1@localhost;

The above command will copy all the privileges acquired by the user ‘bms_analyst1’ to the new user ‘bms_analyst2’.

In this manner, we end this blog and will now discuss some of the frequently asked questions related to this topic.0

FAQs

  1.  Is it possible to regrant the revoked privileges to the roles?
     Yes! We need to reuse the 'GRANT' command to regrant privileges to the roles.
  2. How can we create a new user in MySQL?
    We can create a new user in MySQL with the help of the following query.
    CREATE USER USER_NAME@LOCALHOST; 
  3. Is it necessary to set default roles?
    No! It is not necessary to do it, but if you want to be able to access the database for the user, you need to set default roles.
  4. How we can show already assigned privileges?
    We can use the SHOW PRIVILEGES command in MySQL to show the already assigned privileges of the user account.
  5. What are the various operation that can be performed on MySQL Roles?
    We may perform various operations such as setting default roles, active roles, revoking a role or privilege, and copying privileges from user accounts

Let us now summarize our learnings!

Key Takeaways

In this article, we have extensively discussed MySQL Manage roles and their implementation in MySQL. We learned so many useful operations that we can perform after creating a role and implementing them. We also answered some of the frequently asked questions related to this topic. 

Recommended Readings:

We hope that this blog has helped you enhance your knowledge regarding MySQL Manage roles and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass