Clickhouse数据库之权限管理

标签: Clickhouse 分类: 数据库 创建时间:2020-06-02 08:18:08 更新时间:2023-10-20 11:23:25

1.配置用户名密码

安装完ClickHouse之后,默认情况下它使用‘default’用户无密码的与localhost:9000服务建立连接。 客户端也可以用于连接远程服务。
ClickHouse主要配置文件问config.xml,配置文件位置为:/etc/clickhouse-server/config.xml。可以在配置文件旁边的conf.d和config.d目录的* .xml和* .conf文件中覆盖各个设置。不同的用户配置文件就可以放在user.d文件夹下。
(1) 随机生成密钥

1
2
3
4
## 随机生成密钥
PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
## 指定密码生成sha256密钥
PASSWORD=user1q2w3e4r.; echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'

(2) 在user.d文件夹下新建newuser.xml文件
其中users节点下的admin就是新建的用户名,密码是上面生成的sha256加密后端字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0"?>
<yandex>
<profiles>
<default>
<max_memory_usage>10000000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
<load_balancing>random</load_balancing>
</default>
</profiles>
<quotas>
<!-- Name of quota. -->
<default>
<interval>
<duration>3600</duration>
<queries>0</queries>
<errors>0</errors>
<result_rows>0</result_rows>
<read_rows>0</read_rows>
<execution_time>0</execution_time>
</interval>
</default>
</quotas>
<users>
<admin>
<password_sha256_hex>dac6badf50999c40b7241c3df821dc0dfca7603badda01bf60dbc10d9da17b69</password_sha256_hex>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</admin>
</users>
</yandex>

如果禁止defualt用户在外网登录,则添加users

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 <users>
<admin>
<password_sha256_hex>dac6badf50999c40b7241c3df821dc0dfca7603badda01bf60dbc10d9da17b69</password_sha256_hex>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</admin>
<default>
<networks incl="networks" replace="replace">
<ip>::1</ip>
<ip>127.0.0.1</ip>
</networks>
</default>
</users>

(3) 登录数据库
修改配置文件后,不需要重新启动服务。服务器跟踪配置文件以及执行替换和替代时使用的文件和ZooKeeper节点的更改,并即时重新加载用户和集群的设置。这意味着您可以修改群集,用户及其设置,而无需重新启动服务器。

1
2
## 使用用户名admin和password登录lsmfhx数据库
clickhouse-client -h 127.0.0.1 -d lsmfhx -m -u admin --password 1q2w3e4r

2.开启远程连接

安装之后,默认是监听本机的8123端口,所以我们要开启来自局域网的连接。
(1) 查看监听端口

1
2
## 查看监听端口
lsof -i :8123

(2) 停止clickhouse服务

1
2
## CentOS6.5
sudo service clickhouse-server stop

(3) 编辑 /etc/clickhouse-server/config.xml 文件
找到下面内容,将listen_host两边的注释打开

1
2
<!-- Listen specified host. use :: (wildcard IPv6 address), if you want to accept connections both with IPv4 and IPv6 from everywhere. -->
<listen_host>::</listen_host>

(4) 重启clickhouse服务

1
2
3
4
5
## CentOS6.5
sudo service clickhouse-server start

## CentOS 7+
systemctl restart clickhouse-server

注意
注意
注意

要关闭虚拟机中的防护墙

1
2
3
4
5
6
##  查看运行状态
systemctl status firewalld
## 关闭防火墙
systemctl stop firewalld
## 禁止开机启动
systemctl disable firewalld

需要的话,还要关闭SElinux,我这里没有关,也是可以的。

3.权限设置

配置只读用户

1
2
3
4
5
6
7
8
9
10
11
<users>
<user>
<password_sha256_hex>a07fa339a8dbc7984ba9f30844541913db1e17ded6fb18e7cbbae7f4845ff9b3</password_sha256_hex>
<networks incl="newworks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>readonly</profile>
<quota>default</quota>
</user>
</users>

4.配置数据库权限

编辑/etc/clickhouse-server/users.d下的配置文件,可以指定用户的单独的数据库权限,经过测试,default必须写上,否则读不到数据库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<users>
<!-- If user name was not specified, 'default' user is used. -->
<user_name> --配置的用户
<password></password> --明文密码
<!-- Or -->
<password_sha256_hex></password_sha256_hex> --加密密码,二选一

<networks incl="networks" replace="replace"> --允许登录的地址,用于限制用户登录的客户端地址
</networks>

<profile>profile_name</profile> --指定用户的profile

<quota>default</quota> -- 指定用户的quota,限制用户使用资源

<allow_databases>
<database>gdnx</database>
<database>default</database>
</allow_databases>

</user_name>
<!-- Other users settings -->
</users>
参考文章:
1.User settings:用户配置 (这篇文章对数据库及表的权限有详细的介绍)
2.二.clickhouse 配置认证(clickhouse 极简教程系列)
小额赞助
本人提供免费与付费咨询服务,感谢您的支持!赞助请发邮件通知,方便公布您的善意!
**光 3.01 元
Sun 3.00 元
bibichuan 3.00 元
微信公众号
广告位
诚心邀请广大金主爸爸洽谈合作
每日一省
isNaN 和 Number.isNaN 函数的区别?

1.函数 isNaN 接收参数后,会尝试将这个参数转换为数值,任何不能被转换为数值的的值都会返回 true,因此非数字值传入也会返回 true ,会影响 NaN 的判断。

2.函数 Number.isNaN 会首先判断传入参数是否为数字,如果是数字再继续判断是否为 NaN ,不会进行数据类型的转换,这种方法对于 NaN 的判断更为准确。

每日二省
为什么0.1+0.2 ! == 0.3,如何让其相等?

一个直接的解决方法就是设置一个误差范围,通常称为“机器精度”。对JavaScript来说,这个值通常为2-52,在ES6中,提供了Number.EPSILON属性,而它的值就是2-52,只要判断0.1+0.2-0.3是否小于Number.EPSILON,如果小于,就可以判断为0.1+0.2 ===0.3。

每日三省
== 操作符的强制类型转换规则?

1.首先会判断两者类型是否**相同,**相同的话就比较两者的大小。

2.类型不相同的话,就会进行类型转换。

3.会先判断是否在对比 null 和 undefined,是的话就会返回 true。

4.判断两者类型是否为 string 和 number,是的话就会将字符串转换为 number。

5.判断其中一方是否为 boolean,是的话就会把 boolean 转为 number 再进行判断。

6.判断其中一方是否为 object 且另一方为 string、number 或者 symbol,是的话就会把 object 转为原始类型再进行判断。

每日英语
Happiness is time precipitation, smile is the lonely sad.
幸福是年华的沉淀,微笑是寂寞的悲伤。