要授予用户仅查看MySQL视图的权限,请使用以下语法
GRANT SELECT ON yourDatabaseName.yourViewName TO ' yourUserName@'yourLocalHost';
首先,您需要显示表中的所有视图名称。语法如下-
SHOW FULL TABLES IN yourDatabaseName WHERE TABLE_TYPE LIKE 'VIEW';
现在实现上述语法以显示数据库中的所有视图。在这里,我正在使用数据库名称“ test”。查询如下-
mysql> SHOW FULL TABLES IN test WHERE TABLE_TYPE LIKE 'VIEW';
以下是输出-
+-------------------------------+------------+ | Tables_in_test | Table_type | +-------------------------------+------------+ | empidandempname_view | VIEW | | empinfoview | VIEW | | lookuptable | VIEW | | view_student | VIEW | | viewemployeeidandemployeename | VIEW | +-------------------------------+------------+ 5 rows in set (0.00 sec)
显示MySQL.user表中的所有用户名和主机。查询如下-
mysql> select user,host from MySQL.user;
以下是输出-
+------------------+-----------+ | 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)
授予用户和主机权限。现在,使用数据库“ test”,视图为“ viewemployeeidandemployeename”。要将其授予'Adam Smith'@'localhost',以下是查询-
mysql> GRANT SELECT ON test. viewemployeeidandemployeename TO ' Adam Smith'@'localhost';