GRANT with database name wildcard in MySQL?

2019-01-17 00:22发布

I want to create a user 'projectA' that has the same permissions to every database named 'projectA_%'

I know its possible but MySQL doesn't like my syntax:

grant all on 'projectA\_%'.* to 'projectA'@'%';

Reference: http://dev.mysql.com/doc/refman/5.1/en/grant.html

3条回答
一纸荒年 Trace。
2楼-- · 2019-01-17 00:41

If I use back-tics instead of single quotes in the syntax, it appears to work just fine:

grant all on `projectA\_%`.* to `projectA`@`%`;
查看更多
欢心
3楼-- · 2019-01-17 00:45

As per MySQL's GRANT documentation:

The “_” and “%” wildcards are permitted when specifying database names in GRANT statements that grant privileges at the global or database levels. This means, for example, that if you want to use a “_” character as part of a database name, you should specify it as “\_” in the GRANT statement, to prevent the user from being able to access additional databases matching the wildcard pattern; for example, GRANT ... ON `foo\_bar`.* TO ....

查看更多
贼婆χ
4楼-- · 2019-01-17 00:51
GRANT ALL PRIVILEGES ON `projectA\_%`.* TO 'projectA'@'%' IDENTIFIED BY 'your_passwd';

back-tics are needed for the database name

Edited:Underscore is now escaped.

查看更多
登录 后发表回答