MySQL对以特定名字开头的数据库或表进行授权

对以"db_1"开头的数据库进行授权

grant all privileges on `db_1%`.* to dp_admin identified by 'password';
grant select on `db_1%`.* to dp_admin identified by 'password';

 

假设我们要授权用户'user'@'localhost'对所有以'mydb_'开头的数据库表都有SELECT权限

-- 假设我们要授权用户'user'@'localhost'对所有以'mydb_'开头的数据库表都有SELECT权限
-- 首先,我们需要获取所有以'mydb_'开头的表名
SELECT CONCAT('GRANT SELECT ON `', table_schema, '`.`', table_name, '` TO ''user''@''localhost'';') AS grant_statement
FROM information_schema.tables
WHERE table_schema LIKE 'mydb_%'
  AND table_type = 'BASE TABLE';
 
-- 然后,我们可以将生成的GRANT语句执行,为用户授权
-- 注意:执行上述查询得到的语句需要具有足够权限来执行这些语句,通常需要有管理员权限

 

posted @ 2019-04-17 10:41  paul_hch  阅读(1461)  评论(0)    收藏  举报