在之前使用ClickHouse过程中为了简便我们使用的default默认用户,默认无密码登录模式,这显然不符合生产环境的要求。我们可以通过“/etc/clickhoue-server/user.xml”来配置用户相关配置项,此文件可以配置系统参数设置、用户定义、权限、以及熔断机制等。
用户Profile类似用户角色,可以预先在user.xml中为ClickHouse定义多组profile角色,并为每组角色定义不同的配置项以实现配置的复用,例如:
<yandex>
<!-- 配置profiles -->
<profiles>
<default><!-- 自定义角色,默认名称 -->
<max_memory_usage>10000000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
</default>
<test1><!-- 自定义角色,默认名称 -->
<distributed_product_mode>allow</distributed_product_mode>
</test1>
... ...
</profiles>
... ...
</yandex>注意:
distributed_product_mode:当查询时包含分布式表join时,更改分布式子查询行为。allow允许使用子查询。
以上配置中我们定义了“default”和“test1”两组profile角色,在user.xml中配置用户时,我们就可以使用对应的角色。
注意:在<profiles>标签配置中,名称为“default”的profile将作为默认的配置被加载,所以必须有“default”角色。
constraints标签可以设置一组约束条件,保证profile角色内的参数值不会被随意修改,约束条件有如下三种规则:
例如,修改user.xml中<default>profile角色,如下:
<yandex>
<!-- 配置profiles -->
<profiles>
<default><!-- 自定义角色,默认名称 -->
<max_memory_usage>10000000000</max_memory_usage>
<distributed_product_mode>allow</distributed_product_mode>
<constraints><!--配置约束-->
<max_memory_usage>
<min>5000000000</min>
<max>20000000000</max>
</max_memory_usage>
<distributed_product_mode>
<readonly/>
</distributed_product_mode>
</constraints>
</default>
... ...
</profiles>
... ...
</yandex>从上面的配置我们可以看出,在default默认的profile角色内给两组参数设置了约束,配置完成之后可以直接登录ClickHouse,尝试修改对应的值:
#修改使用最大内存
node1 :) set max_memory_usage=50;
DB::Exception: Setting max_memory_usage shouldn't be less than 5000000000. (SETTING_CONSTRAINT_VIOLATION)
#修改distributed_product_mode的参数值
node1 :) set distributed_product_mode='deny';
DB::Exception: Setting distributed_product_mode should not be changed. (SETTING_CONSTRAINT_VIOLATION)以上我们可以看到配置约束成功阻止了预期外的修改。
注意:在default中默认定义的constraints约束,将作为默认全局约束,自动被其他profile继承。
在user.xml中可以使用users标签配置自定义用户,我们可以在user.xml中看到<users>标签下有default用户,我们之前使用默认就是此用户。下面我们配置一个新的用户“zhangsan”,user.xml配置如下:
<yandex>
... ...
<users>
<default>
... ...
</default>
<zhangsan>
<!-- 设置密码 -->
<password>123456</password>
<!-- 设置用户允许登录的网络地址 -->
<networks>
<!-- 表示任意节点登录 -->
<ip>::/0</ip>
</networks>
<!-- 用户使用的profile 角色 -->
<profile>default</profile>
<!-- 设置熔断机制 -->
<quota>default</quota>
</zhangsan>
</users>
</yandex>以上配置完成后,不需重启ClickHouse,使用用户zhangsan登录ClickHouse,操作如下:
[root@node1 ~]# ClickHouse-client -u zhangsan --password 123456用户zhangsan有密码登录生效。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。