安装

1
2
3
4
5
docker run -d --name clickhouse-server \\
-p 8123:8123 -p 9000:9000 \\
-v <CONFIG_PATH>:/etc/clickhouse-server/ \\
-v <DATA_PATH>:/var/lib/clickhouse/
yandex/clickhouse-server

拉取并将Clickhouse的8123端口, 9000端口映射到本机.

Clickhouse的默认配置文件路径为/etc/clickhouse-server/ , 默认文件存储位置为/var/lib/clickhouse/

MySQL连接Clickhouse

Clickhouse默认开放了9004端口, mysql client可以通过这个端口查询Clickhouse的数据.

开启Tabix

在配置文件中, 将Tabix的注释打开.

1
2
<!-- 这里默认被注释掉, 打开后即可使用Tabix来进行查询 -->
<http_server_default_response><![CDATA[<html ng-app="SMI2"><head><base href="<http://ui.tabix.io/>"></head><body><div ui-view="" class="content-ui"></div><script src="<http://loader.tabix.io/master.js>"></script></body></html>]]></http_server_default_response>

设置分布式表

由于Clickhouse的分布式表依赖zookeeper, 所以需要先配置zookeeper.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<zookeeper-servers>  
<node index="1">
<host>10.0.55.17</host>
<port>2181</port>
</node>
<node index="2">
<host>10.0.10.23</host>
<port>2181</port>
</node>
<node index="3">
<host>10.0.55.16</host>
<port>2181</port>
</node>
</zookeeper-servers>

集群配置

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<remote_servers>
<clusterA>
<shard>
<!-- Optional. Shard weight when writing data. Default: 1. -->
<weight>1</weight>
<!-- Optional. Whether to write data to just one of the replicas. Default: false (write data to all replicas). -->
<internal_replication>false</internal_replication>
<replica>
<host>ip001</host>
<port>9000</port>
</replica>
<replica>
<host>ip002</host>
<port>9000</port>
</replica>
</shard>
<shard>
<weight>2</weight>
<internal_replication>false</internal_replication>
<replica>
<host>ip003</host>
<port>9000</port>
</replica>
<replica>
<host>ip004</host>
<port>9440</port>
</replica>
</shard>
</clusterA>

<clusterB>
<shard>
<weight>1</weight>
<internal_replication>false</internal_replication>
<replica>
<host>ip001</host>
<port>9000</port>
</replica>
</shard>
<shard>
<weight>1</weight>
<internal_replication>false</internal_replication>
<replica>
<host>ip002</host>
<port>9000</port>
</replica>
</shard>
<shard>
<weight>1</weight>
<internal_replication>false</internal_replication>
<replica>
<host>ip003</host>
<port>9000</port>
</replica>
</shard>
<shard>
<weight>1</weight>
<internal_replication>false</internal_replication>
<replica>
<host>ip004</host>
<port>9000</port>
</replica>
</shard>
</clusterB>
</remote_servers>

配置的意思:

创建一个名称为clusterAclusterB 两个集群, 公用ip001, ip002, ip003, ip004这4台机器.

clusterA中, 有2个分片, 每个分片都有一个备份.

  • 分片1: 权重为1, 总权重为3 (1+2), 也就是每3条记录会有1条写入这个分片. 这个分片下有机器ip001, ip002.
  • 分片2: 权重为2, 每3条记录会有2条记录写入这个分片. 这个分片下有机器ip003, ip004.

在clusterB中, 有4个分片, 每个分片没有备份.

  • 分片1: 权重为1, 机器为ip001
  • 分片2: 权重为1, 机器为ip002
  • 分片3: 权重为1, 机器为ip003
  • 分片4: 权重为1, 机器为ip004

从这个配置中我们可以看出, 我们可以在相同的机器上配置多个集群

这个配置我们也可以从主配置文件中抽离, 放到单独的配置文件中.

1
2
3
4
<!-- config.xml 里 -->
...
<remote_servers incl="clickhouse_remote_servers" />
...

这句的意思是去读取clickhouse_remote_servers这个节点下的信息, 所以新的集群配置必须在这个节点下. 新建的集群配置文件:

1
2
3
4
5
6
7
8
9
10
<yandex>
<clickhouse_remote_servers>
<clusterA>
...
</clusterA>
<clusterB>
...
</clusterB>
</clickhouse_remote_servers>
</yandex>

分布式表的创建语句为:

1
2
3
4
5
6
7
CREATE TABLE [IF NOT EXISTS] [db.]table_name ON CLUSTER clusterName
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) ENGINE = Distributed(clusterName, database, table, sharding_key)
[SETTINGS name=value, ...]

这个语句将在集群clusterName上创建一个表, 并根据sharding_key做数据分片

其他配置

主配置文件为: /etc/clickhouse-server/config.xml

  • 数据存放
    • path — 文件存放位置
    • tmp_path — 查询临时结果存放位置. 默认为/var/lib/clickhouse/tmp/
  • 端口
    • http_port
    • tcp_port
    • mysql_port — mysql协议的端口
    • interserver_http_port — 内部数据交互端口, 副本之间通过这个端口进行通信和数据交换
  • 连接
    • max_connections
    • max_concurrent_queries — MergeTree引擎同时处理的请求最大数量
  • 用户与权限
    • user_directories/users_xml — Clickhouse默认的用户配置在users.xml中, 可以直接在这个配置文件上进行改动, 或者在users.d文件夹下创建其他用户专属的xml配置文件, 具体设置项很多, 具体可以查看 https://clickhouse.com/docs/en/operations/access-rights/