操作系统:Centos-7.7
ClickHouse: YUM 在线安装,20.x
官网:https://clickhouse.yandex/ 具体安装细节看:https://clickhouse.tech/#quick-start
安装前准备
CentOS7 打开文件数限制:
在 /etc/security/limits.conf 这个文件的末尾加入一下内容:
sudo vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
在 /etc/security/limits.d/90-nproc.conf 这个文件的末尾加入一下内容:
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
重启服务器之后生效,用 ulimit -n 或者 ulimit -a 查看设置结果
ulimit -n
ulimit -a
CentOS7 取消 SELINUX
修改 /etc/selinux/config 中的 SELINUX=disabled 后重启
vim /etc/selinux/config
SELINUX=disabled
关闭防火墙
centos6:
service iptables stop
service ip6tables stop
centos7:
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl start firewalld.service
安装依赖
yum install -y libtool
yum install -y *unixODBC*
单机模式安装
ClickHouse的安装可以使用 yum在线安装,也可以使用 rpm 离线安装的方式! 具体信息见官网文档:https://clickhouse.tech/#quick-start
准备操作
需要验证当前服务器的 CPU 是否支持 SSE 4.2 指令集,因为向量化执行需要用到这项特性:
grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"
安装
依次执行如下命令:
yum install yum-utils -y
rpm --import https://repo.clickhouse.tech/CLICKHOUSE-KEY.GPG
yum-config-manager --add-repo https://repo.clickhouse.tech/rpm/clickhouse.repo yum install clickhouse-server clickhouse-client -y
如果您没法链接互联网,则也可以使用 rpm 的方式来进行离线安装:需要下载的安装包有:
clickhouse-server-20.5.4.40-2.noarch.rpm
clickhouse-common-static-20.5.4.40-2.x86_64.rpm
clickhouse-client-20.5.4.40-2.noarch.rpm
下载地址在:
https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/ https://packagecloud.io/Altinity/clickhouse
启动服务端
前台启动:
clickhouse-server --config-file=/etc/clickhouse-server/config.xml
后台启动:
nohup clickhouse-server --config-file=/etc/clickhouse-server/config.xml 1>~/logs/clickhouse_std.log 2>~/logs/clickhouse_err.log &
启动成功,检查一下:
ps -aux | grep clickhouse
netstat -nltp | grep clickhouse
如果报权限错误,修改安装目录的权限,默认使用 clickhouse 用户。
命令为:
cd /var/lib/
chown -R root:root clickhouse
默认情况下: /var/lib/clickhouse 的 group 和 owner 是 : cickhouse:clickhouse
两种解决方式:
1、如果你坚持使用root启动:请把/var/lib/clickhouse 改成:root:root
2、你使用clickhouse用户去启动:chmod -u /bin/bash clickhouse
su - clickhouse 切换用户之后再启动!
启动客户端
具体命令:
clickhouse-client
或者:
TZ=Asia/Shanghai clickhouse-client
启动情况如下:
[root@ck05 ~]# clickhouse-client
ClickHouse client version 20.5.4.40 (official build). Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.5.4 revision 54435.
ck05 :) show databases;
SHOW DATABASES
┌─name───────────────────────────┐
│ default │
│ system │
└────────────────────────────────┘
4 rows in set. Elapsed: 0.001 sec.
ck05 :) quit
Bye.
基本使用
创建库:
create database test;
切换库:
use test;
创建表:
create table test01(id Int8, name String) engine = TinyLog;
查询表列表:
show tables;
插入数据和统计查询:
insert into test01 values (1, 'test'), (2, 'clickhouse'), (3, 'spark');
select count(*) as total from test01;
退出客户端:
quit
安装成功的核心目录
(1)/etc/clickhouse-server:服务端的配置文件目录,包括全局配置config.xml和用户配置users.xml 等。
(2)/var/lib/clickhouse:默认数据存储目录,通常会修改默认路径配置,将数据保存到大容量磁盘挂 载路径
(3)/var/log/clickhouse-server:默认日志保存目录,通常会修改路径配置将日志保存到大容量磁盘 挂载的路径
可执行文件
(1)clickhouse:主程序的可执行文件。
(2)clickhouse-client:一个指向ClickHouse可执行文件的软链接,供客户端连接使用。
(3)clickhouse-server:一个指向ClickHouse可执行文件的软链接,供服务端启动使用。
(4)clickhouse-compressor:内置提供的压缩工具,可用于数据的正压反解。
集群模式
注意事项:
每个节点的 clickhouse 都是单独的服务器在运行,在有需要的时候,可以通过配置文件,把他们联合 起来变成一个集群。甚至还可以,把你准备的所有的服务器,配置成多个集群!
就算你配置成集群了,每个服务器依然还是单独运行的。 如果你创建了一张表,这张表的引擎是分布式的引擎,那么这个表所存储在那个集群里面的那个集群里面的机器,就是一个集群了。每个 clickhouse 中的部分表是单机的, 部分表是分布式的。 clickhouse 既是单独运行的。游离于集群之外。被包含在集群之类的!
表:单机的表,和分布式的表
在其他的机器上,分别安装上clickhouse
这里以4节点为例,都需要安装一下,集群安装与单机模式相同。
四台机器修改配置文件config.xml
vim /etc/clickhouse-server/config.xml
先修改 tcp_port 为 9977, 因为这个端口和 HDFS 的冲突了
<tcp_port>9977</tcp_port>
listen_host 表示能监听的主机,:: 表示任意主机都可以访问
<listen_host>::</listen_host>
<!-- <listen_host>::1</listen_host> -->
<!-- <listen_host>127.0.0.1</listen_host> -->
所有节点同步:
scp -r /etc/clickhouse-server/config.xml ck02:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/config.xml ck03:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/config.xml ck04:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/config.xml ck05:/etc/clickhouse-server/
在四台机器的etc目录下新建metrika.xml文件
vim /etc/metrika.xml
<yandex>
<clickhouse_remote_servers>
<!-- 4分片1副本 --> <nx_clickhouse_4shards_1replicas>
<shard>
<!-- 数据自动同步 --> <internal_replication>true</internal_replication> <replica>
<host>ck02</host>
<port>9977</port>
</replica>
</shard>
<shard>
<replica>
<internal_replication>true</internal_replication>
<host>ck03</host>
<port>9977</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>ck04</host>
<port>9977</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>ck05</host>
<port>9977</port>
</replica>
</shard>
</nx_clickhouse_4shards_1replicas>
</clickhouse_remote_servers>
<!-- zookeeper 自动同步 --> <zookeeper-servers>
<node index="2">
<host>ck02</host>
<port>2181</port>
</node>
<node index="3">
<host>ck03</host>
<port>2181</port>
</node>
<node index="4">
<host>ck04</host>
<port>2181</port>
</node>
</zookeeper-servers>
<!-- 配置文件中macros若省略,则建复制表时每个分片需指定zookeeper路径及副本名称,同一分片 上路径相同,副本名称不同;若不省略需每个分片不同配置 -->
<macros>
<replica>ck02</replica>
</macros>
<networks>
<ip>::/0</ip>
</networks>
<!-- 配置压缩 --> <clickhouse_compression>
<case>
<min_part_size>10000000000</min_part_size> <min_part_size_ratio>0.01</min_part_size_ratio> <method>lz4</method>
</case>
</clickhouse_compression>
</yandex>
需要根据不同的机器做不同的修改。 所有节点同步:
scp -r /etc/metrika.xml ck02:/etc/
scp -r /etc/metrika.xml ck03:/etc/
scp -r /etc/metrika.xml ck04:/etc/
scp -r /etc/metrika.xml ck05:/etc/
启动
切记:要先启动 zookeeper
zkServer.sh start
启动服务端
nohup clickhouse-server --config-file=/etc/clickhouse-server/config.xml 1>~/logs/clickhouse_std.log 2>~/logs/clickhouse_err.log &
检查启动是否成功
netstat -nltp | grep clickhouse
启动客户端:
clickhouse-client --host=... --port=... --user=... --password=... -m
参数解释:
username:用户名
password:密码
ip:服务器IP
port:端口
-m:允许多行查询
启动本地多行查询客户端
clickhouse-client --host=localhost --port=9977 -m
clickhouse-client --host=ck02 --port=9977 --user=ck --password=ck -m
检查进程
ps -aux | grep clickhouse
初始使用
查看数据库列表:
show databases;
查看表列表:
show tables;
ClickHouse 的 Client 参数介绍
默认情况下,配置文件的搜索顺序如下
./clickhouse-client.xml
~/.clickhouse-client/config.xml
/etc/clickhouse-client/config.xml
设置用户和密码
进入到配置文件目录,修改配置配置文件:
vim /etc/clickhouse-server/users.xml
系统默认使用 default 用户登录 无密码。 现在我们配置用户 ck 密码为 ck
配置一个用户:你配置的 ck 就是用户名, 这个标签中的值,就是密码
<ck>
<password>ck</password>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</ck>
所有节点同步:
scp -r /etc/clickhouse-server/users.xml ck02:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/users.xml ck03:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/users.xml ck04:/etc/clickhouse-server/
scp -r /etc/clickhouse-server/users.xml ck05:/etc/clickhouse-server/
启动客户端连接:
clickhouse-client --host=ck02 --port=9977 --user=bigdata --password=bigdata -m
ClickHouse一些信息总结
配置文件路径
/etc/clickhouse-server/config.xml
/etc/clickhouse-server/users.xml
日志文件路径
/var/log/clickhouse-server/
表信息路径、元数据存储目录
/var/lib/clickhouse/metadata/
表数据路径
/var/lib/clickhouse/data/