介绍 ClickHouse 管理与运维相关的知识:权限、熔断机制、数据备份和服务监控。
1.1 用户配置
user.xml 配置文件位于 /etc/clickhouse-server 路径下,用于定义用户相关的配置项。
1.1.1 用户 profile
profile 类似于用户角色,可预先定义多组 profile 实现配置复用。
<yandex> <profiles> <default> <max_memory_usage>10000000000</max_memory_usage> <use_uncompressed_cache>0</use_uncompressed_cache> </default> <test1> <allow_experimental_live_view>1</allow_experimental_live_view> <distributed_product_mode>allow</distributed_product_mode> </test1> </profiles> </yandex>
切换 profile:
SET profile = test1;
profile 继承:
<normal_inherit> <profile>test1</profile> <profile>test2</profile> <distributed_product_mode>deny</distributed_product_mode> </normal_inherit>
⚠️ 必须存在名为
default的 profile,否则登录时会报错。
1.1.2 配置约束
constraints 标签设置参数修改约束:
| 规则 | 说明 |
|---|---|
| min | 最小值约束 |
| max | 最大值约束 |
| readonly | 只读约束 |
<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>
-- 超出约束值会报错 SET max_memory_usage = 50; -- DB::Exception: Setting max_memory_usage shouldn't be less than 5000000000.
1.1.3 用户定义
定义新用户必须包含以下属性:
1. username
全局唯一的登录用户名。
2. password
支持三种形式:
(1)明文密码:
<password>123</password> -- 空密码 = 免密登录 <password></password>
(2)SHA256 加密:
<password_sha256_hex>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3</password_sha256_hex>
生成加密串:
echo -n 123 | openssl dgst -sha256
(3)double_sha1 加密:
<password_double_sha1_hex>23ae809ddacaf96af0fd78ed04b6a265e05aa257</password_double_sha1_hex>
生成加密串:
echo -n 123 | openssl dgst -sha1 -binary | openssl dgst -sha1
3. networks
允许登录的网络地址(详见 11.2 节)。
4. profile
引用相应的 profile 名称:
<default> <profile>default</profile> </default>
5. quota
资源限额,熔断机制(详见 11.3 节)。
完整用户示例:
<users> <user_plaintext> <password>123</password> <networks> <ip>::/0</ip> </networks> <profile>normal_1</profile> <quota>default</quota> </user_plaintext> <user_sha256> <password_sha256_hex>a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3</password_sha256_hex> <networks><ip>::/0</ip></networks> <profile>default</profile> <quota>default</quota> </user_sha256> <user_double_sha1> <password_double_sha1_hex>23ae809ddacaf96af0fd78ed04b6a265e05aa257</password_double_sha1_hex> <networks><ip>::/0</ip></networks> <profile>default</profile> <quota>limit_1</quota> </user_double_sha1> </users>
1.2 权限管理
ClickHouse 权限体系分三层:访问权限 → 查询权限 → 数据权限。
1.2.1 访问权限
1. 网络访问权限
<user_normal> <password></password> <networks> <ip>10.37.129.13</ip> <!-- 或使用 host --> <host>ch5.nauu.com</host> <!-- 或正则匹配 --> <host>^ch\d.nauu.com$</host> </networks> </user_normal>
2. 数据库与字典访问权限
<user_normal> <allow_databases> <database>default</database> <database>test_dictionaries</database> </allow_databases> <allow_dictionaries> <dictionary>test_flat_dict</dictionary> </allow_dictionaries> </user_normal>
1.2.2 查询权限
| 权限类型 | 操作 |
|---|---|
| 读权限 | SELECT、EXISTS、SHOW、DESCRIBE |
| 写权限 | INSERT、OPTIMIZE |
| 设置权限 | SET |
| DDL 权限 | CREATE、DROP、ALTER、RENAME、ATTACH、DETACH、TRUNCATE |
| 其他权限 | KILL、USE(所有用户可用) |
控制标签:
<profiles> <normal> <readonly>1</readonly> -- 只读 <allow_ddl>0</allow_ddl> -- 禁止 DDL </normal> <normal_1> <readonly>2</readonly> -- 读 + SET <allow_ddl>0</allow_ddl> </normal_1> </profiles>
readonly 取值:
- 0:不限制(默认)
- 1:只读权限
- 2:读权限 + SET
allow_ddl 取值:
- 0:不允许 DDL
- 1:允许 DDL(默认)
1.2.3 数据行级权限
通过 databases 标签定义用户级别的查询过滤器:
<user_normal> <databases> <default> <test_row_level> <filter>id < 10</filter> </test_row_level> <!-- 支持组合条件 --> <test_query_all> <filter>id <= 100 or repo >= 100</filter> </test_query_all> </default> </databases> </user_normal>
⚠️ 使用了行级权限后,PREWHERE 优化不再生效。
1.3 熔断机制
当资源使用量达到阈值时自动中断操作。
1. 时间周期累积用量熔断
<quotas> <default> <interval> <duration>3600</duration> -- 时间周期(秒) <queries>0</queries> -- 查询次数(0=不限制) <errors>0</errors> -- 异常次数 <result_rows>0</result_rows> -- 结果行数 <read_rows>0</read_rows> -- 读取行数 <execution_time>0</execution_time> -- 执行时间(秒) </interval> </default> <limit_1> <interval> <duration>3600</duration> <queries>100</queries> <errors>100</errors> <result_rows>100</result_rows> <read_rows>2000</read_rows> <execution_time>3600</execution_time> </interval> </limit_1> </quotas>
用户引用:
<user_normal> <quota>limit_1</quota> </user_normal>
2. 单次查询用量熔断
在 profile 中定义:
| 配置项 | 说明 | 默认值 |
|---|---|---|
| max_memory_usage | 单次查询最大内存 | 10GB |
| max_memory_usage_for_user | 用户级别最大内存 | 0(不限) |
| max_memory_usage_for_all_queries | 全局最大内存 | 0(不限) |
| max_partitions_per_insert_block | 单次 INSERT 最大分区数 | 100 |
| max_rows_to_group_by | GROUP BY 最大聚合 Key 数 | 0(不限) |
| group_by_overflow_mode | 超阈值处理方式 | throw |
| max_bytes_before_external_group_by | 聚合查询最大内存(超阈值则用磁盘) | 0 |
group_by_overflow_mode 处理方式:
- throw: 抛出异常(默认)
- break: 停止查询,返回当前数据
- any: 仅根据现有聚合 Key 继续
1.4 数据备份
1.4.1 导出文件备份
# 导出 clickhouse-client --query="SELECT * FROM test_backup" > /chbase/test_backup.tsv # 导入 cat /chbase/test_backup.tsv | clickhouse-client --query "INSERT INTO test_backup FORMAT TSV" # 或直接复制目录 mkdir -p /chbase/backup/default/ cp -r /chbase/data/default/test_backup /chbase/backup/default/
1.4.2 通过快照表备份
-- 创建备份表
CREATE TABLE test_backup_0206 AS test_backup;
-- 点对点备份
INSERT INTO test_backup_0206 SELECT * FROM test_backup;
-- 远程节点备份
INSERT INTO test_backup_0206
SELECT * FROM remote('ch5.nauu.com:9000', 'default', 'test_backup', 'default');
1.4.3 按分区备份
FREEZE 备份
ALTER TABLE partition_v2 FREEZE PARTITION 201908;
备份文件保存在 <ch-path>/data/shadow/N/ 目录下(硬链接,不占用额外空间)。
还原: 将 shadow 下的分区复制到 detached 目录 → ATTACH:
ALTER TABLE partition_v2 ATTACH PARTITION '201908_5_5_0';
FETCH 备份
仅支持 ReplicatedMergeTree 系列:
ALTER TABLE test_fetch FETCH PARTITION 2019 FROM '/clickhouse/tables/01/test_fetch';
分区下载到 detached 目录,同样需要 ATTACH 还原。
⚠️ FREEZE 和 FETCH 都不会备份元数据。元数据在
/data/metadata/[table “” not found /] .sql,需手动复制。
1.5 服务监控
1.5.1 系统表
metrics
当前正在执行的概要信息:
SELECT * FROM system.metrics LIMIT 5; -- Query, Merge, PartMutation, ReplicatedFetch, ReplicatedSend
events
累积概要信息:
SELECT event, value FROM system.events LIMIT 5; -- Query, SelectQuery, InsertQuery, FileOpen, ReadBufferFromFileDescriptorRead
asynchronous_metrics
后台异步运行的概要信息:
SELECT * FROM system.asynchronous_metrics LIMIT 5; -- jemalloc.background_thread.*, jemalloc.retained, jemalloc.mapped
1.5.2 查询日志
默认关闭,需要在 config.xml 中开启。
query_log
最常用的查询日志,记录所有查询:
<query_log> <database>system</database> <table>query_log</table> <partition_by>toYYYYMM(event_date)</partition_by> <flush_interval_milliseconds>7500</flush_interval_milliseconds> </query_log>
SELECT type, concat(substr(query,1,20),'...') AS query, read_rows, query_duration_ms AS duration FROM system.query_log LIMIT 6;
query_thread_log
记录线程级查询信息:
<query_thread_log> <database>system</database> <table>query_thread_log</table> <flush_interval_milliseconds>7500</flush_interval_milliseconds> </query_thread_log>
part_log
MergeTree 分区操作日志:
<part_log> <database>system</database> <table>part_log</table> <flush_interval_milliseconds>7500</flush_interval_milliseconds> </part_log>
text_log
运行日志(INFO/DEBUG/Trace):
<text_log> <database>system</database> <table>text_log</table> <flush_interval_milliseconds>7500</flush_interval_milliseconds> </text_log>
metric_log
汇聚 metrics 和 events 数据:
<metric_log> <database>system</database> <table>metric_log</table> <flush_interval_milliseconds>7500</flush_interval_milliseconds> <collect_interval_milliseconds>1000</collect_interval_milliseconds> </metric_log>
1.6 小结
本章从三个维度介绍了 ClickHouse 的管理与运维:
安全性:
- 用户定义(明文/SHA256/double_sha1 密码)
- 三层权限体系(网络访问 → 查询操作 → 行级数据)
- Profile 配置与约束
健壮性:
- 时间周期熔断(quotas)
- 单次查询熔断(max_memory_usage 等)
- 资源保护机制
运维:
- 数据备份(导出文件 / 快照表 / FREEZE / FETCH)
- 系统表监控(metrics / events / asynchronous_metrics)
- 查询日志(query_log / query_thread_log / part_log / text_log / metric_log)