Grant a user permission to only view a MySQL view?

MySQLMySQLi Database

To grant a user permission to only view a MySQL view, use the below syntax

GRANT SELECT ON yourDatabaseName.yourViewName TO ' yourUserName@'yourLocalHost';

First you need to display all the view names from a table. The syntax is as follows −

SHOW FULL TABLES IN yourDatabaseName WHERE TABLE_TYPE LIKE 'VIEW';

Now implement the above syntax to display all views from a database. Here I am using the database name ‘test’. The query is as follows −

mysql> SHOW FULL TABLES IN test WHERE TABLE_TYPE LIKE 'VIEW';

The following is the output −

+-------------------------------+------------+
| Tables_in_test                | Table_type |
+-------------------------------+------------+
| empidandempname_view          | VIEW       |
| empinfoview                   | VIEW       |
| lookuptable                   | VIEW       |
| view_student                  | VIEW       |
| viewemployeeidandemployeename | VIEW       |
+-------------------------------+------------+
5 rows in set (0.00 sec)

Display all user name and host from MySQL.user table. The query is as follows −

mysql> select user,host from MySQL.user;

The following is the output −

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Manish           | %         |
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| Adam Smith       | localhost |
| User1            | localhost |
| am               | localhost |
+------------------+-----------+
9 rows in set (0.00 sec)

Grant the user as well as host. Now, use the database ‘test’ and view is ‘viewemployeeidandemployeename’. To grant it to ‘Adam Smith'@'localhost’, the following is the query −

mysql> GRANT SELECT ON test. viewemployeeidandemployeename TO ' Adam Smith'@'localhost';
Query OK, 0 rows affected (0.18 sec)
raja
Published on 31-Jan-2019 12:31:00
Advertisements