下载clickhouse安装包
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
在所有服务器进行上述操作
修改密码
cd /etc/clickhouse-server/
vi users.xml
<password>xxxxxx</password>
下边remote_servers里的password
修改配置文件
cd /etc/clickhouse-server/
vi config.xml
# 更改监听让所有正常ip可连接
<listen_host>0.0.0.0</listen_host>
# 配置 remote_server
# enic_cluster 这个自己命名就行,建库建表需要用到
<remote_servers>
<enic_cluster>
<shard>
<replica>
<host>ip1</host>
<port>9000</port>
<user>default</user>
<password>xxxxxx</password>
</replica>
</shard>
<shard>
<replica>
<host>ip2</host>
<port>9000</port>
<user>default</user>
<password>xxxxxx</password>
</replica>
</shard>
<shard>
<replica>
<host>ip3</host>
<port>9000</port>
<user>default</user>
<password>xxxxxx</password>
</replica>
</shard>
</enic_cluster>
</remote_servers>
# 配置zookeeper
<zookeeper>
<node index="1">
<host>ip1</host>
<port>2181</port>
</node>
<node index="2">
<host>ip2</host>
<port>2181</port>
</node>
<node index="3">
<host>ip3</host>
<port>2181</port>
</node>
</zookeeper>
# 如果需要数据副本需要用到macros,不需要可以忽略
# 第3分片的第1副本
<macros>
<layer>02</layer>
<shard>03</shard>
<replica>ip</replica>
</macros>
复制
/etc/clickhouse-server/
users.xml
config.xml
/etc/clickhouse-server/下这两个文件每个服务器复制一遍
启动clickhouse服务
# 启动
systemctl start clickhouse-server
# 停止
systemctl stop clickhouse-server
# 重启
systemctl restart clickhouse-server
# 查看状态
systemctl status clickhouse-server
用dbeaver连接测试
SELECT * FROM system.clusters WHERE cluster = 'enic_cluster'
创建本地表
# 创建工作空间
CREATE DATABASE test_database ON CLUSTER enic_cluster;
# 创建本地表
CREATE TABLE test_database.test_local ON CLUSTER enic_cluster (
id Int16,
name String,
birth Date
)ENGINE = MergeTree()
PARTITION BY toYYYYMM(birth)
ORDER BY id;
在每个clickhouse服务都会有test_database 工作空间,每个test_database 下都会有test_local 表
创建分布式表
CREATE TABLE test_database.test_all ON CLUSTER enic_cluster engine = Distributed(enic_cluster, test_database, test_local,rand());
Distributed(A.B.C,D)
A:remote_servers 里命名的
B:工作空间
C:表
当我们查询test_database.test_all时,会把3个数据库的数据全部展示出来
往test_database.test_all新增会随机往三个数据库的test_database.test_local表存放数据
这个在dbeaver自己查看吧