Deploying ClickHouse - Stand-alone | 8lovelife's life
0%

Deploying ClickHouse - Stand-alone

ClickHouse 是由俄罗斯IT公司 Yandex 为 Yandex.Metrica 网络分析服务开发的一款面向列存储的高性能 OLAP 数据库,支持 SQL 查询,于2016年6月开源。本文记录如何使用 Docker 搭建 ClickHouse 的 Stand-alone 实例,以及常见操作

Modify Xml File

设置 default 登陆密码,并为其开启 SQL user mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# copy config.xml、users.xml to host path
docker cp $(docker create --name tc clickhouse/clickhouse-server:latest):/etc/clickhouse-server/config.xml . && docker rm tc
docker cp $(docker create --name tc clickhouse/clickhouse-server:latest):/etc/clickhouse-server/users.xml . && docker rm tc

# set password for 'default' user
# modify users.xml
<clickhouse>
<users>
<default>
<password>123456</password>
<access_management>1</access_management>
<named_collection_control>1</named_collection_control>
<show_named_collections>1</show_named_collections>
<show_named_collections_secrets>1</show_named_collections_secrets>
</default>
</users>
</clickhouse>

Up ClickHouse Server

通过 docker compose 创建 ClickHouse Server 实例

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
# docker-compose.yml contents
version: "3.6"

services:

clickhouse-server:
container_name: ck-test
image: clickhouse/clickhouse-server:latest
restart: always
ports:
- "18123:8123"
- "19000:9000"
- "19004:9004"
deploy:
resources:
limits:
cpus: "2"
memory: 1G
reservations:
cpus: "0.25"
memory: 128M
ulimits:
nofile:
soft: "262144"
hard: "262144"
volumes:
- ./config/config.d/config.xml:/etc/clickhouse-server/config.d/config.xml:rw
- ./config/users.d/users.xml:/etc/clickhouse-server/users.d/users.xml:rw
- ./data:/var/lib/clickhouse:rw
- ./logs:/var/log/clickhouse-server

# up server
docker compose -f "docker-compose.yml" up -d --build

# login clickhouse
docker exec -it ck-test clickhouse client --user default --password 123456

Create User

创建管理员用户

1
2
3
4
5
6
7
8
9
# role
CREATE ROLE admin_role;

# authorization
GRANT ALL ON *.* TO admin_role WITH GRANT OPTION;

# user
CREATE USER mark IDENTIFIED BY '123';
GRANT admin_role to mark;

Data Sample

数据的简单测试

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
# db
CREATE DATABASE dp;

# table
use dp;
CREATE TABLE random_data_max (
date String,
val UInt8,
timestamp Datetime64
)ENGINE=MergeTree()
PARTITION by toYYYYMMDD(timestamp)
ORDER BY timestamp;

# data insert
INSERT INTO random_data_max(date, val,timestamp) SELECT today(), number,now() FROM numbers(100);

# db infos | 压缩率信息等
select
database,
table,
formatReadableSize(size) as size,
formatReadableSize(bytes_on_disk) as bytes_on_disk,
formatReadableSize(data_uncompressed_bytes) as data_uncompressed_bytes,
formatReadableSize(data_compressed_bytes) as data_compressed_bytes,
compress_rate,
rows,
days,
formatReadableSize(avgDaySize) as avgDaySize
from
(
select
database,
table,
sum(bytes) as size,
sum(rows) as rows,
min(min_date) as min_date,
max(max_date) as max_date,
sum(bytes_on_disk) as bytes_on_disk,
sum(data_uncompressed_bytes) as data_uncompressed_bytes,
sum(data_compressed_bytes) as data_compressed_bytes,
(data_compressed_bytes / data_uncompressed_bytes) * 100 as compress_rate,
max_date - min_date as days,
size / (max_date - min_date) as avgDaySize
from system.parts
where active
and database = 'dp'
group by
database,
table
) FORMAT Vertical;

Mysql Client Connect

ClickHouse 支持 MySQL protocol,可以通过 MySQL Client 连接 ClickHouse Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
****@**** ~ % docker run -it --network=host --rm mysql mysql --protocol tcp -h 127.0.0.1 -u mark -P 19004 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 23.6.2.18-ClickHouse

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>