Open In App

MySQL | Grant / Revoke Privileges

We have already learned about how to create users in MySQL using MySQL | create user statement. However, using the Create User Statement only creates a new user but does not grant any privileges to the user account. Therefore to grant privileges to a user account, the GRANT statement is used.

Syntax:



GRANT privileges_names ON object TO user;

Parameters Used



Privileges: The privileges that can be granted to the users are listed below along with the description:

Grant Privileges on Table

Let us now learn about different ways of granting privileges to the users:

  1. To grant Select Privilege to a table named “users” where User Name is Amit, the following GRANT statement should be executed.
  2. The general syntax of specifying a username is: ‘user_name’@’address’
  3. If the user ‘Amit’ is on the local host then we have to mention it as ‘Amit’@’localhost’. Or suppose if the ‘Amit’ username is on 192.168.1.100 IP address then we have to mention it as ‘Amit’@’192.168.1.100’.

‘user_name’@’address’ – When you’re granting or revoking permissions in MySQL, you use the ‘username’ or ‘hostname’ format to tell which users are allowed or denied. This is important for keeping security and access control in place, so here’s why we use it:

GRANT SELECT ON Users TO 'Amit'@'localhost;'
GRANT SELECT, INSERT, DELETE, UPDATE ON Users TO 'Amit'@'localhost';
GRANT ALL ON Users TO 'Amit'@'localhost';
GRANT SELECT  ON Users TO '*'@'localhost';

In the above example the “*” symbol is used to grant select permission to all the users of the table “users”.

GRANT EXECUTE ON [ PROCEDURE | FUNCTION ] object TO user; 

Different ways of granting EXECUTE Privileges

GRANT EXECUTE ON FUNCTION Calculatesalary TO 'Amit'@'localhost';
GRANT EXECUTE ON FUNCTION Calculatesalary TO '*'@'localhost'; 
GRANT EXECUTE ON PROCEDURE DBMSProcedure TO 'Amit'@'localhost'; 
GRANT EXECUTE ON PROCEDURE DBMSProcedure TO '*'@'localhost'; 
SHOW GRANTS FOR  'Amit'@'localhost'; 

Output:

GRANTS FOR  Amit@localhost 
GRANT USAGE ON *.* TO `SUPER`@`localhost` 

Revoking Privileges from a Table

The Revoke statement is used to revoke some or all of the privileges which have been granted to a user in the past.

Syntax:

REVOKE privileges ON object FROM user;

Parameters Used:

Privileges can be of the following values

Revoke Privileges on Table

Different Ways of revoking privileges from a user

REVOKE SELECT ON  Users FROM 'Amit'@'localhost'; 
REVOKE SELECT, INSERT, DELETE, UPDATE ON Users FROM 'Amit'@'localhost'; 
REVOKE ALL ON Users FROM 'Amit'@'localhost'; 
REVOKE SELECT  ON Users FROM '*'@'localhost'; 
REVOKE EXECUTE ON [ PROCEDURE | FUNCTION ] object FROM User; 
REVOKE EXECUTE ON FUNCTION Calculatesalary FROM 'Amit'@'localhost'; 
REVOKE EXECUTE ON FUNCTION Calculatesalary FROM '*'@'localhost'; 
REVOKE EXECUTE ON PROCEDURE DBMSProcedure FROM 'Amit'@'localhost'; 
REVOKE EXECUTE ON PROCEDURE DBMSProcedure FROM '*'@'localhost'; 

Article Tags :