MySQL Revoke Privilege
We have already learned how to give access right from grant privileges to a user account. Now, we are going to learn about revoke privileges from a user account. MySQL provides REVOKE statements to remove privileges from a user account.
The revoke statement enables system administrators to revoke privileges and roles to the MySQL user accounts so that they cannot use the assigned permission on the database in the past.
The following are the basic syntax of using the REVOKE statement:
In the above syntax, we can have the following parameters:
MySQL supports the following privilege levels:
REVOKE Statement Example
Let us understand the REVOKE privileges through the example. First, we need to create a new user named "[email protected]" using the following statement:
Next, assign all privileges to all databases in the current server to [email protected], using the below statement:
Next, execute the SHOW GRANT statement to verify the privileges. In the output, we can see that all privileges are assigned to all databases in the current server to [email protected]
If we want to revoke all privileges assign to the user, execute the following statement:
We will get the output below where we can see that a user can log in to the database without any privileges.
REVOKE selected privilege from a user account
Suppose we have provided grant privilege of SELECT, INSERT, and UPDATE command on mystudentdb to the user with the following statement:
Next, display the GRANT privilege with the following statement:
Finally, execute the REVOKE statement to remove UPDATE and INSERT privilege with the below statement:
It will give the below output where only SELECT privilege is left.
REVOKE Proxy User Example
First, we need to grant the proxy privilege to the user whom you want using the following statement:
Next, display the GRANT privilege with the given statement:
Finally, execute the REVOKE statement to remove proxy privilege from the user with the below statement:
It will give the below output where proxy privilege is revoked successfully.
Revoking Privileges from Stored Routine Example
Here, the revoke privileges are applied to procedures and functions where we can revoke the privileges from the user who has a execute privilege in the past.
Let us understand it with the example. Suppose we have a function calculatesalary and want to grant EXECUTE privilege to a user john, run the following query:
If there is a need to revoke the EXECUTE privilege to the users, we must run the below command:
We can revoke privileges from the below list on which privileges can be applied.