关于数据库:ClickHouse安装及集群搭建

本文介绍如何在CentOS零碎上装置ClickHouse以及集群部署。

本文依赖的环境为CentOS-7.4.1708, 应用clickhouse的版本为20.9.3.45

单节点装置

在线yum装置

clickhouse的安装包并不在Linux官网yum仓库中,所以首先须要增加clickhouseyum镜像:

curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo bash

查看镜像状况:

[root@master ~]# yum list | grep clickhouse
clickhouse-client.x86_64                 20.8.3.18-1.el7               Altinity_clickhouse
clickhouse-common-static.x86_64          20.8.3.18-1.el7               Altinity_clickhouse
clickhouse-debuginfo.x86_64              20.1.11.73-1.el7              Altinity_clickhouse
clickhouse-odbc.x86_64                   1.1.9-1.el7                   Altinity_clickhouse
clickhouse-server.x86_64                 20.8.3.18-1.el7               Altinity_clickhouse
clickhouse-server-common.x86_64          20.8.3.18-1.el7               Altinity_clickhouse
clickhouse-test.x86_64                   20.8.3.18-1.el7               Altinity_clickhouse
clicktail.x86_64                         1.0.20180401-1                Altinity_clickhouse

由此可见,应用yum在线装置的版本最新到20.8.3.18。然而实际上clickhouse曾经迭代了很多版本,所以咱们不采纳此种装置形式,而采纳rpm包的装置形式。

PS: 有了下面的信息,yum形式装置clickhouse就很简略了,间接 yum install -y clickhouse-server-common.x86_64 clickhouse-server.x86_64 clickhouse-client.x86_64装置即可,此处就不演示了。

rpm包离线装置

咱们能够从官网rpm镜像仓库找到各种版本的rpm安装包,官网地址是:https://repo.yandex.ru/clickh… 。

咱们抉择 20.9.3.45 版本的rpm包,次要须要以下三个包:

clickhouse-client-20.9.3.45-2.noarch.rpm
clickhouse-server-20.9.3.45-2.noarch.rpm
clickhouse-common-static-20.9.3.45-2.x86_64.rpm

装置命令如下:

rpm -ivh clickhouse-common-static-20.9.3.45-2.x86_64.rpm clickhouse-client-20.9.3.45-2.noarch.rpm clickhouse-server-20.9.3.45-2.noarch.rpm

如下图所示,咱们即在node5这台机器上装置好了clickhouse

[root@node5 ck]# rpm -ivh clickhouse-common-static-20.9.3.45-2.x86_64.rpm clickhouse-client-20.9.3.45-2.noarch.rpm clickhouse-server-20.9.3.45-2.noarch.rpm
正告:clickhouse-common-static-20.9.3.45-2.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID e0c56bd4: NOKEY
筹备中...                          ################################# [100%]
正在降级/装置...
   1:clickhouse-common-static-20.9.3.4################################# [ 33%]
   2:clickhouse-client-20.9.3.45-2    ################################# [ 67%]
   3:clickhouse-server-20.9.3.45-2    ################################# [100%]
Path to data directory in /etc/clickhouse-server/config.xml: /var/lib/clickhouse/
[root@node5 ck]# 

验证是否装置胜利

如果以上步骤没有报错,则能够通过如下命令开启clickhouse的服务:

systemctl start clickhouse-server

而后通过clickhouse-client客户端去连贯:

[root@node5 ck]# clickhouse-client 
ClickHouse client version 20.9.3.45 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.9.3 revision 54439.

node5 :) select 1

SELECT 1

┌─1─┐
│ 1 │
└───┘

1 rows in set. Elapsed: 0.003 sec. 

node5 :) 

如果呈现上述界面,则阐明单机装置clickhouse胜利。

clickhouse-client CLI工具阐明

在下面,咱们用到了clickhouse-client这样一款工具。

clickhouse-client是一个带命令行的客户端工具,它通过TCP9000端口连贯clickhouse-server,能够在该命令行工具里进行各种操作。

通过 clickhouse-client --help能够查看工具的帮忙文档,能够看到该工具反对很多参数,此处简要说一下一些罕用的参数。

参数 用法举例 阐明 备注
-h [ --host ] -h 192.168.0.1
--host=192.168.0.1
指定连贯clickhouse服务的host地址,个别是近程连贯其余机器上的clickhouse服务。 clickhouse要想连贯近程服务器,须要将配置文件中<listen_host>::</listen_host>选项开启。
--port --port=9000 指定近程连贯clickhouse服务的端口号,默认为9000 这个端口号可通过配置文件配置:<tcp_port>9000</tcp_port>
-u [ --user ] -u ck
--user=ck
指定登录clickhouse的用户,默认是default default用户是内置的用户。能够通过users.xml配置文件自定义用户信息。
--password --password=123456 指定登录clickhouse的明码 default用户是没有明码的。明码可配置明文明码和加密明码
-q [ --query ] -q "show databases" 通过命令行间接输出sql,即时返回,而不须要进入客户端外部
-m [ --multiline ] 在客户端内反对多行语句操作,以分号作为结束符 如果不指定-m参数,每句sql语法后可加分号,也可不加,默认一行就是一个语句
-n [ --multiquery ] 命令行间接输出语句时反对多条sql语句,个别和-q联合应用 留神-n的地位不能在-qsql语句之间
-f [ --format ] -f JSON 设置输入的显示格局 反对JSONCSVTSV等,详见https://clickhouse.tech/docs/…

利用CLI工具导入数据

假如咱们在default数据库下有一张test表,在/data01目录下有一个file.csv的文件,可通过如下形式将数据导入到test表中。

clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV" < /data01/file.csv

配置文件阐明

config.xml

config.xml文件是clickhouse默认的配置文件,门路为/etc/clickhouse-server/config.xml

config.xml中能够配置的内容有很多,上面节选一些比拟重要的配置项来阐明一下:

<?xml version="1.0"?>
<yandex>
    <logger>
        <!-- clickhouse日志相干的配置 -->
        <level>trace</level>
        <log>/var/log/clickhouse-server/clickhouse-server.log</log> <!--日志门路-->
        <errorlog>/var/log/clickhouse-server/clickhouse-server.err.log</errorlog>
        <size>1000M</size>
        <count>10</count>
    </logger>
    
    <http_port>8123</http_port>   <!--http 客户端连贯ck的端口-->
    <tcp_port>9000</tcp_port>     <!--tcp 客户端连贯ck的端口 -->
    <mysql_port>9004</mysql_port> 

    <listen_host>::</listen_host>  <!--ip地址,配置成::能够被任意ipv4和ipv6的客户端连贯-->
    
    
    <path>/var/lib/clickhouse/</path>  <!--ck寄存数据库内容的门路-->
    
    <user_files_path>/var/lib/clickhouse/user_files/</user_files_path>  <!--File 引擎文件寄存的门路,个别可设置为/,这样就能够指定绝对路径-->
    
    <users_config>users.xml</users_config> <!--指定用户配置文件-->
    
    <default_database>default</default_database>  <!--指定默认数据库-->
    
    <!--配置prometheus信息-->
    <prometheus>     
        <endpoint>/metrics</endpoint>
        <port>9363</port>
        <metrics>true</metrics>
        <events>true</events>
        <asynchronous_metrics>true</asynchronous_metrics>
        <status_info>true</status_info>
    </prometheus>
    
    <include_from>/etc/clickhouse-server/metrika.xml</include_from>  <!--指定metrika.xml配置文件-->

users.xml

<users>
    <!--用户名为ck-->
    <ck>    
        <password>123456</password>  <!--明文明码-->
        <!--
        SHA256加密明码,可通过上面的命令生成:
        PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
        <password_sha256_hex>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</password_sha256_hex>
        SHA1加密形式加密的明码,可通过上面的命令生成
        PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-'
        <password_double_sha1_hex>7c4a8d09ca3762af61e59520943dc26494f8941b</password_double_sha1_hex>
        -->
        <networks>
              <ip>::/0</ip>
              </networks>
        <profile>default</profile>
        <quota>default</quota>
    </ck>
</users>

metrika.xml

metrika.xml 次要是用来服务于集群搭建的。它外面是对于zookeepershard以及replica的配置,比较复杂,将在部署集群中具体阐明。

部署集群

Clickhouse 的集群是依赖于zookeeper的。所以在搭建clickhouse集群之前,首先要有一个zookeeper集群。

对于zookeeper集群的搭建,此处不再开展详述,如有不分明的请自行百度。(本文默认zookeeper集群曾经搭建好)

后面说过,clickhouse集群的相干配置都在metrika.xml配置文件里,所以首先咱们须要配置好metrika.xmlmetrika.xml的门路能够在config.xml中通过<include_from>指定。

zookeeper配置

zookeeper 不肯定要和ck的节点装置在同一台机器上,只有ck的几点可能拜访zk即可。

<zookeeper-servers>
    <node index="1">
      <host>192.168.0.1</host>
      <port>2181</port>
    </node>
    <node index="2">
      <host>192.168.0.2</host>
      <port>2181</port>
    </node>
    <node index="3">
      <host>192.168.0.3</host>
      <port>2181</port>
    </node>
  </zookeeper-servers>

Shard和Replica设置

如下图所示,为一个残缺的shardReplica的配置,以下为test集群配置2分片2正本。

<clickhouse_remote_servers>
    <test>      <!--集群的名字-->
      <shard>   <!--shard分片的配置-->
        <internal_replication>true</internal_replication>  <!--是否只将数据写入其中一个正本,默认为false-->
        <replica>   <!--shard正本的配置-->
          <host>192.168.0.1</host>
          <port>9000</port>
        </replica>
        <replica>
          <host>192.168.0.2</host>
          <port>9000</port>
        </replica>
      </shard>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
          <host>192.168.0.3</host>
          <port>9000</port>
        </replica>
        <replica>
          <host>192.168.0.4</host>
          <port>9000</port>
        </replica>
      </shard>
    </test>
  </clickhouse_remote_servers>

一个残缺的metrika.xml如下所示:

<yandex>
    <zookeeper-servers>
    <node index="1">
      <host>192.168.0.1</host>
      <port>2181</port>
    </node>
    <node index="2">
      <host>192.168.0.2</host>
      <port>2181</port>
    </node>
    <node index="3">
      <host>192.168.0.3</host>
      <port>2181</port>
    </node>
  </zookeeper-servers>
  <clickhouse_remote_servers>
    <test>      <!--集群的名字-->
      <shard>   <!--shard分片的配置-->
        <internal_replication>true</internal_replication>  <!--是否只将数据写入其中一个正本,默认为false-->
        <replica>   <!--replica正本的配置-->
          <host>192.168.0.1</host>
          <port>9000</port>
        </replica>
        <replica>
          <host>192.168.0.2</host>
          <port>9000</port>
        </replica>
      </shard>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
          <host>192.168.0.3</host>
          <port>9000</port>
        </replica>
        <replica>
          <host>192.168.0.4</host>
          <port>9000</port>
        </replica>
      </shard>
    </test>
  </clickhouse_remote_servers>
</yandex>

集群搭建

首先依照单节点装置的形式,在node1node2node3node4四个节点上装置好clickhouse

批改config.xml,次要批改内容如下:

<!--1. 减少listen_host,容许近程拜访-->
<listen_host>0.0.0.0</listen_host>   
<!--
这里listen_host到底是配置::还是0.0.0.0是有说法的。
官网举荐的是::,然而在某些不反对IPV6的机器上并不能work,这时候就要配置成0.0.0.0。
我集体比拟喜爱配置成0.0.0.0,因为不论是IPV4还是IPV6,0.0.0.0都能够反对
-->

<!--2. 批改path门路,指定数据寄存地位,我这里指定/data01/clickhouse目录-->
<!-- 须要留神的是,这个门路必须提前存在,且clickhouse用户有权限拜访 -->
<path>/data01/clickhouse</path>
<tmp_path>/data01/clickhouse/tmp/</tmp_path>
<user_files_path>/data01/clickhouse/user_files/</user_files_path>
<access_control_path>/data01/clickhouse/access/</access_control_path>

<!--3.指定metrika.xml文件-->
<include_from>/etc/clickhouse-server/metrika.xml</include_from>


<!--4. 重新制定tcp_port -->
<tcp_port>19000</tcp_port>
<!--这一步不是必须的,我这里之所以重新制定,是因为发现我的机器上9000端口曾经被占用了,个别状况下,默认的9000即可-->

批改users.xml,咱们减少一个用户,专门用来治理cluster

<!--在<users>外面新增如下内容,创立一个用户名为ck,明码为123456的用户-->
        <ck>
            <password>123456</password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </ck>

创立metrika.xml文件。

在/etc/clickhouse-server门路下创立metrika.xml文件(这个门路为下面config.xml中配置的include_from的门路):

<yandex>
    <zookeeper-servers>
    <node index="1">
      <host>192.168.0.1</host>
      <port>2181</port>
    </node>
    <node index="2">
      <host>192.168.0.2</host>
      <port>2181</port>
    </node>
    <node index="3">
      <host>192.168.0.3</host>
      <port>2181</port>
    </node>
  </zookeeper-servers>
  <clickhouse_remote_servers>
    <test>      
      <shard>   
        <replica>  
          <host>192.168.0.1</host>
          <port>19000</port>    <!--这里的端口号要和config.xml中tcp_port保持一致,如果那里改了,这里也要对应的改-->
        </replica>
        <replica>
          <host>192.168.0.2</host>
          <port>19000</port>
        </replica>
      </shard>
      <shard>
        <replica>
          <host>192.168.0.3</host>
          <port>19000</port>
        </replica>
        <replica>
          <host>192.168.0.4</host>
          <port>19000</port>
        </replica>
      </shard>
    </test>
  </clickhouse_remote_servers>
</yandex>

以上配置在四台机器上同步,而后重启clickhouse

systemctl restart clickhouse-server

如果启动胜利,就能看到clickhouse-server的过程曾经在运行了:

[root@node2 test]# ps -ef |grep clickhouse
clickho+ 17548     1  1 11:11 ?        00:00:00 /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid
root     17738 10683  0 11:11 pts/0    00:00:00 grep --color=auto clickhouse

咱们应用clickhouse-client工具,登录到其中一台机器:

[root@node4 test]# clickhouse-client -u ck --password=123456 --port=19000 -m
ClickHouse client version 20.9.3.45 (official build).
Connecting to localhost:19000 as user ck.
Connected to ClickHouse server version 20.9.3 revision 54439.

node4 :) 

查问system.clusters表:

node4 :) select * from system.clusters where cluster = 'test';

SELECT *
FROM system.clusters
WHERE cluster = 'test'

┌─cluster─┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name─────┬─host_address──┬──port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─estimated_recovery_time─┐
│ test    │         1 │            1 │           1 │ 192.168.0.1 │ 192.168.0.1 │ 19000 │        0 │ default │                  │            0 │                       0 │
│ test    │         1 │            1 │           2 │ 192.168.0.2 │ 192.168.0.2 │ 19000 │        0 │ default │                  │            0 │                       0 │
│ test    │         2 │            1 │           1 │ 192.168.0.3 │ 192.168.0.3 │ 19000 │        0 │ default │                  │            0 │                       0 │
│ test    │         2 │            1 │           2 │ 192.168.0.4 │ 192.168.0.4 │ 19000 │        1 │ default │                  │            0 │                       0 │
└─────────┴───────────┴──────────────┴─────────────┴───────────────┴───────────────┴───────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────────────┘

4 rows in set. Elapsed: 0.002 sec.

呈现下面所示的后果,阐明配置集群胜利。

分布式表

接下来就能够在集群里创立分布式表了。

首先在集群的各个节点创立本地表。

CREATE TABLE t_cluster ON CLUSTER test (
id Int16,
name String,
birth Date
)ENGINE = MergeTree()
PARTITION BY toYYYYMM(birth)
ORDER BY id;

登录到任意节点,执行以上sql,呈现如下提醒,阐明执行胜利:

node4 :) CREATE TABLE default.t_cluster ON CLUSTER test ( id Int16, name String, birth Date )ENGINE = MergeTree() PARTITION BY toYYYYMM(birth) ORDER BY id;

CREATE TABLE default.t_cluster ON CLUSTER test
(
    `id` Int16,
    `name` String,
    `birth` Date
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(birth)
ORDER BY id

┌─host──────────┬──port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ 192.168.0.1 │ 19000 │      0 │       │                   3 │                0 │
│ 192.168.0.2 │ 19000 │      0 │       │                   2 │                0 │
│ 192.168.0.3 │ 19000 │      0 │       │                   1 │                0 │
│ 192.168.0.4 │ 19000 │      0 │       │                   0 │                0 │
└───────────────┴───────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.108 sec. 

咱们能够登录任意一台机器,都能查问到t_cluster表,这就阐明曾经在test集群上所有节点创立了一个t_cluster本地表。

这个时候如果往t_cluster表插入数据,依然是在所在节点的本地表中操作,在其余集群中是无奈看见的。

如咱们在node1节点插入如下数据:

node1 :) insert into t_cluster values(1, 'aa', '2021-02-01'), (2, 'bb', '2021-02-02');

INSERT INTO t_cluster VALUES

Ok.

2 rows in set. Elapsed: 0.002 sec.

node1节点查问:

node1 :) select * from t_cluster;

SELECT *
FROM t_cluster

┌─id─┬─name─┬──────birth─┐
│  1 │ aa   │ 2021-02-01 │
│  2 │ bb   │ 2021-02-02 │
└────┴──────┴────────────┘

2 rows in set. Elapsed: 0.002 sec. 

node2节点查问:

node2 :) select * from t_cluster;

SELECT *
FROM t_cluster

Ok.

0 rows in set. Elapsed: 0.002 sec.

node3node4查问也是如此。

这就充分说明了如果间接操作t_cluster表,操作的数据其实只是以后节点的数据。有人可能会有纳闷,我不是明明指定了node1node2互为replica吗?为什么node1的数据也没有同步到node2下面去?

这是因为咱们的引擎指定的是MergerTree,而不是ReplicaMergeTree,如果指定的是ReplicaMergeTree,确实是会同步到对应的replica下面去的,但这里创立的仅仅是MergeTree,这个引擎自身不具备同步正本的性能,所以node2上并没有同步数据。

个别在理论利用中,创立分布式表指定的都是Replica的表,这里仅仅是做一个例子阐明。

那么既然如此,这个集群中的表数据不能相互关联起来,不就失去了集群的意义了吗?

为了解决下面的问题,从而引入了分布式表。

分布式表的创立是这样的:

CREATE TABLE default.dist_t_cluster ON CLUSTER test as t_cluster engine = Distributed(test, default, t_cluster,rand());

Distributed是一个非凡的引擎,用来创立分布式表。它自身具备四个参数,第一个参数示意集群的名字,第二个参数为数据库的名字,第三个参数为表的名字,第四个参数为sharding key,它决定最终数据落在哪一个分片上,最初一个参数能够省略,如果省略,则实用配置文件中的weight分片权重。

下面的语句就是创立一张名为dist_t_cluster的分布式表,该分布式表是基于test集群中的default.t_cluster表。as t_cluster示意这张表的构造和t_cluster截然不同。

分布式表自身不存储数据,数据存储其实还是由本地表t_cluster实现的。这个dist_t_cluster仅仅做一个代理的作用。

如下所示,示意分布式表创立胜利。

node1 :) CREATE TABLE default.dist_t_cluster ON CLUSTER test as t_cluster engine = Distributed(test, default, t_cluster, rand());

CREATE TABLE default.dist_t_cluster ON CLUSTER test AS t_cluster
ENGINE = Distributed(test, default, t_cluster, rand())

┌─host──────────┬──port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ 192.168.0.1 │ 19000 │      0 │       │                   3 │                0 │
│ 192.168.0.2 │ 19000 │      0 │       │                   2 │                0 │
│ 192.168.0.3 │ 19000 │      0 │       │                   1 │                0 │
│ 192.168.0.4 │ 19000 │      0 │       │                   0 │                0 │
└───────────────┴───────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.106 sec.

接下来咱们应用分布式表向集群中插入数据,察看景象:

node1 :) insert into dist_t_cluster values(1, 'aaa', '2021-02-01'), (2, 'bbb', '2021-02-02');

INSERT INTO dist_t_cluster VALUES

Ok.

2 rows in set. Elapsed: 0.002 sec. 

这时候咱们在不同节点上查问dist_t_cluster表,失去的后果都是截然不同的:

/* node1 查问后果 */
node1 :) select * from dist_t_cluster;

SELECT *
FROM dist_t_cluster

┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘
┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘

2 rows in set. Elapsed: 0.005 sec. 

/* node2 查问后果 */
node2 :) select * from dist_t_cluster;

SELECT *
FROM dist_t_cluster

┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘
┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘

2 rows in set. Elapsed: 0.005 sec.

/* node3 查问后果 */
node3 :) select * from dist_t_cluster;

SELECT *
FROM dist_t_cluster

┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘
┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘

2 rows in set. Elapsed: 0.006 sec. 

/* node4 查问后果 */
node4 :) select * from dist_t_cluster;

SELECT *
FROM dist_t_cluster

┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘
┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘

2 rows in set. Elapsed: 0.005 sec.

咱们再去看各个节点的本地表t_cluster表中数据分布状况:

/* node1 查问后果 */
node1 :) select * from t_cluster;

SELECT *
FROM t_cluster

┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘

1 rows in set. Elapsed: 0.002 sec.

/* node2 查问后果 */
node2 :) select * from t_cluster;

SELECT *
FROM t_cluster

┌─id─┬─name─┬──────birth─┐
│  2 │ bbb  │ 2021-02-02 │
└────┴──────┴────────────┘

1 rows in set. Elapsed: 0.002 sec. 

/* node3 查问后果 */
node3 :) select * from t_cluster;

SELECT *
FROM t_cluster

┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘

1 rows in set. Elapsed: 0.002 sec.

/* node4 查问后果 */
node4 :) select * from t_cluster;

SELECT *
FROM t_cluster

┌─id─┬─name─┬──────birth─┐
│  1 │ aaa  │ 2021-02-01 │
└────┴──────┴────────────┘

1 rows in set. Elapsed: 0.002 sec. 

由以上后果可知,node1node2下面存储的数据一样,node3node4下面存储的数据一样,然而node1node3下面的数据不一样。node1node2)和node3node4)的数据组成了dist_t_cluster表中的所有数据。

这是因为node1node2node3node4互为正本,所以它们的数据会主动同步。而node1node2)和node3node4)属于不同的分片,所以数据依照肯定的规定(sharding key)落在了不同分片。

如果咱们关上存储数据的目录,能够看到dist_t_cluster表中仅仅有一个长期文件夹,并没有理论存储数据,因为数据是存储在t_cluster本地表中的。

[root@node1 chenyc]# ls -l  /data01/clickhouse/data/default/dist_t_cluster/
总用量 0
drwxr-x--- 3 clickhouse clickhouse 17 2月  25 13:57 default@192%2E168%2E21%2E52:19000
drwxr-x--- 3 clickhouse clickhouse 17 2月  25 13:57 default@192%2E168%2E21%2E53:19000
drwxr-x--- 2 clickhouse clickhouse  6 2月  25 13:57 default@192%2E168%2E21%2E54:19000

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理