MySQL Dynamic and Static PrivilegesWhen organizing things helps to simplify life.

In the previous article, we start to explore dynamic privileges and the interaction with static ones. We also saw how to remove SUPER privilege from a DBA account. 

What we did was go by subtraction. But in real life, we should act differently. We should ADD only what is really needed for the account to work correctly.

Adding privilege one by one, and for each user is problematic given the level of interaction they may have, and also prone to mistakes. 

Instead, we can use ROLES to group, assign, and revoke the correct privileges in a much easier way.

This is becoming even more important in MySQL with the advent of dynamic privileges.

What should we do to correctly use ROLES? Well first of all design.  

The first step is to identify the ROLES, in doing so we need to keep a basic principle, make it simple, as such let us try to avoid having too many ROLES, or ROLE with too many cross-functional privileges.

My proposal: 

  • DBA (The lord of the databases who can do all)
  • MaintenanceAdmin (DBA minions 🙂 they can perform only some action on the server, and server only)
  • UserAdmin (Can create users assign grants and so on)
  • MonitorUser (See all process and read from performance_schema)
  • DBManager (Can add/drop/modify schemas/tables/triggers/view/routines etc )
  • DBDesigner (Can modify specific objects mostly with clear identification by schema/table)
  • ReplicationAdmin (Can add/change/remove start/stop replication also GR)
  • BackupAdmin (Can take backup, cannot restore)

We have eight administrative ROLES and they should cover ALL we need for administrative tasks.

Now let us create them:

Let us check the roles one by one and see what privileges we need to assign.

Our test user does not have any grant:

DBA

Well, you may say .. easy GRANT ALL.

Wrong! As already indicated in the previous article, doing that will also assign SUPER, which is deprecated from MySQL 8.0.x. Let us start with the right foot and add ONLY what we need:

That should be exactly the same as GRANT ALL but without SUPER. 

To assign the ROLE to our test user:

Now our user has:

Correct you now see DBA as grant but that is not active:

To ACTIVATE a role you need to do it explicitly:

And have the user reconnect!

Once a role is activated we can also use:

To check which privileges are now active for a specific user.

We can also control which role is active for which user querying the table mysql.default_roles. 

To remove the active role:

Anyhow, NOW we have our DBA role available for all DBA and if we need to change something we can do it just there on the ROLE and not on every single user. 

MaintenanceAdmin

UserAdmin

MonitorUser

DBManager

DBDesigner

ReplicationAdmin

BackupAdmin

Once all our ROLES are in, we can test them. For instance, we can check our ReplicationAdmin checking the Binary Logs and stopping/starting our Group Replication (or normal Replication):

Also if created and assigned the role is not active. Let us now enable the role for the user:

Remember to reconnect!

And these are the privileges active:

Conclusion

Using the ROLES allows us to modify the needed privileges in one place, and at the same time will allow us to keep under control the possible proliferation of dynamic privileges due to the use of components or plugins, significantly reducing the complexity of having multiple privileges sources.

Roles are normally used in the most common databases and MySQL had implemented them quite late. But using roles is the natural evolution of how we should deal with user grants when moving from small platforms to medium/large. 

The time when we assign single user privileges IS GONE, welcome to 2021 MySQLers!

For your convenience, I am distributing a simple SQL file with all commands to create the Roles as described in this article.

References

https://dev.mysql.com/doc/refman/8.0/en/roles.html

Some queries related to MySQL Roles

MySQL 8.0: Listing Roles

 

MySQL 8.0 Roles and Graphml

Percona Distribution for MySQL is the most complete, stable, scalable, and secure, open-source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!

Download Percona Distribution for MySQL Today

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments