暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

clickhouse基本操作

原创 kobrey 2021-08-26
1189

1:创建角色
?
1 CREATE ROLE [IF NOT EXISTS | OR REPLACE] name
2     [SETTINGS variable [= value] [MIN [=] min_value] [MAX [=] max_value] [READONLY|WRITABLE] | PROFILE ‘profile_name’] [,…]
2:创建账号
?
1 CREATE USER [IF NOT EXISTS | OR REPLACE] name [ON CLUSTER cluster_name]
2     [IDENTIFIED [WITH {NO_PASSWORD|PLAINTEXT_PASSWORD|SHA256_PASSWORD|SHA256_HASH|DOUBLE_SHA1_PASSWORD|DOUBLE_SHA1_HASH}] BY {‘password’|‘hash’}]
3     [HOST {LOCAL | NAME ‘name’ | REGEXP ‘name_regexp’ | IP ‘address’ | LIKE ‘pattern’} [,…] | ANY | NONE]
4     [DEFAULT ROLE role [,…]]
5     [SETTINGS variable [= value] [MIN [=] min_value] [MAX [=] max_value] [READONLY|WRITABLE] | PROFILE ‘profile_name’] [,…]
?
1 # 创建角色
centf8118.sharding1.db 😃 CREATE ROLE DBA;
2  
3 CREATE ROLE DBA
4  
5  
6 Received exception from server (version 20.6.4):
7 Code: 497. DB::Exception: Received from localhost:9000. DB::Exception: default: Not enough privileges. To execute this query it’s necessary to have the grant CREATE ROLE ON ..
8  
9 0 rows in set. Elapsed: 0.003 sec.
 
?
1 # 创建账号
centf8118.sharding1.db 😃 CREATE USER dba_u@‘192.168.%’ IDENTIFIED WITH sha256_password BY ‘123456’;
2  
3 CREATE USER dba_u@192.168.% IDENTIFIED WITH sha256_hash BY ‘8D969EEF6ECAD3C29A3A629280E686CF0C3F5D5A86AFF3CA12020C923ADC6C92’ HOST LIKE ‘192.168.%’
4  
5  
6 Received exception from server (version 20.6.4):
7 Code: 497. DB::Exception: Received from 192.168.81.18:9000. DB::Exception: xinchen: Not enough privileges. To execute this query it’s necessary to have the grant CREATE USER ON ..
8  
9 0 rows in set. Elapsed: 0.004 sec.
10  
11 centf8118.sharding1.db 😃
这里不管创建角色还是账号都会报错,提示没有足够权限:
Not enough privileges. To execute this query it’s necessary to have the grant CREATE ROLE ON ..
Not enough privileges. To execute this query it’s necessary to have the grant CREATE USER ON ..
这里clickhouse的权限不像mysql直接创建角色,用户。而是需要在配置文件中添加角色用户。
默认的配置文件路径是:/etc/clickhouse-server
其中有config.xml,users.xml,一个是服务器相关配置,一个是用户权限的配置。下面看看users.xml。这里面分几部分。

  1. 用户名,用户密码,访问来源地址
  2. 资源限制,和greenplum有点像。
  3. 配置设置,这其中有用户是否只读。目前只有select和insert。所以权限也比较简单。
    密码需要特殊说明,如果不写,那么就是空密码,也可以写明文密码,也可以写密文,可以用如下命令生成密文密码:
    ?
    1 PASSWORD=(base64</dev/urandomheadc8);echo"(base64 < /dev/urandom | head -c8); echo "PASSWORD"; echo -n "PASSWORD" | sha256sum | tr -d '-' 我这里执行随机生成的明文和加密后的密码: ? 1 [root@centf8118 ~]# PASSWORD=(base64 < /dev/urandom | head -c8); echo “PASSWORD";echon"PASSWORD"; echo -n "PASSWORD” | sha256sum | tr -d ‘-’
    2 Jt9Us0rG   ## 明文密码
    3 de5a7f4866fd2205876184dce54bc89921052a9057a9fc4e4346f4e073a2123d   ## 加密后的密码
    然后在users.xml中加入要新创建的账号。
    ?
    1
    2     123456
    3     
    4         ::/0
    5     

    6     default
    7     default
    8     <allow_databases>
    9         default
    10     </allow_databases>
    11     <access_management>1</access_management>
    12

    13  
    14
    15     <password_sha256_hex>de5a7f4866fd2205876184dce54bc89921052a9057a9fc4e4346f4e073a2123d</password_sha256_hex>
    16     
    17         ::/0
    18     

    19     readonly
    20     default
    21     <allow_databases>
    22         default
    23         testdb
    24     </allow_databases>
    25

    我这里是创建两个用户:dba和xinchen,
    dba用户采用明文密码,默认权限,<allow_databases>允许访问的数据库是default。
    admin用户采用密文密码,只读权限,<allow_databases>允许访问的数据库是default,testdb。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论