How to Grant And Revoke Privileges In MySQL?

13 minutes read

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 privileges you want to grant, the database or table to which the privileges should apply, and the user or users to whom you are granting the privileges. For example:

1
2
GRANT SELECT, INSERT ON database_name.* TO 'username'@'localhost';
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';


The first statement grants SELECT and INSERT privileges on all tables in the specified database to the user 'username' connecting from 'localhost'. The second statement grants all privileges on the same database to the same user.


You can use more specific database and table names, such as 'database_name.table_name', or even use wildcards like '*' to grant privileges on all databases or tables.


To revoke privileges, you use the REVOKE statement followed by the specific privileges, database or table, and user. For example:

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


The first statement revokes the SELECT and INSERT privileges on all tables in the database from the user 'username' connecting from 'localhost'. The second statement revokes all privileges from the same user on the same database.


Again, you can use more specific names or wildcards to revoke privileges on specific databases or tables.


It's important to note that to grant or revoke privileges, you need to have the necessary administrative privileges yourself. This typically means being connected as a user with the 'GRANT OPTION' privilege or having the SUPER or CREATE USER 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 grant privileges to a user on a specific tablespace in MySQL?

To grant privileges to a user on a specific tablespace in MySQL, you can follow these steps:

  1. Log in to MySQL server as a user with administrator privileges using the following command: mysql -u root -p
  2. Create the user if it doesn't exist. Replace 'username' and 'password' with the desired username and password: CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
  3. Grant privileges to the user on the specific tablespace. Replace 'username' and 'tablespace_name' with the actual values: GRANT USAGE ON *.* TO 'username'@'localhost' USING 'tablespace_name';
  4. Grant specific privileges to the user on the desired table in the tablespace. Replace 'username', 'table_name', and 'tablespace_name' with the actual values: GRANT SELECT, INSERT, UPDATE, DELETE ON `tablespace_name`.`table_name` TO 'username'@'localhost';
  5. Flush the privileges to apply the changes: FLUSH PRIVILEGES;


Now you have granted privileges to a user on a specific tablespace in MySQL.


What is the command to revoke SELECT privileges on a specific table in MySQL?

The command to revoke SELECT privileges on a specific table in MySQL is as follows:


REVOKE SELECT ON database_name.table_name FROM username;


Replace "database_name" with the name of the database where the table resides, "table_name" with the name of the specific table, and "username" with the name of the user to be revoked the SELECT privilege.


How to check the privileges assigned to a user in MySQL?

You can use the following SQL command to check the privileges assigned to a user in MySQL:

1
SHOW GRANTS FOR 'username'@'localhost';


Replace 'username' with the actual username you want to check and 'localhost' with the actual hostname or IP address of the MySQL server if applicable.


Alternatively, you can also run the command without specifying a specific host:

1
SHOW GRANTS FOR 'username';


This will display the privileges assigned to the specified user.

Best MySQL Database Books to Read in 2024

1
Murach's MySQL (3rd Edition)

Rating is 5 out of 5

Murach's MySQL (3rd Edition)

2
Learning MySQL: Get a Handle on Your Data

Rating is 4.9 out of 5

Learning MySQL: Get a Handle on Your Data

3
MySQL Crash Course: A Hands-on Introduction to Database Development

Rating is 4.8 out of 5

MySQL Crash Course: A Hands-on Introduction to Database Development

4
PHP & MySQL: Server-side Web Development

Rating is 4.7 out of 5

PHP & MySQL: Server-side Web Development

5
MySQL Cookbook: Solutions for Database Developers and Administrators

Rating is 4.6 out of 5

MySQL Cookbook: Solutions for Database Developers and Administrators

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

Rating is 4.5 out of 5

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

7
Murach's PHP and MySQL (4th Edition)

Rating is 4.4 out of 5

Murach's PHP and MySQL (4th Edition)

8
High Performance MySQL: Proven Strategies for Operating at Scale

Rating is 4.3 out of 5

High Performance MySQL: Proven Strategies for Operating at Scale

9
MySQL(TM): The Complete Reference

Rating is 4.2 out of 5

MySQL(TM): The Complete Reference


How to check the privileges assigned to a specific table in MySQL?

To check the privileges assigned to a specific table in MySQL, you can use the following steps:

  1. Log in to MySQL server with the appropriate user credentials.
  2. Select the database containing the table you want to check the privileges for. Use the following command to select the database: USE your_database_name;
  3. Use the SHOW GRANTS command followed by the table name to display the privileges assigned to the table. For example, if your table is named "your_table_name": SHOW GRANTS FOR 'your_username'@'your_host' ON your_database_name.your_table_name; Replace 'your_username' with the actual username and 'your_host' with the actual host address.
  4. Execute the command, and you will see the output displaying the specific privileges assigned to the table for the specified user.


Note: If you want to check the privileges for all tables in a database, you can omit the table name from the command mentioned in step 3:

1
SHOW GRANTS FOR 'your_username'@'your_host' ON your_database_name.*;



How to grant privileges to a user to create tables in a specific schema in MySQL?

To grant privileges to a user to create tables in a specific schema in MySQL, you can follow these steps:

  1. Connect to your MySQL server using a privileged user account that has the necessary privileges to grant privileges.
  2. Execute the following command to grant the user permission to create tables in the specific schema:
1
GRANT CREATE TABLE ON `database_name`.`schema_name` TO 'username'@'localhost' IDENTIFIED BY 'password';


Replace database_name, schema_name, username, and password with their respective values. Make sure to use backticks around the database and schema names if they contain special characters or spaces.

  1. Flush privileges to ensure the changes take effect:
1
FLUSH PRIVILEGES;


The specified user should now have the necessary privileges to create tables in the specified schema.


What is the command to grant privileges for a specific database in MySQL?

The command to grant privileges for a specific database in MySQL is:


GRANT privileges ON database_name.* TO 'username'@'host';


Replace "privileges" with the specific privileges to grant, such as SELECT, INSERT, UPDATE, DELETE, etc. Replace "database_name" with the name of the specific database. Replace "username" with the username you want to grant the privileges to. Replace "host" with the hostname or IP address from which the user will connect, or use '%' as a wildcard for any host.


What is the command to grant SELECT privileges on a specific table in MySQL?

The command to grant SELECT privileges on a specific table in MySQL is:

1
GRANT SELECT ON database_name.table_name TO 'username'@'localhost';


Replace database_name with the name of the database, table_name with the name of the table, and 'username'@'localhost' with the appropriate username and host.


What is the command to revoke ALTER privileges on a specific table in MySQL?

The command to revoke ALTER privileges on a specific table in MySQL is:

1
REVOKE ALTER ON database_name.table_name FROM 'username'@'host';


Replace database_name with the name of the database that contains the table, table_name with the name of the table, 'username'@'host' with the specific user and host from which the privileges are being revoked.


For example, if you want to revoke ALTER privileges for user 'john' accessing from host 'localhost' on a table named 'employees' in a database named 'company', you would run the following command:

1
REVOKE ALTER ON company.employees FROM 'john'@'localhost';



How to grant only SELECT privileges on all tables in a database in MySQL?

To grant only SELECT privileges on all tables in a database in MySQL, you can follow these steps:

  1. Open the MySQL client or any MySQL administration tool of your choice (e.g., phpMyAdmin).
  2. Connect to the MySQL server with appropriate credentials.
  3. Select the database for which you want to grant SELECT privileges on all tables. For example, to use the "mydatabase" database, run the following command: USE mydatabase;
  4. Once you have selected the database, run the following command to grant SELECT privileges on all tables to a specific user: GRANT SELECT ON *.* TO 'username'@'localhost'; Replace 'username' with the actual username for which you want to grant SELECT privileges. You can also change 'localhost' to a specific host address if needed.
  5. Finally, run the following command to apply the changes: FLUSH PRIVILEGES; This will ensure that the privileges are updated and applied immediately.


After following these steps, the specified user will only have SELECT privileges on all tables in the selected database.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

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 find MySQL usernames and passwords, you can check the following locations:MySQL Configuration File: The usernames and passwords for MySQL are usually specified in the "my.cnf" or "my.ini" configuration file. This file can be found in differe...
The MySQL config file contains important configuration settings for the MySQL database server. The location of the MySQL config file depends on the operating system you are using.On Unix-based systems (such as Linux and macOS), the MySQL config file is typical...