clickhouse 数据库默认的用户是default ,clickhouse 数据库的用户管理有两种方式:

1、直接修改配置文件(user.xml) 在用户配置文件里面配置需要创建的用户。

2、使用SQL直接创建用户并授权(比较灵活,无需重启服务)与mysql 管理用户类似,但是需要修改一下默认的user配置文件。默认的配置default 用户没有创建用户的权限。需要给default 用户添加access_management>1</access_management>选项

3、重启服务后使用default用户登录后创建一个管理员用户

clickhouse-client --user default --port 9000

create user  dba@‘%’  identified with plaintext_password by 'dba@123' on cluster cluster_name;

grant  on cluster cluster_name   all on *.*  to  'dba'@'%'  with grant option;

以后使用dba用户进行日常管理。

0、clickhouse 使用默认配置启动后,登录clickhouse 服务,尝试创建用户时报如下错误:

op-data-base :) create user clickhouse_admin@'%' identified with plaintext_password by 'clickhouse_password';

CREATE USER clickhouse_admin IDENTIFIED WITH plaintext_password BY 'clickhouse_password'

Query id: c3f78024-7d87-476d-b66e-b202aefa7bac

0 rows in set. Elapsed: 0.001 sec.

Received exception from server (version 22.6.2):
Code: 497. DB::Exception: Received from localhost:9000. DB::Exception: default: Not enough privileges. To execute this query it's necessary to have grant CREATE USER ON *.*. (ACCESS_DENIED)

这是因为default用户没有access_management权限。

1、开启default 用户的超级管理员权限添加 access_management>1</access_management>

<default>
   
            <password></password>
            <access_management>1</access_management>

2、重启clickhouse 服务

clickhouse  restart

[root@op-data-base clickhouse-server]# clickhouse restart
/var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 26366.
The process with pid = 26366 is running.
Sent terminate signal to process with pid 26366.
Waiting for server to stop
/var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 26366.
The process with pid = 26366 is running.
Waiting for server to stop
/var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 26366.
The process with pid = 26366 is running.
Waiting for server to stop
Now there is no clickhouse-server process.
Server stopped
 chown -R clickhouse: '/var/run/clickhouse-server/'
Will run clickhouse su 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon
Waiting for server to start
Waiting for server to start
Server started

3、创建用户并授权

op-data-base :) create user clickhouse_admin identified with plaintext_password by 'clickhouse_admin';

CREATE USER clickhouse_admin IDENTIFIED WITH plaintext_password BY 'clickhouse_admin'

Query id: eb4f758d-c3f8-4362-90ee-29785203351e

Ok.

0 rows in set. Elapsed: 0.001 sec.

4、授与新用户拥有所有管理权限

grant all on *.*  to  clickhouse_admin with grant option;

5、使用clickhouse_admin 用户创建一个列管理用户;

 clickhouse-client  --user clickhouse_admin --password clickhouse_admin

create user column_user identified  with plaintext_password by 'column_user'; 

CREATE USER column_user IDENTIFIED WITH plaintext_password BY 'column_user'

Query id: a62d60e1-c492-4404-acbf-7dff27d54c56

Ok.

0 rows in set. Elapsed: 0.001 sec.

6、创建一个行管理用户;

create user row_user identified with plaintext_password by 'row_user';

7、建库建表并插入数据

op-data-base :) select * from db1.t1;

SELECT *
FROM t1

Query id: 01ec08a4-abff-42a6-ad30-c555125b0531

┌─id─┬─column1─┬─column2─┐
│  1 │ A       │ abc     │
│  2 │ A       │ def     │
│  3 │ B       │ abc     │
│  4 │ B       │ def     │
└────┴─────────┴─────────┘

8、创建角色

1、创建一个角色 只允许查看表t1的Colum1列;

create role column1_users;

3. Creating roles​

With this set of examples, roles for different privileges such as columns and rows will be created, privileges will be granted to the roles and users will be assigned to each role. Roles are used to define groups of users for certain privileges instead of managing each user seperately.

  1. Create a role to restrict users of this role to only see column1 in database db1 and table1:

    CREATE ROLE column1_users;
    
  2. Set privileges to allow view on column1

    GRANT SELECT(id, column1) ON db1.table1 TO column1_users;
    
  3. Add the column_user user to the column1_users role

    GRANT column1_users TO column_user;
    
  4. Create a role to restrict users of this role to only see selected rows, in this case only rows containing A in column1

    CREATE ROLE A_rows_users;
    
  5. Add the row_user to the A_rows_users role

    GRANT A_rows_users TO row_user;
    
  6. Create a policy to allow view on only where column1 has the values of A

    CREATE ROW POLICY A_row_filter ON db1.table1 FOR SELECT USING column1 = 'A' TO A_rows_users;
    
  7. Set privileges to the database and table

    GRANT SELECT(id, column1, column2) ON db1.table1 TO A_rows_users;
    
  8. grant explicit permissions for other roles to still have access to all rows

    CREATE ROW POLICY allow_other_users_filter ON db1.table1 FOR SELECT USING 1 TO clickhouse_admin, column1_users;
    

    NOTE

    When attaching a policy to a table, the system will apply that policy and only those users and roles defined will be able to do operations on the table, all others will be denied any operations. In order to not have the restrictive row policy applied to other users, another policy must be defined to allow other users and roles to have regular or other types of access.

4. Testing role privileges with column restricted user​

  1. Log into the clickhouse client using the clickhouse_admin user

    clickhouse-client --user clickhouse_admin --password password
    
  2. Verify access to database, table and all rows with the admin user.

    SELECT *
    FROM db1.table1
    
    Query id: f5e906ea-10c6-45b0-b649-36334902d31d┌─id─┬─column1─┬─column2─┐
    │  1 │ A       │ abc     │
    │  2 │ A       │ def     │
    │  3 │ B       │ abc     │
    │  4 │ B       │ def     │
    └────┴─────────┴─────────┘
    
  3. Log into the ClickHouse client using the column_user user

    clickhouse-client --user column_user --password password
    
  4. Test SELECT using all columns

    SELECT *
    FROM db1.table1
    
    Query id: 5576f4eb-7450-435c-a2d6-d6b49b7c4a230 rows in set. Elapsed: 0.006 sec.Received exception from server (version 22.3.2):
    Code: 497. DB::Exception: Received from localhost:9000. DB::Exception: column_user: Not enough privileges. To execute this query it's necessary to have grant SELECT(id, column1, column2) ON db1.table1. (ACCESS_DENIED)
    

    NOTE

    Access is denied since all columns were specified and the user only has access to id and column1

  5. Verify SELECT query with only columns specified and allowed:

    SELECTid,column1
    FROM db1.table1
    
    Query id: cef9a083-d5ce-42ff-9678-f08dc60d4bb9┌─id─┬─column1─┐
    │  1 │ A       │
    │  2 │ A       │
    │  3 │ B       │
    │  4 │ B       │
    └────┴─────────┘
    

5. Testing role privileges with row restricted user​

  1. Log into the ClickHouse client using row_user

    clickhouse-client --user row_user --password password
    
  2. View rows available

    SELECT *
    FROM db1.table1
    
    Query id: a79a113c-1eca-4c3f-be6e-d034f9a220fb┌─id─┬─column1─┬─column2─┐
    │  1 │ A       │ abc     │
    │  2 │ A       │ def     │
    └────┴─────────┴─────────┘
    

4. Modifying Users and Roles​

Users can be assigned multiple roles for a combination of privileges needed. When using multiple roles, the system will combine the roles to determine privileges, the net effect will be that the role permissions will be cumulative.

For example, if one role1 allows for only select on column1 and role2 allows for select on column1 and column2 then the user will have access to both columns.

  1. Using the admin account, create new user to restrict by both row and column with default roles

    CREATE USER row_and_column_user IDENTIFIED WITH plaintext_password BY 'password' DEFAULT ROLE A_rows_users;
    
  2. Remove prior privileges for A_rows_users role

    REVOKE SELECT(id, column1, column2) ON db1.table1 FROM A_rows_users;
    
  3. Allow A_row_users role to only select from column1

    GRANT SELECT(id, column1) ON db1.table1 TO A_rows_users;
    
  4. Log into the ClickHouse client using row_and_column_user

    clickhouse-client --user row_and_column_user --password password;
    
  5. Test with all columns:

    SELECT *
    FROM db1.table1
    
    Query id: 8cdf0ff5-e711-4cbe-bd28-3c02e52e8bc4
    
0 rows in set. Elapsed: 0.005 sec.Received exception from server (version 22.3.2):
Code: 497. DB::Exception: Received from localhost:9000. DB::Exception: row_and_column_user: Not enough privileges. To execute this query it's necessary to have grant SELECT(id, column1, column2) ON db1.table1. (ACCESS_DENIED)
```
  1. Test with limited allowed columns:

    SELECTid,column1
    FROM db1.table1
    
    Query id: 5e30b490-507a-49e9-9778-8159799a6ed0┌─id─┬─column1─┐
    │  1 │ A       │
    │  2 │ A       │
    └────┴─────────┘
    
  2. Examples on how to delete privileges, policies, unassign users from roles, delete users and roles:

    • Remove privilege from a role
    REVOKE SELECT(column1, id) ON db1.table1 FROM A_rows_users;
    
    • Delete a policy
    DROP ROW POLICY A_row_filter ON db1.table1;
    
    • Unassign a user from a role
    REVOKE A_rows_users FROM row_user;
    
    • Delete a role
    DROP ROLE A_rows_users;
    
    • Delete a user
    DROP USER row_user;
    

5. Troubleshooting​

  1. There are occasions when privileges intersect or combine to produce unexpected results, the following commands can be used to narrow the issue using an admin account

    • Listing the grants and roles for a user
    SHOW GRANTS FOR row_and_column_user
    
    Query id: 6a73a3fe-2659-4aca-95c5-d012c138097b┌─GRANTS FOR row_and_column_user───────────────────────────┐
    │ GRANT A_rows_users, column1_users TO row_and_column_user │
    └──────────────────────────────────────────────────────────┘
    
    • List roles in ClickHouse
    SHOW ROLES
    
    Query id: 1e21440a-18d9-4e75-8f0e-66ec9b36470a┌─name────────────┐
    │ A_rows_users    │
    │ column1_users   │
    └─────────────────┘
    
    • Display the policies
    SHOW ROW POLICIES
    
    Query id: f2c636e9-f955-4d79-8e80-af40ea227ebc┌─name───────────────────────────────────┐
    │ A_row_filter ON db1.table1             │
    │ allow_other_users_filter ON db1.table1 │
    └────────────────────────────────────────┘
    
    • View how a policy was defined and current privileges
    SHOW CREATE ROW POLICY A_row_filter ON db1.table1
    
    Query id: 0d3b5846-95c7-4e62-9cdd-91d82b14b80b┌─CREATE ROW POLICY A_row_filter ON db1.table1────────────────────────────────────────────────┐
    │ CREATE ROW POLICY A_row_filter ON db1.table1 FOR SELECT USING column1 = 'A' TO A_rows_users │
    └─────────────────────────────────────────────────────────────────────────────────────────────┘
    

Summary​

This article demostrated the basics of creating SQL users and roles and provided steps to set and modify privileges for users and roles. For more detailed information on each please refer to our user guides and reference documenation.

clickhouse 如何使用SQL 管理用户和角色相关推荐

  1. springBoot+springSecurity 数据库动态管理用户、角色、权限(二)

    序:  本文使用springboot+mybatis+SpringSecurity 实现数据库动态的管理用户.角色.权限管理 本文细分角色和权限,并将用户.角色.权限和资源均采用数据库存储,并且自定义 ...

  2. linux服务器管理公司用户,在Linux服务器Jenkins中管理用户和角色的方法

    下面将教你如何在Linux服务器Jenkins中管理用户和角色,它需要创建角色并分配给用户,你需要运行Jenkins服务器才能操作接下来的工作. 安装Jenkins参考文章 在Jenkins中管理用户 ...

  3. PostgreSQL 用户和角色管理

    PostgreSQL的用户管理 用户和角色的区别:用户有权限.角色没有权限 一.组角色管理 ---->使用组角色的概念管理数据库访问权限: 1.创建组角色 一个组角色可以看做是一组数据用户.组角 ...

  4. oracle中角色和用户权限,Oracle用户、角色、权限管理

    用户在会话的权利上,应该有其他操作的权利:Oracle的用户和口令不区分大小写,真是让人大跌眼镜:Oralce中,所有用户必须明确被 create or replace type address as ...

  5. mysql 角色管理_MySQL 8 用户和角色管理入门

    MySQL 8.0 正式版目前已发布,MySQL 8.0 增加了很多新的功能,具体可参考「MySQL 8.0 正式版 8.0.11 发布!」一文. MySQL 8.0 在用户管理方面增加了角色管理,默 ...

  6. Oracle操作管理之用户和角色

    1.用户管理 (1)建立用户(数据库验证) CREATE USER smith IDENTIFIED BY smith_pwd DEFAULTTABLESPACE users TEMPORARY TA ...

  7. SQL Server 数据库之角色、管理权限

    角色.管理权限 1. 角色 1.1 概述 1.2 预定义角色 1.3 角色的操作 2. 管理权限 2.1 概述 2.2 授予权限 2.3 收回权限 1. 角色 1.1 概述 一个数据库可能会有许多个用 ...

  8. Oracle数据库用户管理之二---权限授权去权(用户和角色)

                                Oracle数据库用户管理之二---权限授权去权(用户和角色) 书接上回,https://blog.csdn.net/alwaysbefine/ ...

  9. oracle 授权访问条空间,oracle创建用户和角色、管理授权以及表空间操作

    show user 显示当前用户 connect username/password@datebasename as sysdba 切换用户和数据库 和用户身份 Oracle登录身份有三种: norm ...

最新文章

  1. [转载]IPMSG(飞鸽传书)协议翻译
  2. 特斯拉线圈的阻抗分析
  3. openCV中的findHomography函数分析以及RANSAC算法的详解(源代码分析)
  4. 程序员面试题精选100题(03)-子数组的最大和[算法]
  5. eclipse中如何配置tomcat
  6. P4827-[国家集训队]Crash 的文明世界【树形dp,换根法,斯特林数】
  7. 在 Ubuntu 14.04 中配置 PXE 服务器
  8. stm32 web get 参数_纯进口mpv销量排行榜 迈巴赫vs680商务车参数
  9. Android中如何获取应用版本号
  10. 从 重复叠加字符串匹配 看Java String源码中的contains方法
  11. LeetCode刷题(14)
  12. 遍历字段_以字段覆盖标准指导的高效测试生成技术
  13. python可以下载百度文库_百度文档,用Python一键免费下载
  14. iOS--在线搜索苹果 App Store 应用商店
  15. 各国程序员薪资水平,看完我想静静。。。
  16. linux vi编译显示行号,Linux系统vi或者vim编辑器中如何显示行号
  17. 【Derivation】 条件数学期望公式泊松分布推导(Poisson distribution)
  18. JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@xxx] will not be managed by Spring
  19. 好看简约加速器官网源码
  20. 实现WinForm的DataGridView折叠功能(非原创,仅供收藏)

热门文章

  1. DIY电脑检测软件大集中
  2. 魅族Android10内测招募答案,魅族flyme9内测招募答案,魅族16系列flyme9内测招募题目答案免费分享预约 v1.0-手游汇...
  3. 黑客突破防火墙常用的几种技术(转)
  4. Mysql出现问题:ERROR 1062 (23000): Duplicate entry ‘‘ for key ‘PRIMARY‘解决方案
  5. vite:配置ip访问
  6. 塞拉利昂一公司计划投资10亿美元用于建设光伏农业项目
  7. 解决Failed to load module canberra-gtk-module错误
  8. Websocket服务端和客户端通信(WSS、WS)
  9. 深圳盛世光影简述影视后期制作包括哪些工作?
  10. Dango Web 开发指南 学习笔记 2