How to Revoke Privileges From A User In MySQL?

7 minutes read

To revoke privileges from a user in MySQL, you can use the REVOKE statement followed by the specific privileges that you want to remove from the user. The syntax for revoking privileges is: REVOKE privilege_type ON database_name.table_name FROM 'username'@'host';


For example, if you want to revoke all privileges for a user named 'john' on a database named 'employees', you can use the following command: REVOKE ALL PRIVILEGES ON employees.* FROM 'john'@'localhost';


After executing this command, the user 'john' will no longer have any privileges on the 'employees' database. You can also revoke specific privileges, such as SELECT, INSERT, UPDATE, DELETE, etc., by replacing ALL PRIVILEGES with the specific privilege type that you want to revoke.


It is important to note that only users with the GRANT OPTION privilege can revoke privileges from other users. Additionally, make sure to use caution when revoking privileges, as it can impact the user's ability to interact with the database.

Best Managed MySQL Hosting Providers in 2024?

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


How to revoke privileges from multiple users in MySQL?

To revoke privileges from multiple users in MySQL, you can use the following steps:

  1. Connect to the MySQL server using the command line or a MySQL client.
  2. Run the following query to revoke the desired privileges from multiple users:
1
2
3
REVOKE privilege_name
ON database_name.table_name
FROM user1, user2, user3;


Replace "privilege_name" with the specific privilege you want to revoke (e.g. SELECT, INSERT, UPDATE, DELETE, etc.), "database_name.table_name" with the name of the database and table you want to revoke privileges from, and "user1, user2, user3" with the usernames of the users you want to revoke privileges from.

  1. After running the query, MySQL will revoke the specified privileges from the specified users.
  2. You can also run the FLUSH PRIVILEGES; command to reload the grant tables and apply the changes immediately.


It is important to note that you must have the necessary permissions to revoke privileges from users in MySQL.


How to revoke GRANT OPTION from a user in MySQL?

To revoke the GRANT OPTION from a user in MySQL, you can use the REVOKE command with the GRANT OPTION keyword. Here's the syntax you can use:

1
REVOKE GRANT OPTION ON *.* FROM 'username'@'hostname';


Replace 'username' with the name of the user you want to revoke the GRANT OPTION from and 'hostname' with the user's host name or IP address.


For example, if you want to revoke the GRANT OPTION from the user 'example_user' with host 'localhost', you would use the following command:

1
REVOKE GRANT OPTION ON *.* FROM 'example_user'@'localhost';


After running this command, the user will no longer be able to grant privileges to other users.


How to revoke privileges from a user in MySQL using command line?

To revoke privileges from a user in MySQL using the command line, you can use the following command:

1
REVOKE privilege_type ON database_name.table_name FROM 'username'@'localhost';


Replace privilege_type with the specific privilege you want to revoke, such as SELECT, INSERT, UPDATE, DELETE, etc. Replace database_name and table_name with the specific database and table the privileges are being revoked from. Replace username with the username of the user you want to revoke privileges from.


For example, to revoke the SELECT privilege from a user named john on a table named users in a database named mydatabase, you would use the following command:

1
REVOKE SELECT ON mydatabase.users FROM 'john'@'localhost';


After running the command, the user john will no longer have the SELECT privilege on the users table in the mydatabase database.


How to revoke privileges from a user in MySQL using a stored procedure?

You can create a stored procedure to revoke privileges from a user in MySQL by using the following steps:

  1. Create a stored procedure with the necessary parameters to revoke the required privileges from a user. Below is an example of a stored procedure that revokes all privileges from a user:
1
2
3
4
5
6
DELIMITER //
CREATE PROCEDURE revoke_user_privileges(IN username VARCHAR(50))
BEGIN
    REVOKE ALL PRIVILEGES ON *.* FROM username;
END //
DELIMITER ;


  1. Call the stored procedure and pass the username of the user from whom you want to revoke the privileges. For example:
1
CALL revoke_user_privileges('example_user');


This will revoke all privileges from the user 'example_user'.


Note: Make sure you have the necessary privileges to revoke privileges from users in MySQL.


What is the difference between REVOKE and DROP USER in MySQL?

The main difference between REVOKE and DROP USER in MySQL is that:

  • REVOKE is used to revoke specific privileges assigned to a user, without actually deleting the user account itself. This means that the user can no longer perform certain actions that were previously allowed, but the user account still exists in the database.
  • DROP USER is used to completely delete a user account from the database, including all privileges and associated data. Once a user account is dropped, it cannot be used to access the database or perform any actions.


In summary, REVOKE is used to remove specific privileges from a user, while DROP USER is used to delete the entire user account.


How to revoke only SELECT and INSERT privileges from a user in MySQL?

To revoke only SELECT and INSERT privileges from a user in MySQL, you can use the following SQL command:

1
REVOKE SELECT, INSERT ON database_name.* FROM 'username'@'localhost';


Replace 'database_name' with the name of the database from which you want to revoke the privileges, and 'username' with the name of the user from whom you want to revoke the privileges. Also, make sure to replace 'localhost' with the appropriate host from which the user accesses the database.


After running this command, the specified user will no longer have SELECT and INSERT privileges on the specified database.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

In MySQL, privileges control what actions a user can perform on the database server. Granting privileges means giving certain permissions to a user account, allowing them to execute specific operations. Conversely, revoking privileges refers to removing those ...
To grant privileges to a user in MySQL, you can use the GRANT statement followed by the specific privileges you want to grant, such as SELECT, INSERT, UPDATE, DELETE, etc. You also need to specify which database or tables the privileges should apply to.For exa...
To create users in a MySQL database, you can use the SQL statement "CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';". This will create a new user with the specified username, hostname, and password. You can also gran...