How to Use MySQL GRANT to Grant Privileges to Account

Summary: In this tutorial, you will learn how to use MySQL GRANT statement to grant privileges to accounts. In order to follow this tutorial, we recommend that you read the ‘Getting started with MySQL access control system" and "How to create a user in MySQL" tutorials first.

MySQL GRANT Statement Explained

In order to give access privileges to an account, you use the MySQL GRANT statement with the syntax as follows:

GRANT privileges (column_list)
ON [object_type] privilege_level
TO account [IDENTIFIED BY 'password']
[REQUIRE encryption]
WITH with_options

If the account exists, MySQL modifies its privileges. If account does not exist, GRANT statement creates a new account with the specified privileges. Besides granting privileges to the account, the GRANT statement also sets other account's characteristics such as limit on access to the database server resources and use secure connection. If you want to grant a privilege to an account, you must have that privilege and GRANT OPTION privilege. We will examine the MySQL GRANT statement in detail as follows:

  • privileges indicates the privileges to assign to the account. For example the CREATE privilege allows user to create database and tables. You can assign multiple privileges at a time. Each privilege is separated by a comma. You can use the privileges in the table 1.1 below.
  • column_list specifies the columns to which a privilege applies. Each column is separated by commas and listed within parentheses. The column_list is optional.
  • privilege_level specifies the level at which the privileges apply. You can use global privileges, database-specific privileges, table-specific privileges, column-specific privileges…
  • account specifies which account is being granted the privileges.
  • password specifies the password to assign to the account. If the account exists, the GRANT statement replaces the old password. Like the CREATE USER statement, you use plaintext password followed by the IDENTIFIED BY. The IDENTIFIED BY clause is optional.
  • After the REQUIRE clause, you specifies whether the user has to connect to the database server over secure connection using SSL.
  • If you want the account to have the privilege to grant its own privileges to other accounts, you need to use WITH with GRANT OPTION. In addition, WITH clause is used to allocate the database server's resource that enable you to set how many connections or statements an account can use per hour.

The MySQL GRANT statement is normally used with the CREATE USER statement. You create a new user first before granting privileges to that user.

Examples with MySQL GRANT

Let's practice with some examples to have a better understanding of MySQL GRANT statement. If you want to create a superuser account that can do anything including being able to grant privileges to other user you can use the following queries:

CREATE USER 'super'@'localhost' IDENTIFIED BY 'SecurePass1';

GRANT ALL ON *.* TO 'super'@'localhost' WITH GRANT OPTION;

The ON *.* clause means all databases and all objects in them. The only limit here is that super can only connect to the database server from localhost, which makes the server more secured. To create a user that has all access in our sample database classicmodels and can connect from any host you use the following queries:

CREATE USER 'super2'@'%' IDENTIFIED BY 'SecurePass2';

GRANT ALL classicmodels.* TO 'super2'@'%' WITH GRANT OPTION;

You can grant multiple privileges at a time; a comma separates each privilege. For example, you can create a user to use SELECT, INSERT and UPDATE statements on our sample database classicmodels, you can use the following queries:

CREATE USER 'rfc'@'%' IDENTIFIED BY 'SecurePass3';

GRANT SELECT, UPDATE, DELETE ON  classicmodels.* TO 'rfc'@'%';

Available privileges to use with MySQL GRANT

Here is the table that lists all the available privileges in MySQL

Privilege Description
ALL [PRIVILEGES] Grant all privileges at specified access level except GRANT OPTION
ALTER Allow to use of ALTER TABLE statement
ALTER ROUTINE Allow user to alter or drop stored routine
CREATE Allow user to create database and table
CREATE ROUTINE Allow user to create stored routine
CREATE TABLESPACE Allow user to create, alter or drop tablespaces and log file groups
CREATE TEMPORARY TABLES Allow user to create temporary table by using CREATE TEMPORARY TABLE
CREATE USER Allow user to use the CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES statements.
CREATE VIEW Allow user to create or modify view
DELETE Allow user to use DELETE
DROP Allow user to drop database, table and view
EVENT Allow user to schedule events in Event Scheduler
EXECUTE Allow user to execute stored routines
FILE Allow user to read any file in the database directory.
GRANT OPTION Allow user to have privileges to grant or revoke privileges from other accounts
INDEX Allow user to create or remove indexes.
INSERT Allow user to use INSERT statement
LOCK TABLES Allow user to use LOCK TABLES on tables for which you have the SELECT privilege
PROCESS Allow user to see all processes with SHOW PROCESSLIST statement.
PROXY Enable user proxying
REFERENCES Not implemented
RELOAD Allow user to use FLUSH operations
REPLICATION CLIENT Allow user to query to see where master or slave servers are
REPLICATION SLAVE Allow user to use replicate slaves to read binary log events from the master.
SELECT Allow user to use SELECT statement
SHOW DATABASES Allow user to show all databases
SHOW VIEW Allow user to use SHOW CREATE VIEW statement
SHUTDOWN Allow user to use mysqladmin shutdown command
SUPER Allow user to use other administrative operations such as CHANGE MASTER TO, KILL, PURGE BINARY LOGS, SET GLOBAL, and mysqladmin command
TRIGGER Allow user to use TRIGGER operations.
UPDATE Allow user to use UPDATE statement
USAGE Equivalent to “no privileges”

Table 1.1

In this tutorial, you've learned how to use MySQL GRANT statement to grant various privileges to users with different options.