Using roles
Besides granting permissions to individual users, in MySQL, it is also possible to create roles and grant permissions to roles and then assign roles to users. This makes handling groups of users with similar permissions much easier.
To create a role for webdeveloper
, we can provide the following query:
CREATE ROLE 'webdeveloper';
The next step is to assign some permissions to the role. This is done with GRANT
, just like how you did for the user permissions:
GRANT SELECT ON mysql.user TO 'webdeveloper';
To assign a role to a user, we need to use GRANT
as follows:
GRANT 'webdeveloper' TO 'johndoe'@'%';
An account can have no roles, a single role, or multiple roles. If a role is granted to your user, then you might need to tell MySQL which roles you want to use with the help of the following query:
SET ROLE 'webdeveloper';
Instead of having to do this every time or having to modify an application...