先安装zookeeper集群。

安装简单理解配置需要仔细研究

4个节点每2主副本为一组分片

直接yum安装先安单机逐个安装

yum install yum-utils -y
rpm --import https://repo.clickhouse.com/CLICKHOUSE-KEY.GPG
yum-config-manager --add-repo https://repo.clickhouse.com/rpm/stable/x86_64
yum install clickhouse-server clickhouse-client -y

修改配置文件config.xml

#分布式互联地址(建议使用host配置集群域名)
<interserver_http_host>ch01</interserver_http_host>
#开通可以远程访问
<listen_host>0.0.0.0</listen_host>
#配置集群类型
##<tcfuture_cluster> 自定义的集群名字
##<internal_replication>true</internal_replication>开始副本复制
##一组<shard>表示一组分片,里面有几个<replica>表示有个副本
        <tcfuture_cluster>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>ch01</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>P@ssw0rd</password>
                </replica>
                <replica>
                    <host>ch02</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>P@ssw0rd</password>
                </replica>
            </shard>
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>ch03</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>P@ssw0rd</password>
                </replica>
                <replica>
                    <host>ch04</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>P@ssw0rd</password>
                </replica>
            </shard>       
        </tcfuture_cluster>

    </remote_servers>

#添加zookeeper集群连接
    <zookeeper>
        <node index="1">
            <host>ch01</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>ch02</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>ch03</host>
            <port>2181</port>
        </node>
        <node index="4">
            <host>ch04</host>
            <port>2181</port>
        </node>
    </zookeeper>

#定义副本数据集
##replica 配置当前节点的备份同步节点信息(一般就是填当前节点)
##shard 指定的是集群分片信息中的配置(每组副本集统一配置一个id)
##layer 指定我们的集群标志,或者使用cluster关键字
    <macros>
        <shard>01</shard>
        <layer>01</layer>
        <replica>ch01</replica>
    </macros>

启动服务并设为开机启动

systemctl start clickhouse-server
systemctl enable clickhouse-server

常规操作

#登录节点:
 clickhouse-client --host ch01 --port 9000 -u default --password P@ssw0rd

#查看集群状态
select * from system.clusters;

#默认情况下,CREATE、DROP、ALTER和RENAME查询仅影响执行它们的当前服务器。 在集群设置中,可以使用ON CLUSTER子句以分布式方式运行此类查询。

#查询clickhouse集群信息
select * from system.clusters;

#创建数据库命令(一个节点上执行,多个节点同时创建)
create database tc ON CLUSTER tcfuture_cluster;

#删除数据库命令(一个节点上执行,多个节点同时删除)
drop database test ON CLUSTER tcfuture_cluster

#删除本地表数据(分布式表无法删除表数据)
alter table test.events_local ON CLUSTER tcfuture_cluster delete where 1=1;
#1=1表示删除所有数据,可以接字段名删除满足某个条件的数据

#查看zookeeper下目录
select * from system.zookeeper WHERE path='/'

#先创建本地的复制表(数据只保存在一个分片组上)
##/clickhouse/tables/{shard}/table_name1 表示zook的位置
CREATE TABLE tc.table_name1 ON CLUSTER tcfuture_cluster (EventDate DateTime,CounterID UInt32,UserID UInt32) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/table_name1', '{replica}') PARTITION BY toYYYYMM(EventDate) ORDER BY (CounterID, EventDate, intHash32(UserID)) SAMPLE BY intHash32(UserID)

#然后创建分布式视图表(汇总所有分片组的数据并不保存实际数据,数据还是保存到本地库上)
## (tcfuture_cluster, tc, table_name1,rand());{集群名,库名,本地表,索引方式}
 CREATE TABLE tc.table ON CLUSTER tcfuture_cluster (EventDate DateTime,CounterID UInt32,UserID UInt32) ENGINE = Distributed(tcfuture_cluster, tc, table_name1,rand());

#查看数据所在节点
SELECT hostname(),* from tc.dalu1

#删除分布式的表
DROP table tc.home ON CLUSTER tcfuture_cluster;
DROP table db.本地表 ON CLUSTER cluster_name;



分布式负载接入可通过nginx反代

upstream clickhouse{
        server 192.168.109.121:8123;
        server 192.168.109.123:8123;
}
server {
	listen 8123;
	location / {
		proxy_pass http://clickhouse/;
	}
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐