How to Grant And Revoke Privileges In MySQL?

7 minutes read

To grant and revoke privileges in MySQL, you can use the GRANT and REVOKE statements. Here is an overview of how to accomplish this:


To grant privileges:

  1. Start by logging in to MySQL as an admin user or a user with the necessary privileges to grant privileges to other users.
  2. Use the GRANT statement followed by the specific privileges you want to grant to a user or a user group. For example, to grant all privileges on a database named 'mydatabase' to a user named 'myuser', you would use: GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
  3. You can also grant privileges on specific tables instead of the entire database. For example, to grant only the SELECT and INSERT privileges on a table named 'mytable' in 'mydatabase' to 'myuser', you can use: GRANT SELECT, INSERT ON mydatabase.mytable TO 'myuser'@'localhost';
  4. After specifying the privileges, you need to specify the user or user group for whom you are granting privileges. In the above examples, 'myuser'@'localhost' represents the user 'myuser' connecting from the 'localhost' server. Replace these values with your desired user and host values.
  5. You can grant privileges to multiple users simultaneously by separating the users with commas. For example: GRANT SELECT, INSERT ON mydatabase.mytable TO 'user1'@'localhost', 'user2'@'%';


To revoke privileges:

  1. Log in to MySQL as an admin user or a user with the necessary privileges to revoke privileges from other users.
  2. Use the REVOKE statement followed by the specific privileges you want to revoke from a user or a user group. For example, to revoke all privileges on a database named 'mydatabase' from a user named 'myuser', you would use: REVOKE ALL PRIVILEGES ON mydatabase.* FROM 'myuser'@'localhost';
  3. Similar to granting privileges, you can specify privileges on specific tables. For example, to revoke only the SELECT privilege on a table named 'mytable' in 'mydatabase' from 'myuser', you can use: REVOKE SELECT ON mydatabase.mytable FROM 'myuser'@'localhost';
  4. Specify the user or user group from whom you want to revoke the privileges. As in the grant statement, 'myuser'@'localhost' represents the user 'myuser' connecting from the 'localhost' server. Replace these values accordingly.
  5. You can revoke privileges from multiple users simultaneously by separating the users with commas. For example: REVOKE SELECT, INSERT ON mydatabase.mytable FROM 'user1'@'localhost', 'user2'@'%'.


Once you have granted or revoked the desired privileges, remember to reload the privileges for the changes to take effect using the FLUSH PRIVILEGES statement:


FLUSH PRIVILEGES;

Best MySQL Managed Hosting Providers in 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core
  • High Performance and Cheap Cloud Dedicated Servers
  • 1 click install Wordpress
  • Low Price and High Quality
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Active Digital Community
  • Simple Control Panel
  • Starting from 5$ per month


How to revoke DROP privileges from a specific database in MySQL?

To revoke the DROP privileges from a specific database in MySQL, you can follow these steps:

  1. Connect to MySQL with a user that has sufficient privileges to modify user permissions. For example, you can use the root user or any user with the GRANT OPTION.
1
mysql -u root -p


  1. Switch to the specific database.
1
USE your_database_name;


  1. Revoke the DROP privileges from a specific user.
1
REVOKE DROP ON your_database_name.* FROM 'your_username'@'localhost';


Replace your_database_name with the name of the database you want to revoke the privileges from, and your_username with the relevant username.

  1. Finally, flush the privileges to apply the changes.
1
FLUSH PRIVILEGES;


The specified user will no longer have the DROP privileges on the specified database.


What is the difference between granting privileges with GRANT and GRANT OPTION in MySQL?

In MySQL, the GRANT statement is used to grant certain privileges to a user account. On the other hand, the GRANT OPTION privilege allows the user account to grant or revoke privileges for other user accounts.


To understand the difference more clearly, let's consider the following example:


Suppose there are two user accounts: User A and User B. User A has the GRANT OPTION privilege.

  1. GRANT: When User A uses the GRANT statement to grant certain privileges to User B, User B is only granted those specific privileges. User B does not acquire the ability to grant or revoke privileges for other user accounts.
  2. GRANT OPTION: Since User A has the GRANT OPTION privilege, they can use the GRANT statement to grant privileges to other user accounts, including User B. User B, however, still does not have the GRANT OPTION privilege and cannot grant or revoke privileges for other user accounts.


In summary, the GRANT statement is used to grant privileges to a user account, while the GRANT OPTION privilege allows a user account to grant or revoke privileges for other user accounts.


What is the syntax for granting table-specific privileges in MySQL?

The syntax for granting table-specific privileges in MySQL is as follows:

1
2
3
GRANT privilege(s)
ON table_name
TO user_name;


Here, you need to replace the following placeholders as per your requirements:

  • privilege(s): Specifies the specific privileges you want to grant. For example, SELECT, INSERT, UPDATE, DELETE, etc. You can also use the wildcard % to grant all privileges.
  • table_name: Specifies the name of the table for which you want to grant privileges.
  • user_name: Specifies the name of the user or users to whom you want to grant privileges.


For example, if you want to grant SELECT and INSERT privileges on a table named employees to a user named john, the syntax would be:

1
2
3
GRANT SELECT, INSERT
ON employees
TO john;


You can also grant privileges to multiple users or roles by specifying their names separated by commas:

1
2
3
GRANT privilege(s)
ON table_name
TO user_name1, user_name2, ...;


Remember to execute the FLUSH PRIVILEGES; statement after granting or revoking privileges to ensure the privilege changes take effect immediately.


How to revoke INSERT privileges in MySQL?

To revoke INSERT privileges in MySQL, you need to follow these steps:

  1. Connect to the MySQL server using a MySQL client tool like mysql command-line client or MySQL Workbench.
  2. Choose the appropriate database by running the following command: USE database_name;
  3. Revoke the INSERT privilege from a specific user or users by executing the following command: REVOKE INSERT ON table_name FROM user_name; Replace table_name with the name of the table you want to revoke the privilege on, and user_name with the name of the user you want to revoke the privilege from. If you want to revoke the INSERT privilege on all tables within the database, you can use the asterisk (*) as a wildcard: REVOKE INSERT ON *.* FROM user_name; If you want to revoke the INSERT privilege from all users, you can replace user_name with '%': REVOKE INSERT ON table_name FROM '%';
  4. Once you've executed the appropriate REVOKE command, the INSERT privilege will be revoked from the specified user(s).


Remember that only users with the necessary privileges (like 'GRANT OPTION') can revoke privileges from other users.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To grant and revoke privileges in MySQL, you can use the GRANT and REVOKE statements. These statements allow you to control the access and permissions granted to different MySQL users.To grant privileges, you use the GRANT statement followed by the specific pr...
To grant MySQL users all permissions, you can execute the following query:GRANT ALL PRIVILEGES ON . TO 'username'@'localhost';Replace 'username' with the actual username for which you want to grant all privileges. The 'localhost&#39...
To create a new database in MySQL, you can follow the steps below:Open your MySQL client, such as MySQL Workbench or MySQL Command Line, and connect to the MySQL server where you have the necessary privileges. Use the CREATE DATABASE statement followed by the ...