How to Grant And Revoke Privileges In MySQL?

11 minutes read

To grant and revoke privileges in MySQL, you can use the GRANT and REVOKE statements. Here's how you can do it:


To grant privileges to a user, you would use the GRANT statement, followed by the specific privileges you wish to grant, and then specify the username and the database or tables that the privileges should apply to. For example, to grant all privileges on a specific database to a user named "john", you can use the following command:


GRANT ALL PRIVILEGES ON database_name.* TO 'john'@'localhost';


You can also grant specific privileges, such as SELECT, INSERT, UPDATE, DELETE, etc., by replacing "ALL PRIVILEGES" with the specific privileges you want to grant. To grant privileges on all databases, you can use "*" instead of the specific database name.


To revoke privileges from a user, you would use the REVOKE statement, followed by the specific privileges you want to revoke, and then specify the username and the database or tables that the privileges should be revoked from. For example, to revoke the SELECT privilege on a specific table from a user named "john", you can use the following command:


REVOKE SELECT ON table_name FROM 'john'@'localhost';


You can also revoke all privileges with the keyword "ALL" instead of specific privileges.


Note that you need to have the necessary administrative privileges (such as the GRANT OPTION) to grant or revoke privileges. Additionally, it is important to grant only the necessary privileges to users to ensure proper security measures are in place.

Best MySQL Books to Read in 2024

1
Murach's MySQL (3rd Edition)

Rating is 5 out of 5

Murach's MySQL (3rd Edition)

2
High Performance MySQL: Proven Strategies for Operating at Scale

Rating is 4.9 out of 5

High Performance MySQL: Proven Strategies for Operating at Scale

3
Learning MySQL: Get a Handle on Your Data

Rating is 4.8 out of 5

Learning MySQL: Get a Handle on Your Data

4
MySQL Crash Course

Rating is 4.7 out of 5

MySQL Crash Course

5
High Performance MySQL: Optimization, Backups, and Replication

Rating is 4.6 out of 5

High Performance MySQL: Optimization, Backups, and Replication

6
Querying MySQL: Make your MySQL database analytics accessible with SQL operations, data extraction, and custom queries (English Edition)

Rating is 4.5 out of 5

Querying MySQL: Make your MySQL database analytics accessible with SQL operations, data extraction, and custom queries (English Edition)

7
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Rating is 4.4 out of 5

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

8
MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration

Rating is 4.3 out of 5

MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration

9
The MySQL Workshop: A practical guide to working with data and managing databases with MySQL

Rating is 4.2 out of 5

The MySQL Workshop: A practical guide to working with data and managing databases with MySQL


What is the purpose of granting privileges in MySQL?

The purpose of granting privileges in MySQL is to provide specific permissions and access rights to users or user groups for performing various operations or actions on databases, tables, or columns within the MySQL server.


By granting privileges, database administrators can control and manage the level of access that users have within the MySQL server. These privileges can include permissions to create, modify, and delete databases, tables, or columns, as well as executing specific queries, managing users, and more.


Granting privileges allows for fine-grained access control and enforcing security measures to ensure that users are only able to perform the necessary actions while restricting any unauthorized access or modifications to the data and database structure.


How to grant only read-only access to a user in MySQL?

To grant read-only access to a user in MySQL, you can follow these steps:

  1. Connect to MySQL as a user with administrative privileges. This user is typically named "root". mysql -u root -p
  2. Create a new database user without any privileges using the following command. Replace username with the desired username and password with the desired password for the user. CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
  3. Grant only the SELECT privilege on specific database tables to the user. In this example, we are granting read-only access to all tables in the database named "exampledb". To grant access selectively, replace *.* with the desired table names. GRANT SELECT ON exampledb.* TO 'username'@'localhost';
  4. Flush the privileges to ensure the changes take effect. FLUSH PRIVILEGES;
  5. Exit the MySQL prompt. exit


The user "username" will now have read-only access to the specified database tables and won't be able to modify or delete data.


How to grant SELECT privilege on a specific table in MySQL?

To grant the SELECT privilege on a specific table in MySQL, you can follow these steps:

  1. Log in to your MySQL server as a user with administrative privileges (e.g., root).
  2. Open the MySQL command-line client or any other MySQL client interface.
  3. Switch to the database where the table exists, using the command: USE database_name; (replace "database_name" with the actual name of the database).
  4. Run the following command to grant the SELECT privilege on the table to a specific user:
1
GRANT SELECT ON table_name TO 'username'@'localhost';


Replace "table_name" with the actual name of the table and 'username'@'localhost' with the username and hostname of the user you want to grant the privilege to. If you want to grant the privilege to all users, you can use '%@localhost' instead of 'username'@'localhost'.

  1. If you want the user to be able to SELECT from the table remotely, you can replace 'localhost' with the appropriate remote hostname or the wildcard '%' to allow connections from any host.
  2. After granting the privilege, you need to flush the privileges for the changes to take effect. Run the following command:
1
FLUSH PRIVILEGES;


That's it! The SELECT privilege should now be granted to the specified user for the specific table in MySQL.


What is the command to assign a specific password to a user in MySQL?

To assign a specific password to a user in MySQL, you can use the SET PASSWORD command. Here is the syntax:

1
SET PASSWORD FOR 'username'@'hostname' = PASSWORD('new_password');


Replace 'username' with the actual username you want to set the password for, 'hostname' with the hostname or IP address from which the user will be connecting (or use '% for any host), and 'new_password' with the desired password.


For example, to set the password "mynewpassword" for a user named "myuser" connecting from localhost, the command would be:

1
SET PASSWORD FOR 'myuser'@'localhost' = PASSWORD('mynewpassword');



How to grant SELECT privilege on multiple tables in MySQL?

To grant SELECT privilege on multiple tables in MySQL, you can use the following steps:

  1. Log in to MySQL as a user with sufficient privileges (e.g., root).
  2. Identify the tables for which you want to grant SELECT privilege.
  3. Run the GRANT statement to grant SELECT privilege on each table individually, specifying the desired user and privileges for each table: GRANT SELECT ON database_name.table_name TO 'username'@'host'; Replace database_name with the name of the database that contains the table, table_name with the name of the table, username with the target user you want to grant the privilege to, and host with the target host or IP address the user will access from.
  4. Repeat the GRANT statement for each table you want to grant SELECT privilege to.
  5. If you want to grant SELECT privilege on all tables of a specific database, use a wildcard (*) for the table name: GRANT SELECT ON database_name.* TO 'username'@'host'; This grants SELECT privilege on all tables within the specified database.
  6. Ensure that the user and host specified in the GRANT statements match the credentials of the user who needs the SELECT privilege.


Note: Take caution when granting privileges to ensure that only authorized users have access to the desired tables.


What is the command to grant all privileges except DELETE on a table in MySQL?

The command to grant all privileges except DELETE on a table in MySQL is:

1
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, DROP ON database.table TO 'username'@'localhost';


Replace 'username' with the actual username you want to grant the privileges to, and 'database.table' with the specific database and table you want to grant the privileges on.

Facebook Twitter LinkedIn Telegram

Related Posts:

To grant privileges in Oracle, you need to use the GRANT statement. This statement allows you to give certain privileges to specific users or roles. The basic syntax of the GRANT statement is as follows:GRANT privilege_name [, privilege_name] ON object_name TO...
The flush command in MySQL is used to clear or reload various aspects of the server such as privileges, logs, caches, and table statistics. It is a powerful command that helps in managing and optimizing the database server effectively.When using the flush comm...
To reset MySQL to factory settings, you need to follow these steps:Stop MySQL service: Stop the MySQL service running on your system. The method to stop the service may vary depending on your operating system. Locate the MySQL configuration file: Find the MySQ...