Centos安装ClickHouse21.7.3.14单机&集群&分片集群

[TOC]

Centos安装ClickHouse21.7.3.14单机&集群&分片集群

一、单机安装

1、删除旧的

  • 查找rpm包
1
2
rpm -qa|grep clickhouse
yum list installed|grep clickhouse
  • 删除
1
2
3
4
yum -y remove lickhouse-client-21.7.3.14-2.noarch
yum -y remove clickhouse-common-static-21.7.3.14-2.x86_64
yum -y remove clickhouse-common-static-dbg-21.7.3.14-2.x86_64
yum -y remove clickhouse-server-21.7.3.14-2.noarch

2、下载

ClickHouse_免费高速下载|百度网盘-分享无限制

1
2
3
4
lickhouse-client-21.7.3.14-2.noarch
clickhouse-common-static-21.7.3.14-2.x86_64
clickhouse-common-static-dbg-21.7.3.14-2.x86_64
clickhouse-server-21.7.3.14-2.noarch

3、准备

  • 关闭防火墙

    1
    2
    3
    systemctl status firewalld
    systemctl stop firewalld
    systemctl disable firewalld
  • 关闭 SELINUX

    1
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  • 增加打开文件数和进程数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 查看限制情况
    ulimit -a

    # 修改配置
    vim /etc/security/limits.conf
    * soft nofile 65536
    * hard nofile 65536
    * soft nproc 131072
    * hard nproc 131072

    vim /etc/security/limits.d/20-nproc.conf
    * soft nofile 65536
    * hard nofile 65536
    * soft nproc 131072
    * hard nproc 131072
  • 安装依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    yum list installed|grep libtool
    #-----------------------------------------------------------------------
    # libtool-ltdl.x86_64 2.4.2-22.el7_3 @anaconda
    #-----------------------------------------------------------------------
    yum install -y libtool

    yum list installed|grep unixODBC
    #-----------------------------------------------------------------------
    # freeradius-unixODBC.x86_64 3.0.13-15.el7 @base
    # unixODBC.x86_64 2.3.1-14.el7 @base
    # unixODBC-devel.x86_64 2.3.1-14.el7 @base
    #-----------------------------------------------------------------------
    yum install -y *unixODBC*

4、rpm 安装

1
2
3
4
# 修改目录
cd /opt/download/second/clickhouse
# 安装
rpm -ivh *.rpm

默认初始密码:直接回车可以没有密码

#———–目录映射—————

bin/ ==> /usr/bin/
conf/ ==> /etc/clickhouse-server/
lib/ ==> /var/lib/clickhouse/
log/ ==> /var/log/clickhouse/

1
2
3
4
5
6
7
8
# 1. 设置权限
chmod -R 777 /etc/clickhouse-server

# 2. 修改配置
vim /etc/clickhouse-server/config.xml
#------取消以下内容注释-------
<listen_host>::</listen_host>
#-------------------------------------

5、start & stop

1
2
3
4
5
clickhouse start | stop | restart
clickhouse status
# 访问:远程 -h host -p port -m 换行不提交,分号提交
clickhouse-client -m --passwords
# 默认端口 9000

6、权限管理

默认用户default默认关闭 access_managenment
开放权限管理 通过配置users.xml 将axxess_managenment 改为1,重启服务后生效

主要用于控制用户对数据库系统中各种资源的访问和操作权限。当它开启时,意味着用户可以进行诸如创建、修改和删除其他用户、角色,以及管理权限等操作,能够对数据库的访问控制体系进行全面的管理和调整。
default用户的access_management关闭时,若需要进行用户和权限管理操作,通常需要使用具有相应管理权限的其他用户账号登录,或者由管理员为default用户显式地授予相关权限后再进行操作。

1
vim /etc/clickhouse-server/users.xml

二、副本

1、概念、作用

副本的目的主要是保障数据的高可用性,即使一台 ClickHouse 节点宕机,那么也可以从其他服务器获得相同的数据。image-20250312143543203

2、配置

  1. 启动 zookeeper 集群

  2. 在每台机器下的/etc/clickhouse-server/config.d 目录下创建一个名为metrika.xml的配置文件, 内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0"?>
    <yandex>
    <zookeeper-servers>
    <node index="1">
    <host>node01</host>
    <port>2181</port>
    </node>
    <node index="2">
    <host>node02</host>
    <port>2181</port>
    </node>
    <node index="3">
    <host>node03</host>
    <port>2181</port>
    </node>
    </zookeeper-servers>
    </yandex>

    修改文件组和所属用户(尽量改一下)

    1
    chown clickhouse:clickhouse /etc/clickhouse-server/config.d/metrika.xml

    /etc/clickhouse-server/config.xml 中添加

    1
    2
    <zookeeper incl="zookeeper-servers" optional="true" />
    <include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
  3. 重启clickhouse

    1
    clickhouse restart

3、验证

clickhouse之间没有优先级互为副本

建表合并树家族 数据复制 | ClickHouse Docs

1
2
3
4
5
6
7
8
9
10
create table t_order_rep (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine = ReplicatedMergeTree('/clickhouse/table/01/t_order_rep','rep_101')
--第一个参数为zookeeper路径,第二个参数需要不一样
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
1
2
3
4
5
6
7
8
9
create table t_order_rep (
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =ReplicatedMergeTree('/clickhouse/table/01/t_order_rep','rep_102')
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);

在一台插入数据后自动同步其他机器,在其他机器也能查询到

1
2
3
4
5
6
insert into t_order_rep2 values
(101,'sku_001',1000.00,'2020-06-01 12:00:00'),
(102,'sku_002',2000.00,'2020-06-01 12:00:00'),
(103,'sku_004',2500.00,'2020-06-01 12:00:00'),
(104,'sku_002',2000.00,'2020-06-01 12:00:00'),
(105,'sku_003',600.00,'2020-06-02 12:00:00');

三、分片集群

1、概念、作用

  • 副本虽然能够提高数据的可用性,降低丢失风险,但是每台服务器实际上必须容纳全量数据,对数据的横向扩容没有解决。
  • 要解决数据水平切分的问题,需要引入分片的概念。通过分片把一份完整的数据进行切分,不同的分片分布到不同的节点上,再通过 Distributed 表引擎把数据拼接起来一同使用。
  • Distributed 表引擎本身不存储数据,有点类似于 MyCat 之于 MySql,成为一种中间件,通过分布式逻辑表来写入、分发、路由来操作多台节点不同分片的分布式数据。

2、配置

测试:集群及副本规划(2个分片,只有第一个分片有副本)

/etc/clickhouse-server/config.d目录下创建 metrika-shard.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<yandex>
<remote_servers>
<gmall_cluster> <!-- 集群名称-->
<shard> <!--集群的第一个分片-->
<!-- 内部副本同步 -->
<internal_replication>true</internal_replication>
<!--该分片的第一个副本-->
<replica>
<host>node01</host>
<port>9000</port>
</replica>
<!--该分片的第二个副本-->
<replica>
<host>node02</host>
<port>9000</port>
</replica>
</shard>
<shard> <!--集群的第二个分片-->
<internal_replication>true</internal_replication>
<replica> <!--该分片的第一个副本-->
<host>node03</host>
<port>9000</port>
</replica>
</shard>
</gmall_cluster>
</remote_servers>

<zookeeper-servers>
<node index="1">
<host>node01</host>
<port>2181</port>
</node>
<node index="2">
<host>node02</host>
<port>2181</port>
</node>
<node index="3">
<host>node03</host>
<port>2181</port>
</node>
</zookeeper-servers>
<!-- 为之后的建表起别名 -->
<macros>
<shard>01</shard> <!--不同机器放的分片数不一样-->
<replica>rep_1_1</replica> <!--不同机器放的副本数不一样(名字_分片数_副本)-->
</macros>
</yandex>

/etc/clickhouse-server/config.xml 中添加

1
2
<zookeeper incl="zookeeper-servers" optional="true"/>
<include_from>/etc/clickhouse-server/config.d/metrika-shard.xml</include_from>

修改文件组和所属用户(尽量改一下)

1
chown clickhouse:clickhouse /etc/clickhouse-server/config.d/metrika-shard.xml

3、测试

  • 会自动同步到其他机器上
  • 集群名字要和配置文件中的一致
  • 分片和副本名称从配置文件的宏定义中获取
  • 可以到 其他机器上查看表是否创建成功
1
2
3
4
5
6
7
8
9
create table st_order_mt on cluster gmall_cluster (  --gmall_cluster是上面配置的集群名称
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/st_order_mt','{replica}') -- {}引用配置的变量
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);

创建 Distribute 分布式表

1
2
3
4
5
6
7
8
create table st_order_mt_all2 on cluster gmall_cluster(
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
)engine = Distributed(gmall_cluster,default, st_order_mt,hiveHash(sku_id));
-- Distributed(集群名称,库名,本地表名,分片键)
-- 分片键必须是整型数字,所以用 hiveHash 函数转换,也可以 rand()

分布式表不存数据,往本地表写,本地同步

1
2
3
4
5
6
insert into st_order_mt_all2 values
(201,'sku_001',1000.00,'2020-06-01 12:00:00') ,
(202,'sku_002',2000.00,'2020-06-01 12:00:00'),
(203,'sku_004',2500.00,'2020-06-01 12:00:00'),
(204,'sku_002',2000.00,'2020-06-01 12:00:00'),
(205,'sku_003',600.00,'2020-06-02 12:00:00');

查询分布式表会查到全部数据,每台都一样

查询本地表会发现每台含部分数据起始1,2两台一样(配置的互为副本)

1
2
select * from st_order_mt_all2;
select * from st_order_mt;

Centos安装ClickHouse21.7.3.14单机&集群&分片集群
https://leaf-domain.gitee.io/2025/03/22/databases/clickhouse/clickhouse_1_install/
作者
叶域
发布于
2025年3月22日
许可协议