How to display grant defined for a MySQL user?

MySQLMySQLi Database

Use SHOW GRANTS for this. Following is the syntax −

SHOW GRANTS FOR 'yourUserName'@'yourHostName';

Let us display the user name and host name from MySQL.user table.

mysql> select user,host from MySQL.user;

This will produce the following output −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Bob              | %         |
| Charlie          | %         |
| Robert           | %         |
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| @UserName@       | localhost |
| Adam Smith       | localhost |
| Chris            | localhost |
| David            | localhost |
| James            | localhost |
| John             | localhost |
| John Doe         | localhost |
| User1            | localhost |
| am               | localhost |
| hbstudent        | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
+------------------+-----------+
20 rows in set (0.00 sec)

Let use SHOW GRANTS to display grant for a specific user −

mysql> SHOW GRANTS FOR 'Robert'@'%

This will produce the following output −

+----------------------------------------------------------------------+
| Grants for Robert@%                                                  |
+----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `Robert`@`%`                                   |
| GRANT ALL PRIVILEGES ON `sample`.* TO `Robert`@`%` WITH GRANT OPTION |
| GRANT EXECUTE ON `web`.* TO `Robert`@`%`                             |
+----------------------------------------------------------------------+
3 rows in set (0.05 sec)

Above, the SHOW GRANTS example returns the grant information for the user Robert on the host, which MySQL assumes '%'.

raja
Published on 10-Jul-2019 12:21:00
Advertisements