Grant Privileges to Users with the GRANT Statement in MySQL
Learn MySQL GRANT syntax and how to assign global, database, and table privileges; grant only what each account needs.
As a database administrator or maintainer, you need more precise privileges control for database security. You can give different privileges to different users.
After you create a new user, the new user can log in to the MySQL database server, but he may not have any privileges. After he was granted privileges on databases and tables, he can he perform operations such as selecting databases and queries.
If a statement fails with ERROR 1044, the connected account may lack the specific privilege that operation requires. Check the active account and grant only the needed scope; see MySQL Error 1044 troubleshooting.
If the error says a command is denied for a named table, see MySQL Error 1142 troubleshooting.
In MySQL, GRANT statements are used to grant privileges to users.
MySQL GRANT syntax
The following is the syntax of MySQL GRANT:
GRANT privilege_type [,privilege_type],..
ON privilege_object
TO user_account;
In this syntax:
privilege_type-
Privilege type. Privileges to be granted to the user. Such as:
ALL,SELECT,UPDATE,DELETE,ALTER,DROPandINSERTetc. For more details, please refer to: https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_all privilege_object-
Privilege object. It can be global objects, or objects in a certain database. Such as:
*,*.*,db_name.*,db_name.table_name,table_nameetc. user_account-
User account. Specify the user name and host explicitly as
'username'@'host'; omitting the host is equivalent to the broad wildcard%, which is deprecated in current MySQL.
Here are a few common cases:
-
Grant global privileges
ALLon*.*grants privileges server-wide. Reserve it for trusted administrator accounts; application users usually need narrower database- or table-level grants.GRANT ALL ON *.* TO 'db_admin'@'localhost';This grants server-wide privileges to a trusted administrator account. Do not use this scope for an application account.
ALLdoes not includeGRANT OPTIONorPROXY. AddWITH GRANT OPTIONonly if this administrator must delegate privileges; doing so lets the account grant onward the privileges it holds. See the MySQLGRANTreference. -
Grant specific privileges on all objects in the database
GRANT SELECT, INSERT ON sqliz.* TO 'sqliz'@'localhost';This grants only
SELECTandINSERTon objects insqlizto thesqlizaccount. Add other privileges only when the application needs them. -
Grant query and insert privileges on a table
GRANT SELECT, INSERT ON sqliz.test_table TO 'sqliz'@'localhost';Here, the
SELECTandINSERTprivileges ontest_tablein thesqlizdatabase are granted to the usersqliz@localhost -
Grant a privilege on a single column
GRANT SELECT (`email`) ON `sqliz`.`users` TO 'report_reader'@'localhost';This grants access only to the
emailcolumn. MySQL supports column-levelSELECT,INSERT,REFERENCES, andUPDATE; see theGRANTcolumn privilege syntax. If a statement is denied for a particular column, see MySQL Error 1143 troubleshooting.
MySQL GRANT Examples
Follow the steps below to execute some MySQL GRANT Examples:
-
Connect to the MySQL server using the mysql client tool and log in as
rootuser :mysql -u root -pEnter the password for the
rootaccount and pressEnter:Enter password: ******** -
Display users of the current MySQL server :
SELECT user FROM mysql.user;+------------------+ | user | +------------------+ | root | | test_role1 | | test_role2 | | testuser | | mysql.infoschema | | mysql.session | | mysql.sys | | root | +------------------+ -
Create a new local user named
sqliz:CREATE USER 'sqliz'@'localhost' IDENTIFIED BY RANDOM PASSWORD;MySQL returns the generated password in its result; save it securely. This syntax requires MySQL 8.0.18 or later.
-
Show all users again:
SELECT user FROM mysql.user;+------------------+ | user | +------------------+ | root | | test_role1 | | test_role2 | | testuser | | mysql.infoschema | | mysql.session | | mysql.sys | | root | | sqliz | +------------------+The user
sqlizwas successfully created. -
Open a new session and log in to MySQL as
sqlizuser :mysql --user=sqliz --passwordEnter the generated password returned by
CREATE USER; it is not necessarily the same as the account name:Enter password: ******** -
Show the list of accessible databases of the user
sqliz:SHOW DATABASES;The following is a list of databases that
sqlizcan be accessed:+--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ -
Go to user
root’s session and create a new database calledsqlizdb:CREATE DATABASE sqlizdb; -
Grant only the privileges used by the remaining example. The user must create a table, insert a row, and read it:
GRANT CREATE, SELECT, INSERT ON sqlizdb.* TO 'sqliz'@'localhost'; -
Switch to the session of
sqlizand display databases:SHOW DATABASES;Now,
sqlizyou can seesqlizdb:+--------------------+ | Database | +--------------------+ | information_schema | | sqlizdb | +--------------------+ -
Select the
sqlizdbdatabase as default database :USE sqlizdb;Henceforth, the default database is:
sqlizdb. All subsequent operations are performed in this database by default. -
Create a new table named
test_table:CREATE TABLE test_table( id int AUTO_INCREMENT PRIMARY KEY, txt varchar(100) NOT NULL ); -
Display all tables from the
sqlizdbdatabase :SHOW TABLES;Users
sqlizcan see thetest_tabletable:+-------------------+ | Tables_in_sqlizdb | +-------------------+ | test_table | +-------------------+ -
Insert a new row into the
test_tabletable :INSERT INTO test_table(txt) VALUES('Hello World.'); -
Query rows from the
test_tabletable :)SELECT * FROM test_table;Here is the output:
+----+--------------+ | id | txt | +----+--------------+ | 1 | Hello World. | +----+--------------+The
sqlizaccount can create tables insqlizdb, insert rows, and read them. Other operations remain denied unless separately granted.
Conclusion
In this article, you learned how to grant different privileges to users using MySQL GRANT statements.