clickhouse搭建单机版和集群版本

 

官网地址:https://clickhouse.com/docs/zh/install/redhat

本次部署版本是25.7.8.71,github下载地址https://github.com/ClickHouse/ClickHouse/releases

一、单机版

采用tgz方式部署

cd /opt/clickhouse

1、下载clickhouse-common-static包,然后解压运行脚本

wget https://github.com/ClickHouse/ClickHouse/releases/download/v25.7.8.71-stable/clickhouse-common-static-25.7.8.71-amd64.tgz
tar zxvf clickhouse-common-static-25.7.8.71-amd64.tgz
clickhouse-common-static-25.7.8.71/install/doinst.sh

2、下载clickhouse-common-static-dbg包,然后解压运行脚本

wget https://github.com/ClickHouse/ClickHouse/releases/download/v25.7.8.71-stable/clickhouse-common-static-dbg-25.7.8.71-amd64.tgz
tar zxvf clickhouse-common-static-dbg-25.7.8.71-amd64.tgz
clickhouse-common-static-dbg-25.7.8.71/install/doinst.sh

3、下载clickhouse-server包,然后解压运行脚本,然后会提示输入默认账户default的密码,这里我输入密码是sykj1234

wget https://github.com/ClickHouse/ClickHouse/releases/download/v25.7.8.71-stable/clickhouse-server-25.7.8.71-amd64.tgz
tar zxvf clickhouse-server-25.7.8.71-amd64.tgz
clickhouse-server-25.7.8.71/install/doinst.sh

4、下载clickhouse-client包,然后解压运行脚本

wget https://github.com/ClickHouse/ClickHouse/releases/download/v25.7.8.71-stable/clickhouse-client-25.7.8.71-amd64.tgz
tar zxvf clickhouse-client-25.7.8.71-amd64.tgzclickhouse-client-25.7.8.71-amd64.tgz
clickhouse-client-25.7.8.71/install/doinst.sh

5、修改配置

vim /etc/clickhouse-server/config.xml
<!-- 找到<listen_host>配置修改监听IP -->
    <listen_host>0.0.0.0</listen_host>

6、启动clickhouse-server

systemctl start clickhouse-server

 

二、集群版

本次部署为3节点3分片1副本,使用clickhouse-keeper代替zookeeper,服务器信息如下

服务器ip server_id    
192.168.18.12 1    
192.168.18.13 2    
192.168.18.14 3    

 

 

 

 

1、各点节点按照单机版部署clickhouse

 

2、在各个节点配置clickhouse-keeper,主要是修改server_id、listen_host和raft_configuration配置

 vim /etc/clickhouse-keeper/keeper_config.xml
    # 添加listen_host配置
    <listen_host>0.0.0.0</listen_host>

    <keeper_server>
            <tcp_port>9181</tcp_port>

            <!-- Must be unique among all keeper serves -->
            <server_id>1</server_id>

            <log_storage_path>/var/lib/clickhouse/coordination/logs</log_storage_path>
            <snapshot_storage_path>/var/lib/clickhouse/coordination/snapshots</snapshot_storage_path>

            <coordination_settings>
                <operation_timeout_ms>10000</operation_timeout_ms>
                <min_session_timeout_ms>10000</min_session_timeout_ms>
                <session_timeout_ms>100000</session_timeout_ms>
                <raft_logs_level>information</raft_logs_level>
                <compress_logs>false</compress_logs>

            <!-- enable sanity hostname checks for cluster configuration (e.g. if localhost is used with remote endpoints) -->
            <hostname_checks_enabled>true</hostname_checks_enabled>
            <raft_configuration>
                <server>
                    <id>1</id>

                    <!-- Internal port and hostname -->
                    <hostname>192.168.18.12</hostname>
                    <port>9234</port>
                </server>

                <!-- Add more servers here -->
                <server>
                    <id>2</id>

                    <!-- Internal port and hostname -->
                    <hostname>192.168.18.13</hostname>
                    <port>9234</port>
                </server>

                <server>
                    <id>3</id>

                    <!-- Internal port and hostname -->
                    <hostname>192.168.18.14</hostname>
                    <port>9234</port>
                </server>


            </raft_configuration>
    </keeper_server>

 

3、在各个节点启动clickhouse-keeper

systemctl start clickhouse-keeper

 

4、在各个节点配置clickhouse-server

配置clickhouse-server指向clickhouse-keeper

vim /etc/clickhouse-server/config.xml 
#找到zookeeper字段并配置

    <zookeeper>
        <node>
            <host>192.168.18.12</host>
            <port>9181</port>
        </node>
        <node>
            <host>192.168.18.13</host>
            <port>9181</port>
        </node>
        <node>
            <host>192.168.18.14</host>
            <port>9181</port>
        </node>
    </zookeeper>

 

5、配置remote_servers,其中my_cluster是自定义的集群名称

    <remote_servers>
        <!-- Test only shard config for testing distributed storage -->
        <my_cluster>
            <!-- Inter-server per-cluster secret for Distributed queries
                 default: no secret (no authentication will be performed)

                 If set, then Distributed queries will be validated on shards, so at least:
                 - such cluster should exist on the shard,
                 - such cluster should have the same secret.

                 And also (and which is more important), the initial_user will
                 be used as current user for the query.

                 Right now the protocol is pretty simple, and it only takes into account:
                 - cluster name
                 - query

                 Also, it will be nice if the following will be implemented:
                 - source hostname (see interserver_http_host), but then it will depend on DNS,
                   it can use IP address instead, but then you need to get correct on the initiator node.
                 - target hostname / ip address (same notes as for source hostname)
                 - time-based security tokens
            -->
            <!-- <secret></secret> -->

            <shard>
                <!-- Optional. Whether to write data to just one of the replicas. Default: false (write data to all replicas). -->
                <!-- <internal_replication>false</internal_replication> -->
                <!-- Optional. Shard weight when writing data. Default: 1. -->
                <!-- <weight>1</weight> -->
                <replica>
                    <host>192.168.18.12</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>sykj1234</password>
                    <!-- Optional. Priority of the replica for load_balancing. Default: 1 (less value has more priority). -->
                    <!-- <priority>1</priority> -->
                    <!-- Use SSL? Default: no -->
                    <!-- <secure>0</secure> -->
                    <!-- Optional. Bind to specific host before connecting to use a specific network. -->
                    <!-- <bind_host>10.0.0.1</bind_host> -->
                </replica>

            </shard>
            <shard>
                <!-- Optional. Whether to write data to just one of the replicas. Default: false (write data to all replicas). -->
                <!-- <internal_replication>false</internal_replication> -->
                <!-- Optional. Shard weight when writing data. Default: 1. -->
                <!-- <weight>1</weight> -->
                <replica>
                    <host>192.168.18.13</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>sykj1234</password>

                    <!-- Optional. Priority of the replica for load_balancing. Default: 1 (less value has more priority). -->
                    <!-- <priority>1</priority> -->
                    <!-- Use SSL? Default: no -->
                    <!-- <secure>0</secure> -->
                    <!-- Optional. Bind to specific host before connecting to use a specific network. -->
                    <!-- <bind_host>10.0.0.1</bind_host> -->
                </replica>

            </shard>
            <shard>
                <!-- Optional. Whether to write data to just one of the replicas. Default: false (write data to all replicas). -->
                <!-- <internal_replication>false</internal_replication> -->
                <!-- Optional. Shard weight when writing data. Default: 1. -->
                <!-- <weight>1</weight> -->
                <replica>
                    <host>192.168.18.14</host>
                    <port>9000</port>
                    <user>default</user>
                    <password>sykj1234</password>

                   <!-- Optional. Priority of the replica for load_balancing. Default: 1 (less value has more priority). -->
                    <!-- <priority>1</priority> -->
                    <!-- Use SSL? Default: no -->
                    <!-- <secure>0</secure> -->
                    <!-- Optional. Bind to specific host before connecting to use a specific network. -->
                    <!-- <bind_host>10.0.0.1</bind_host> -->
                </replica>

            </shard>



        </my_cluster>
    </remote_servers>

 

6、在各个节点启动clickhouse-server

systemctl start clickhouse-server

 

7、验证集群

在其中3个节点中的任意一个上执行

登录clickhouse

clickhouse-client

查看集群消息

show clusters;
select * from system.cluster

 

8、插入数据验证分片

在其中3个节点中的任意一个上执行

创建数据库

create database ruby_db01 on cluster my_cluster

创建存储表

create table ruby_db01.c_mgtree_l on cluster my_cluster (
id Int,
label String,
dt Date
)engine = MergeTree()
partition by toYYYYMMDD(dt)
order by id

创建分布式表

CREATE TABLE ruby_db01.c_mgtree_c ON CLUSTER my_cluster
(
    `id` Int,
    `label` String,
    `dt` Date
)ENGINE = Distributed('my_cluster', 'ruby_db01', 'c_mgtree_l', rand())

插入数据

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (1, '苹果', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (2, '香蕉', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (3, '橙子', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (4, '葡萄', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (5, '西瓜', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_c (id, label, dt) VALUES (6, '哈密瓜', '2025-10-19 17:42:01')

INSERT INTO ruby_db01.c_mgtree_l (id, label, dt) VALUES (7, '番茄', '2025-10-19 17:42:01')

然后分别在3个节点上查看数据

#可以得到所有数据
select * from ruby_db01.c_mgtree_c

#只得到部分数据
select * from ruby_db01.c_mgtree_l

 

posted @ 2025-10-19 20:16  苦逼yw  阅读(31)  评论(0)    收藏  举报