tonglin0325的个人主页

ubuntu16.04安装clickhouse

1.安装步骤参考官方文档

1
2
https://clickhouse.com/docs/zh/getting-started/install

如下

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install -y apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754

echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \
/etc/apt/sources.list.d/clickhouse.list
sudo apt-get update

sudo apt-get install -y clickhouse-server clickhouse-client

sudo service clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you've set up a password.

填写default user的初始密码,回车可以看到如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Enter password for default user:
Password for default user is saved in file /etc/clickhouse-server/users.d/default-password.xml.
Setting capabilities for clickhouse binary. This is optional.
Cannot set 'net_admin' or 'ipc_lock' or 'sys_nice' or 'net_bind_service' capability for clickhouse binary. This is optional. Taskstats accounting will be disabled. To enable taskstats accounting you may add the required capability later manually.
chown -R clickhouse:clickhouse '/etc/clickhouse-server'

ClickHouse has been successfully installed.

Start clickhouse-server with:
sudo clickhouse start

Start clickhouse-client with:
clickhouse-client --password

修改clickhouse数据路径配置,需要将所有/var/lib/clickhouse的配置修改成

1
2
sudo sed -i 's/var\/lib\/clickhouse/data01\/clickhouse/g' /etc/clickhouse-server/config.xml

创建目录

1
2
3
lintong@master:/data01$ sudo mkdir clickhouse
lintong@master:/data01$ sudo chown -R clickhouse:clickhouse ./clickhouse

启动clickhouse server

1
2
3
4
5
6
7
lintong@master:~$ sudo clickhouse start
chown -R clickhouse: '/var/run/clickhouse-server/'
Will run sudo --preserve-env -u 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon
Waiting for server to start
Waiting for server to start
Server started

连接clickhouse

1
2
3
4
5
6
7
lintong@master:~$ clickhouse-client
ClickHouse client version 23.10.3.5 (official build).
Connecting to localhost:9000 as user default.
Password for user (default):
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 23.10.3 revision 54466.

client使用的9000端口

访问的话可以看到如下说明,如果想要使用http连接的话,需要使用8123端口

1
2
3
Port 9000 is for clickhouse-client program
You must use port 8123 for HTTP.

使用datagrip连接clickhouse

基础语法参考官方文档

1
2
https://clickhouse.com/docs/en/guides/creating-tables

创建database,默认会有一个default库

1
2
CREATE DATABASE IF NOT EXISTS helloworld

创建table

1
2
3
4
5
6
7
8
9
10
CREATE TABLE helloworld.my_first_table
(
user_id UInt32,
message String,
timestamp DateTime,
metric Float32
)
ENGINE = MergeTree()
PRIMARY KEY (user_id, timestamp)

写入数据

1
2
3
4
5
6
INSERT INTO helloworld.my_first_table (user_id, message, timestamp, metric) VALUES
(101, 'Hello, ClickHouse!', now(), -1.0 ),
(102, 'Insert a lot of rows per batch', yesterday(), 1.41421 ),
(102, 'Sort your data based on your commonly-used queries', today(), 2.718 ),
(101, 'Granules are the smallest chunks of data read', now() + 5, 3.14159 )

查询数据

1
2
SELECT * FROM helloworld.my_first_table