Use the SHOW GRANTS statement to show privileges of a user in MySQL
This article describes how to use the SHOW GRANTS statement to list the privileges granted to a user or role.
Sometimes, you need to see the privileges a user has been granted to review.
MySQL allows you to use the SHOW GRANTS statement to show the privileges granted to a user account or a role.
If a connected account is denied access to a database, use SHOW GRANTS to help diagnose MySQL Error 1044.
MySQL SHOW GRANTS Syntax
The following is the basic syntax of the SHOW GRANTS statement :
SHOW GRANTS
[FOR {user | role}
[USING role [, role] ...]]
Here:
- You should specify the user account or the role after the
FORkeyword to show the privileges previously granted to the user account or the role. If theFORclause is omitted,SHOW GRANTSwill return the privileges of the current user. - You should use the
USINGclause to check privileges related to the user’s role. The role you specify in theUSINGclause must have been granted to the user beforehand.
SHOW GRANTS requires the SELECT privilege on the mysql system schema when you inspect another account or role. You can display your own grants without that privilege. See the official MySQL SHOW GRANTS documentation.
MySQL SHOW GRANTS Examples
There are some examples for the usage of MySQL SHOW GRANTS statements here.
Show the privileges granted to a specified user
This example demonstrates the complete steps to create a user, grant privileges, and view privileges.
-
Create a new database using
CREATE DATABASE, namedsqlizdb:CREATE DATABASE sqlizdb; -
USE sqlizdb; -
Create a new table named
sqlizdbin the database :test_tableCREATE TABLE test_table( id int AUTO_INCREMENT PRIMARY KEY, txt varchar(100) NOT NULL ); -
Create a new local user named
'sqliz'@'localhost':CREATE USER 'sqliz'@'localhost' IDENTIFIED BY RANDOM PASSWORD;MySQL returns a generated password. Save it securely; this syntax requires MySQL 8.0.18 or later.
-
Show the privileges for
'sqliz'@'localhost':SHOW GRANTS FOR 'sqliz'@'localhost';+-----------------------------------+ | Grants for sqliz@localhost | +-----------------------------------+ | GRANT USAGE ON *.* TO `sqliz`@`localhost` | +-----------------------------------+GRANT USAGEmeans no authority. By default, when a new user is created, he has no privileges. -
Grant the row privileges used in this example to
'sqliz'@'localhost':GRANT SELECT, INSERT ON sqlizdb.* TO 'sqliz'@'localhost'; -
Finally, show the privileges granted to the user
'sqliz'@'localhost':SHOW GRANTS FOR 'sqliz'@'localhost';+------------------------------------------------------------------------+ | Grants for sqliz@localhost | +------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `sqliz`@`localhost` | | GRANT SELECT, INSERT ON `sqlizdb`.* TO `sqliz`@`localhost` | +------------------------------------------------------------------------+
Show the privileges granted to the current user
The following statement uses the SHOW GRANTS statement to show the privileges granted to the current user:
SHOW GRANTS;
It is equivalent to the following statement:
SHOW GRANTS FOR CURRENT_USER;
or
SHOW GRANTS FOR CURRENT_USER();
Both CURRENT_USER and CURRENT_USER() return the current user.
Show privileges granted to roles
This example demonstrates the complete steps to create a role, grant privileges to the role, and view privileges of the role.
-
Create a new local role named
'write_role'@'localhost':CREATE ROLE 'write_role'@'localhost'; -
Show privileges granted to the role
'write_role'@'localhost':SHOW GRANTS FOR 'write_role'@'localhost';+----------------------------------------+ | Grants for write_role@localhost | +----------------------------------------+ | GRANT USAGE ON *.* TO `write_role`@`localhost` | +----------------------------------------+ -
Grant
SELECT,INSERT,UPDATE, andDELETEprivileges on the databasesqlizdbto the role'write_role'@'localhost':GRANT SELECT, INSERT, UPDATE, DELETE ON sqlizdb.* TO 'write_role'@'localhost'; -
Show privileges granted to the role
'write_role'@'localhost':SHOW GRANTS FOR 'write_role'@'localhost';+-------------------------------------------------------------------------+ | Grants for write_role@localhost | +-------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `write_role`@`localhost` | | GRANT SELECT, INSERT, UPDATE, DELETE ON `sqlizdb`.* TO `write_role`@`localhost` | +-------------------------------------------------------------------------+
D) Show the privileges associated with the role of the user
This example demonstrates the detailed steps for creating a new user, assigning role to a user, and displaying privileges.
-
Create a new local user named
'sqliz2'@'localhost':CREATE USER 'sqliz2'@'localhost' IDENTIFIED BY RANDOM PASSWORD;Save the generated password shown by MySQL securely.
-
Grant
EXECUTEprivileges to the user'sqliz2'@'localhost':GRANT EXECUTE ON sqlizdb.* TO 'sqliz2'@'localhost'; -
Grant the role
'write_role'@'localhost'to the user'sqliz2'@'localhost':GRANT 'write_role'@'localhost' TO 'sqliz2'@'localhost'; -
Show the privileges granted to the user
'sqliz2'@'localhost':SHOW GRANTS FOR 'sqliz2'@'localhost';+----------------------------------------------+ | Grants for sqliz2@localhost | +----------------------------------------------+ | GRANT USAGE ON *.* TO `sqliz2`@`localhost` | | GRANT EXECUTE ON `sqlizdb`.* TO `sqliz2`@`localhost` | | GRANT `write_role`@`localhost` TO `sqliz2`@`localhost` | +----------------------------------------------+ -
Use the
USINGclause inSHOW GRANTSto show the privileges from the'write_role'@'localhost'role:SHOW GRANTS FOR 'sqliz2'@'localhost' USING 'write_role'@'localhost';+------------------------------------------------------------------------------+ | Grants for sqliz2@localhost | +------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO `sqliz2`@`localhost` | | GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON `sqlizdb`.* TO `sqliz2`@`localhost` | | GRANT `write_role`@`localhost` TO `sqliz2`@`localhost` | +------------------------------------------------------------------------------+
Conclusion
In MySQL, you can use the SHOW GRANTS statement to show the privileges granted to a user or a role.