持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第2天,点击查看活动详情
28.工具使用:ClickHouse从入门到放弃-概述 - 掘金 (juejin.cn)
书接上回,今天学习如何搭建clickhouse
4.2 环境搭建
4.2.1 版本选择
ClickHouse: 22.4.5.9
CentOS: Linux release 7.9.2009
MySQL:MySQL-5.7
4.2.2 下载安装
采用官方预编译的tgz软件包方式安装。
参考文档:
所需的版本可以通过curl或wget从存储库https://packages.clickhouse.com/tgz/下载。
# 查看最新版本 22.4.5.9
[root@localhost anchu]# curl -s https://packages.clickhouse.com/tgz/stable/ |grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' | sort -V -r | head -n 1
22.4.5.9
# 设置临时环境变量并下载
[anchu@localhost ~]$ LATEST_VERSION=$(curl -s https://packages.clickhouse.com/tgz/stable/ | \
> grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' | sort -V -r | head -n 1)
[anchu@localhost ~]$ export LATEST_VERSION
[anchu@localhost ~]$ echo $LATEST_VERSION
22.4.5.9
[anchu@localhost ~]$ cd software/
[anchu@localhost software]$ mkdir clickhouse
[anchu@localhost software]$ cd clickhouse/
# 根据最新版本下载
[anchu@localhost clickhouse]$ curl -O "https://packages.clickhouse.com/tgz/stable/clickhouse-common-static-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$
curl -O "https://packages.clickhouse.com/tgz/stable/clickhouse-common-static-dbg-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$
curl -O "https://packages.clickhouse.com/tgz/stable/clickhouse-server-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$
curl -O "https://packages.clickhouse.com/tgz/stable/clickhouse-client-$LATEST_VERSION-amd64.tgz"
# 查看并解压
[anchu@localhost clickhouse]$ ll -h
total 1011M
-rw-rw-r--. 1 anchu anchu 35K May 11 11:03 clickhouse-client-22.4.5.9-amd64.tgz
-rw-rw-r--. 1 anchu anchu 226M May 11 10:54 clickhouse-common-static-22.4.5.9-amd64.tgz
-rw-rw-r--. 1 anchu anchu 785M May 11 11:01 clickhouse-common-static-dbg-22.4.5.9-amd64.tgz
-rw-rw-r--. 1 anchu anchu 55K May 11 11:03 clickhouse-server-22.4.5.9-amd64.tgz
#解压安装
[anchu@localhost clickhouse]$ tar -xzvf "clickhouse-common-static-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$ tar -xzvf "clickhouse-common-static-dbg-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$ tar -xzvf "clickhouse-server-$LATEST_VERSION-amd64.tgz"
[anchu@localhost clickhouse]$ tar -xzvf "clickhouse-client-$LATEST_VERSION-amd64.tgz"
官网操作(需要注意要加操作系统类型,最新版本加入了操作系统名称amd64,原来的url无法下载数据):
LATEST_VERSION=$(curl -s https://packages.clickhouse.com/tgz/stable/ | \
grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' | sort -V -r | head -n 1)
export LATEST_VERSION
tar -xzvf "clickhouse-common-static-$LATEST_VERSION.tgz"
sudo "clickhouse-common-static-$LATEST_VERSION/install/doinst.sh"
tar -xzvf "clickhouse-common-static-dbg-$LATEST_VERSION.tgz"
sudo "clickhouse-common-static-dbg-$LATEST_VERSION/install/doinst.sh"
tar -xzvf "clickhouse-server-$LATEST_VERSION.tgz"
sudo "clickhouse-server-$LATEST_VERSION/install/doinst.sh"
sudo /etc/init.d/clickhouse-server start
tar -xzvf "clickhouse-client-$LATEST_VERSION.tgz"
sudo "clickhouse-client-$LATEST_VERSION/install/doinst.sh"
#注意 :下面这个因为下载的时候没带amd64,导致下载的文件不对
正常的下载如下:\
对于生产环境,建议使用最新的stable版本。你可以在GitHub页面github.com/ClickHouse/…
\
4.2.3 配置启动
首先,需要当前用户有sudo权限,配置如下
[root@localhost clickhouse]# su root
[root@localhost clickhouse]# chmod u+w /etc/sudoers
[root@localhost clickhouse]# vi /etc/sudoers
anchu ALL=(ALL) ALL
初始化
[anchu@localhost clickhouse]$ su anchu
Password:
[anchu@localhost clickhouse]$ pwd
/home/anchu/software/clickhouse
[anchu@localhost clickhouse]$ sudo "clickhouse-common-static-$LATEST_VERSION/install/doinst.sh"
[anchu@localhost clickhouse]$sudo "clickhouse-common-static-dbg-$LATEST_VERSION/install/doinst.sh"
[anchu@localhost clickhouse] sudo "clickhouse-server-$LATEST_VERSION/install/doinst.sh"
# 安装clickhouse-server
[anchu@localhost clickhouse]$ sudo "clickhouse-server-$LATEST_VERSION/install/doinst.sh"
ClickHouse binary is already located at /usr/bin/clickhouse
Creating symlink /usr/bin/clickhouse-server to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-client to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-local to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-benchmark to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-copier to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-obfuscator to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-git-import to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-compressor to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-format to /usr/bin/clickhouse.
Symlink /usr/bin/clickhouse-extract-from-config already exists but it points to /home/anchu/software/clickhouse/clickhouse. Will replace the old symlink to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-extract-from-config to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-keeper to /usr/bin/clickhouse.
Creating symlink /usr/bin/clickhouse-keeper-converter to /usr/bin/clickhouse.
Creating clickhouse group if it does not exist.
groupadd -r clickhouse
Creating clickhouse user if it does not exist.
useradd -r --shell /bin/false --home-dir /nonexistent -g clickhouse clickhouse
Will set ulimits for clickhouse user in /etc/security/limits.d/clickhouse.conf.
Creating config directory /etc/clickhouse-server.
Creating config directory /etc/clickhouse-server/config.d that is used for tweaks of main server configuration.
Creating config directory /etc/clickhouse-server/users.d that is used for tweaks of users configuration.
Data path configuration override is saved to file /etc/clickhouse-server/config.d/data-paths.xml.
Log path configuration override is saved to file /etc/clickhouse-server/config.d/logger.xml.
User directory path configuration override is saved to file /etc/clickhouse-server/config.d/user-directories.xml.
OpenSSL path configuration override is saved to file /etc/clickhouse-server/config.d/openssl.xml.
Creating log directory /var/log/clickhouse-server.
Creating data directory /var/lib/clickhouse.
Creating pid directory /var/run/clickhouse-server.
chown -R clickhouse:clickhouse '/var/log/clickhouse-server'
chown -R clickhouse:clickhouse '/var/run/clickhouse-server'
chown clickhouse:clickhouse '/var/lib/clickhouse'
groupadd -r clickhouse-bridge
useradd -r --shell /bin/false --home-dir /nonexistent -g clickhouse-bridge clickhouse-bridge
chown -R clickhouse-bridge:clickhouse-bridge '/usr/bin/clickhouse-odbc-bridge'
chown -R clickhouse-bridge:clickhouse-bridge '/usr/bin/clickhouse-library-bridge'
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.
Allow server to accept connections from the network (default is localhost only), [y/N]:
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
#默认用户 default 密码 clickhouse
# 安装clickhouse-client
[anchu@localhost clickhouse]$ sudo "clickhouse-client-$LATEST_VERSION/install/doinst.sh"
启动clickhouse-server\
[anchu@localhost clickhouse]$ sudo clickhouse start
chown -R clickhouse: '/var/run/clickhouse-server/'
Will run su -s /bin/sh 'clickhouse' -c '/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
Server started
启动clickhouse-client,并连接clickhouse-server (--multiline等同于-m,支持sql换行)
[anchu@localhost clickhouse]$ clickhouse-client --user=default --password=clickhouse --host=127.0.0.1 --multiline
ClickHouse client version 22.4.5.9 (official build).
Connecting to 127.0.0.1:9000 as user default.
Connected to ClickHouse server version 22.4.5 revision 54455.
Warnings:
* Linux transparent hugepage are set to "always".
* Linux threads max count is too low.
* Maximum number of threads is lower than 30000. There could be problems with handling a lot of simultaneous queries.
localhost :) show tables;
SHOW TABLES
0 rows in set. Elapsed: 0.002 sec.
localhost :) quit;
Bye.
查看进程
[anchu@localhost ~]$ ps -ef|grep clickhouse
clickho+ 29606 1 0 11:30 ? 00:00:00 clickhouse-watchdog --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon
clickho+ 29607 29606 1 11:30 ? 00:00:11 /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon
anchu 29991 29295 0 11:40 pts/2 00:00:00 clickhouse-client --user=default --password= -h 127.0.0.1 --port 9000 -m
anchu 30036 29855 0 11:45 pts/1 00:00:00 grep --color=auto clickhouse
登录数据库执行的每条SQL,ClickHouse都会自动记录到当前用户根路径的.clickhouse-client-history
[anchu@localhost ~]$ cat ~/.clickhouse-client-history
### 2022-05-11 11:34:31.179
show tables;
### 2022-05-11 11:37:23.508
quit;
### 2022-05-11 11:41:50.100
select 1;
### 2022-05-11 11:42:22.323
quit;
[anchu@localhost ~]$
4.2.4 测试数据
修改默认配置文件/etc/clickhouse-server/config.xml,支持远程访问
[anchu@localhost ~]$ su root
Password:
[root@localhost anchu]# vi /etc/clickhouse-server/config.xml
[root@localhost anchu]# chmod u+w /etc/clickhouse-server/config.xml
[root@localhost anchu]# vi /etc/clickhouse-server/config.xml
#去掉注释
<listen_host>::</listen_host>
#或者 <listen_host>0.0.0.0</listen_host>
#重启
[anchu@localhost ~]$ su anchu
#注意启动方式
[anchu@localhost ~]$
sudo clickhouse start
连接测试 (dbeaver或者clickhouse客户端均可)
#默认用户 default 密码 clickhouse 默认库default
[anchu@localhost clickhouse]$ clickhouse-client --user=default --password=clickhouse -h 192.168.120.110 --port 9000 -m
ClickHouse client version 22.4.5.9 (official build).
Connecting to 192.168.120.110:9000 as user default.
Connected to ClickHouse server version 22.4.5 revision 54455.
localhost :)