clickhouse replicated引擎
只有 MergeTree 系列里的表可支持副本:(CREATE,DROP,ATTACH,DETACH 和 RENAME 语句只会在单个服务器上执行,不会复制)
ReplicatedMergeTree
ReplicatedSummingMergeTree
ReplicatedReplacingMergeTree
ReplicatedAggregatingMergeTree
ReplicatedCollapsingMergeTree
ReplicatedVersionedCollapsingMergeTree
ReplicatedGraphiteMergeTree
使用docker compose 安装zookeeper和clickhouse
version: "3.9"
services:
zookeeper:
image: bitnami/zookeeper:3.8.1
container_name: dir-zookeeper
restart: always
user: root
ports:
- "${zookeeper_port}:2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
volumes:
- ${zookeeper_data_dir}:/data
- ${zookeeper_conf_dir}:/conf
- ${zookeeper_log_dir}:/datalog
- /etc/localtime:/etc/localtime:ro
master-clickhouse:
image: yandex/clickhouse-server:21.3.20.1
container_name: master-clickhouse
restart: always
user: root
ports:
- '${ch_master_http_port}:8123'
- '${ch_master_tcp_port}:9000'
volumes:
# 默认配置
- ${ch_master_conf_dir}/config.xml:/etc/clickhouse-server/config.xml:rw
- ${ch_master_conf_dir}/users.xml:/etc/clickhouse-server/users.xml:rw
- /etc/localtime:/etc/localtime:ro
# 运行日志
- ${ch_master_log_dir}:/var/log/clickhouse-server
# 数据持久
- ${ch_master_data_dir}:/var/lib/clickhouse:rw
healthcheck:
test: [ "CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/${ch_master_http_port}" ]
interval: 5s
timeout: 60s
retries: 120
slave-clickhouse:
image: yandex/clickhouse-server:21.3.20.1
container_name: slave-clickhouse
restart: always
user: root
ports:
- '${ck_slave_http_port}:8123'
- '${ck_slave_tcp_port}:9000'
volumes:
# 默认配置
- ${ch_slave_conf_dir}/config.xml:/etc/clickhouse-server/config.xml:rw
- ${ch_slave_conf_dir}/users.xml:/etc/clickhouse-server/users.xml:rw
- /etc/localtime:/etc/localtime:ro
# 运行日志
- ${ch_slave_log_dir}:/var/log/clickhouse-server
# 数据持久
- ${ch_slave_data_dir}:/var/lib/clickhouse:rw
healthcheck:
test: [ "CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/${ck_slave_http_port}" ]
interval: 5s
timeout: 60s
retries: 120
在docker compose文件的当前目录下新建.env文件,并配置对应变量的值
配置clickhouse 数据库配置文件config.xml,添加
<zookeeper>
<node>
<host>zk的地址</host>
<port>zk的端口</port>
</node>
</zookeeper>
<!-- 配置宏 -->
<macros>
<!-- replica 变量名可以自定义, 值可以随便写,我这里主节点使用node1,备用节点使用node2-->
<replica>node1</replica>
</macros>
<!-- 指定 Replicated 数据表引擎的默认参数 -->
<!-- default_replica_path 内容可以自定义,对应zookeeper上的目录,对应某一个表要唯一,分片的话需要需要添加分片标识 -->
<default_replica_path>/clickhouse/tables/{database}/{table}</default_replica_path>
<default_replica_name>{replica}</default_replica_name>
回到docker compose启动服务
docker compose -f docker-compose.yml up -d
主备节点创建测试表
CREATE TABLE test(
x UInt32
) ENGINE = ReplicatedMergeTree
ORDER BY x;
标签:实战,xml,ch,slave,etc,clickhouse,为主,dir
From: https://www.cnblogs.com/fylh/p/18054596