一、clickhouse权限管理
users.xml默认配置文件:

<?xml version="1.0"?> <clickhouse> <profiles> <default> <load_balancing>random</load_balancing> </default> </profiles> <users> <default> <password></password> <networks> <ip>::/0</ip> </networks> <profile>default</profile> <quota>default</quota> <!-- <access_management>1</access_management> --> </default> </users> <quotas> <default> <!-- Limits for time interval. You could specify many intervals with different limits. --> <interval> <!-- Length of interval. --> <duration>3600</duration> <!-- No limits. Just calculate resource usage for time interval. --> <queries>0</queries> <errors>0</errors> <result_rows>0</result_rows> <read_rows>0</read_rows> <execution_time>0</execution_time> </interval> </default> </quotas> </clickhouse>
1.1 users.xml配置文件中设置权限
default权限默认过大需要做限制,可以限制IP或者设置密码
在配置文件中配置:/etc/clickhouse-server/users.xml
<?xml version="1.0"?> <clickhouse> <profiles> <default> <load_balancing>random</load_balancing> </default> <!-- 配置只读权限 --> <r_read> <!-- 自己取名,下面引用> -- <readonly>1</readonly> <allow_ddl>0</allow_ddl> </r_read> <!-- 配置写权限 --> <w_write> <load_balancing>random</load_balancing> </w_write> <!-- 权限全局配置 --> <readonly> <readonly>1</readonly> </readonly> </profiles> <users> <default> <!-- 明文密码或者加密密码,2选1就行 --> <password>123456</password> <password_sha256_hex>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</password_sha256_hex> <!-- 网络访问限制:内网IP访问或者所有IP都可以访问 --> <networks> <ip>::/0</ip> </networks> <!-- 引用上面的权限管理和下面的资源管理 --> <profile>default</profile> <quota>default</quota> <!-- 是否开启sql驱动权限管理:0|1 ,sql命令赋权 --> <!-- <access_management>1</access_management> --> </default> <!-- 读 --> <r_read> <password_sha256_hex>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</password_sha256_hex> <networks> <ip>::/0</ip> </networks> <profile>r_read</profile> <quota>default</quota> <!-- 允许看到的数据库,甚至可以只让用户查看表或者表的多少行 --> <allow_databases> <database>test</database> </allow_databases> </r_read> <!-- 写 --> <w_write> <password>123456</password> <networks> <ip>::/0</ip> </networks> <profile>w_write</profile> <quota>default</quota> </w_write> </users> <!-- 该用户使用的资源限制 --> <quotas> <default> <!-- Limits for time interval. You could specify many intervals with different limits. --> <interval> <!-- Length of interval. --> <duration>3600</duration> <!-- No limits. Just calculate resource usage for time interval. --> <queries>0</queries> <errors>0</errors> <result_rows>0</result_rows> <read_rows>0</read_rows> <execution_time>0</execution_time> </interval> </default> </quotas> </clickhouse>
1.2 sql方式赋权
第一步:在users.xml中将default的赋值权限管理开通 <access_management>1</access_management> 第二步:sql命令创建用户、角色、授权角色给用户 -- 创建用户 chun、明文明码:123456 create user if not exists chun on cluster my_cluster IDENTIFIED WITH plaintext_password BY '123456'; -- 创建角色 chun_role create role if not exists chun_role on cluster my_cluster ; -- 授权 test_db 库查询权限给 角色 chun_role GRANT SELECT ON test_db.* TO chun_role on cluster my_cluster ; -- 分配角色给用户 chun GRANT chun_role TO chun on cluster my_cluster ;
-- 登录 clickhouse-client --host 127.0.0.1 --user chun --password 123456 -- 收回角色 chun_role 的查询权限 REVOKE SELECT ON test_db.* FROM chun_role on cluster my_cluster ;
二、 配置说明
查询权限是整个权限体系的第二层防护,它决定了一个用户能够执行的查询语句,查询权限可以分为以下四类: 读权限:包括SELECT、EXISTS、SHOW和DESCRIBE查询。 写权限:包括INSERT和OPTIMIZE查询。 设置权限:包括SET查询。 DDL权限:包括CREATE、DROP、ALTER、RENAME、ATTACH、DETACH和TRUNCATE查询
2.1 readonly
读权限、写权限和设置权限均由此标签控制,它有三种取值:
当取值为0时,不进行任何限制(默认值) 当取值为1时,只拥有读权限(只能执行SELECT、EXISTS、SHOW和DESCRIBE) 当取值为2时,拥有读权限和设置权限(在读权限基础上,增加了SET查询)
2.2 allow_ddl
DDL权限由此标签控制,它有两种取值: 当取值为0时,不允许DDL查询。 当取值为1时,允许DDL查询(默认值)。 需要注意的是readonly和allow_ddl需要定义在用户使用的profiles角色中
三、命令赋权
# 添加账号新库权限 db.updateUser( "test", { # 旧权限 roles:[{ "role" : "userAdminAnyDatabase", "db" : "admin" }, # 新增权限 {role:"readWrite",db:"testdb2"},{ "role" : "dbAdmin", "db" : "testdb2" }] } ) # 第二种方式 db.grantRolesToUser("test",[{role:"readWrite",db:"testdb2"},{ "role" : "dbAdmin", "db" : "testdb2" }])
参考资料:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-08-19 Rancher管理K8s集群(14)
2022-08-19 基于Jenkins+k8s+Git等技术构建DeOps平台