搭建说明
Clickhouse集群依赖Zookeeper集群。因此需要先搭建zk集群。
请先参考 【记录】zookeeper集群搭建详细步骤 完成zookeeper集群搭建。
如果zookeeper集群已成功搭建完成,下面开始搭建Clickhouse集群。
需要环境:
实例1 | 实例2 | 实例3 | |
---|---|---|---|
IP | 192.168.11.11 | 192.168.11.12 | 192.168.11.13 |
OS | centos7.9 | centos7.9 | centos7.9 |
zk | zookeeper-3.8.0 | zookeeper-3.8.0 | zookeeper-3.8.0 |
clickhouse | 22.2.2.1-2 | 22.2.2.1-2 | 22.2.2.1-2 |
java | JDK8 | JDK8 | JDK8 |
1.rpm安装
# 下载
cd /usr/local/clickhouse
wget https://mirrors.tuna.tsinghua.edu.cn/clickhouse/rpm/stable/x86_64/clickhouse-client-22.2.2.1-2.noarch.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/clickhouse/rpm/stable/x86_64/clickhouse-common-static-22.2.2.1-2.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/clickhouse/rpm/stable/x86_64/clickhouse-server-22.2.2.1-2.noarch.rpm
# 安装命令
rpm -ivh clickhouse-common-static-22.2.2.1-2.x86_64.rpm clickhouse-client-22.2.2.1-2.noarch.rpm clickhouse-server-22.2.2.1-2.noarch.rpm
在三台实例上重复上述操作
2.配置修改
cd /etc/clickhouse-server
config.xml
vim config.xml
<listen_host>0.0.0.0</listen_host><remote_servers><enic_cluster><shard><replica><host>192.168.11.11</host><port>9000</port></replica></shard><shard><replica><host>192.168.11.12</host><port>9000</port></replica></shard><shard><replica><host>192.168.11.13</host><port>9000</port></replica></shard></enic_cluster>
</remote_servers>
users.xml
vim users.xml
<enic><password>12345678</password><networks incl="networks" replace="replace"><ip>::/0</ip></networks><profile>default</profile><quota>default</quota>
</enic>
metrika.xml
cd /etc/clickhouse-server
vim metrika.xml
<yandex><zookeeper-servers><node index="1"><host>192.168.11.11</host><port>2181</port></node><node index="2"><host>192.168.11.12</host><port>2181</port></node><node index="3"><host>192.168.11.13</host><port>2181</port></node></zookeeper-servers>
</yandex>
重启clickhouse-server
systemctl restart clickhouse-server
查看clickhouse-server状态
systemctl status clickhouse-server
● clickhouse-server.service - ClickHouse Server (analytic DBMS for big data)Loaded: loaded (/etc/systemd/system/clickhouse-server.service; enabled; vendor preset: disabled)Active: active (running) since Fri 2022-04-29 11:38:43 CST; 2h 49min agoMain PID: 30158 (clckhouse-watch)Tasks: 203 (limit: 22997)Memory: 437.8MCGroup: /system.slice/clickhouse-server.service├─30158 clickhouse-watchdog --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid└─30159 /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-serve>
在三台实例上重复上述操作
其它操作
启动clickhouse-server
systemctl start clickhouse-server
关闭clickhouse-server
systemctl stop clickhouse-server
查看clickhouse-server状态
systemctl status clickhouse-server
3.查看集群状态
clickhouse-client --host="127.0.0.1" --port="9000" --user="enic" --password="12345678"
select * from system.clusters where cluster = 'enic_cluster';
SELECT *
FROM system.clusters
WHERE cluster = 'enic_cluster'Query id: eb2064de-92f3-41b0-ac74-6b025d5082a1┌─cluster──────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name─────┬─host_address──┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─slowdowns_count─┬─estimated_recovery_time─┐
│ enic_cluster │ 1 │ 1 │ 1 │ 192.168.11.11 │ 192.168.11.11 │ 9000 │ 0 │ default │ │ 0 │ 0 │ 0 │
│ enic_cluster │ 2 │ 1 │ 1 │ 192.168.11.12 │ 192.168.11.12 │ 9000 │ 1 │ default │ │ 0 │ 0 │ 0 │
│ enic_cluster │ 3 │ 1 │ 1 │ 192.168.11.13 │ 192.168.11.13 │ 9000 │ 0 │ default │ │ 0 │ 0 │ 0 │
└──────────────┴───────────┴──────────────┴─────────────┴───────────────┴───────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────┴─────────────────────────┘3 rows in set. Elapsed: 0.002 sec.
4.集群操作
创建本地表
CREATE TABLE enic_test_local ON CLUSTER enic_cluster (
id Int16,
name String,
birth Date
)ENGINE = MergeTree()
PARTITION BY toYYYYMM(birth)
ORDER BY id;
insert into enic_test_local values(1, 'test1', '2022-02-01'), (2, 'test2', '2022-03-01'), (3, 'test3', '2022-04-01');
select * from enic_test_local;
创建分布式表
CREATE TABLE default.enic_test ON CLUSTER enic_cluster as enic_test_local engine = Distributed(enic_cluster, default, enic_test_local,rand());
CREATE TABLE default.enic_test ON CLUSTER enic_cluster AS enic_test_local
ENGINE = Distributed(enic_cluster, default, enic_test_local, rand())Query id: 22bc54a5-a615-4756-9132-a76b65bc5664┌─host──────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ 192.168.11.13 │ 9000 │ 0 │ │ 2 │ 0 │
│ 192.168.11.12 │ 9000 │ 0 │ │ 1 │ 0 │
│ 192.168.11.11 │ 9000 │ 0 │ │ 0 │ 0 │
└───────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘3 rows in set. Elapsed: 0.128 sec.
insert into enic_test values(1, 'test1', '2022-02-01'), (2, 'test2', '2022-03-01'), (3, 'test3', '2022-04-01');
INSERT INTO enic_test FORMAT ValuesQuery id: efd75f9f-c101-4d7f-91ae-448bea1a3aa9Ok.3 rows in set. Elapsed: 0.005 sec.
其他节点查询
select * from enic_test;
SELECT *
FROM enic_testQuery id: 2e74c5b6-b16f-41ef-888d-ea2e6047d6a9┌─id─┬─name──┬──────birth─┐
│ 1 │ test1 │ 2022-02-01 │
└────┴───────┴────────────┘
┌─id─┬─name──┬──────birth─┐
│ 2 │ test2 │ 2022-03-01 │
└────┴───────┴────────────┘
┌─id─┬─name──┬──────birth─┐
│ 3 │ test3 │ 2022-04-01 │
└────┴───────┴────────────┘
┌─id─┬─name──┬──────birth─┐
│ 1 │ test1 │ 2022-02-01 │
└────┴───────┴────────────┘
┌─id─┬─name──┬──────birth─┐
│ 3 │ test3 │ 2022-04-01 │
└────┴───────┴────────────┘
┌─id─┬─name──┬──────birth─┐
│ 2 │ test2 │ 2022-03-01 │
└────┴───────┴────────────┘6 rows in set. Elapsed: 0.007 sec.
以上,完成clickhouse 3shard1replica集群部署。