授予所选表的权限

时间:2015-09-08 12:41:06

标签: mysql privileges grant

我有一个包含大约100个表的MySQL 5.x数据库。我想在某些表上仅授予 SELECT权限:

show tables from my_db like '%some_pattern%'; # select only tables

...以及其他表格上的SELECTINSERTUPDATEDELETE

我该怎么做?

2 个答案:

答案 0 :(得分:1)

SELECT CONCAT('GRANT SELECT ON schemaName.', TABLE_NAME, ' to ''username'';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'schemaName'
  AND TABLE_NAME LIKE '%some_pattern%'

SELECT CONCAT('GRANT SELECT, INSERT, UPDATE, DELETE ON schemaName.', TABLE_NAME, ' to ''username'';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'schemaName'
  AND TABLE_NAME NOT LIKE '%some_pattern%'

然后运行上面的语句,复制生成的sql并运行结果以授予权限

答案 1 :(得分:0)

使用单个查询授予许多表的权限是不可能的。您需要对每个表执行查询。您可以尝试使用Adminer或phpMyAdmin等管理工具。