CREATE DATABASE db_01;
CREATE DATABASE db_02;
CREATE TABLE db_01.table_01 (id Int32) Engine = Memory();
CREATE TABLE db_02.table_02 (id Int32) Engine = Memory();
INSERT INTO db_01.table_01 VALUES (1), (2);
INSERT INTO db_02.table_02 VALUES (10), (20);
创建用户并分配所需的权限:
CREATE USER user_01 HOST ANY PROFILE 'default';
/* Set readonly access to DB 'db_01'. */
GRANT SELECT ON db_01.* TO user_01;
/* Set read-write access to DB 'db_02'. */
GRANT SELECT ON db_02.* TO user_01;
GRANT INSERT ON db_02.* TO user_01;
测试(需要以用户\u 01身份登录):
SELECT currentUser(); /* Make sure that user is right. */
/*
┌─currentUser()─┐
│ user_01 │
└───────────────┘
* /
SHOW GRANTS; /* Double check the assigned privileges. */
/*
┌─GRANTS─────────────────────────────────────┐
│ GRANT SELECT ON db_01.* TO user_01 │
│ GRANT INSERT, SELECT ON db_02.* TO user_01 │
└────────────────────────────────────────────┘
* /
SELECT * FROM db_01.table_01; /* OK */
INSERT INTO db_01.table_01 VALUES (3); /* DB::Exception: user_01: Not enough privileges. */
SELECT * FROM db_02.table_02; /* OK */
INSERT INTO db_02.table_02 VALUES (30); /* OK */
1条答案
按热度按时间jk9hmnmh1#
考虑使用sql驱动的访问控制管理:
准备测试环境:
创建用户并分配所需的权限:
测试(需要以用户\u 01身份登录):
上面的代码只是为了演示,您需要考虑引入角色/配额/,继承另一个用户配置文件(更严格,而不是“默认”)等。