官方文档:https://clickhouse.com/docs/zh/install
一、单机版部署
环境说明:centos7.5、clickhouse25.7.1
环境准备
systemctl stop firewalld && systemctl disable firewalld
查看selinux状态是否为disabled,否则修改
[root@localhost ~]# getenforce
Enforcing
修改为disabled
vim /etc/selinux/config
#修改
SELINUX=disabled
#重启生效
reboot
修改打开文件数限制
在 /etc/security/limits.conf文件的末尾加入以下内容
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
在/etc/security/limits.d/20-nproc.conf文件的末尾加入以下内容
vim /etc/security/limits.d/20-nproc.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
安装依赖
yum install -y libtool
yum install -y *unixODBC*
1、设置RPM仓库
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
2、安装ClickHouse服务器和客户端
sudo yum install -y clickhouse-server clickhouse-client
3、启动ClickHouse服务器
解除IP访问限制
vim /etc/clickhouse-server/config.xml
#265行打开注释
<listen_host>::</listen_host>
sudo systemctl enable clickhouse-server
sudo systemctl start clickhouse-server
sudo systemctl status clickhouse-server
要启动ClickHouse客户端,请运行:
clickhouse-client
如果您为服务器设置了密码,则需要运行:
clickhouse-client --password
4、设置管理员用户和密码
原来默认有default用户,如果想要修改为root,执行以下步骤
vim /etc/clickhouse-server/users.xml
找到
<users>
<default>
... ...
</default>
</users>
更改为下面,用户名root,密码123456
<users>
<root>
<password>123456</password>
... ...
</root>
</users>
wq!保存
重启服务
sudo systemctl restart clickhouse-server
5、测试连接


二、集群版部署
环境说明:172.16.22.100/101/102
1、参考上述单机版部署,三台机器上分别部署单机版clickhouse
启动成功后,可以使用命令连接clickhouse:
clickhouse-client -u root
2、安装zookeeper集群,三台机器均需要操作
zookeeper节点必须是奇数个,因为zookeeper选举的规则:leader选举,要求可用节点数量必须大于总节点数量/2,所以我们这次选用了三台机器部署
(1)准备JDK环境,这里使用JDK8
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
chmod +x jdk-8u131-linux-x64.rpm
rpm -ivh jdk-8u131-linux-x64.rpm
java -version
(2)下载zookeeper
yum -y install wget
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
(3)解压安装
tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz
(4)配置环境变量
打开配置文件:
sudo vim /etc/profile
添加我们需要的配置信息:
(ZOOKEEPER_HOME 为你安装的zookeeper目录)
export ZOOKEEPER_HOME=/root/apache-zookeeper-3.5.9-bin
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
使环境变量生效
source /etc/profile
可选择查看是否生效:
echo $ZOOKEEPER_HOME
(5)配置zookeeper
先创建一个data目录,记下它的路径,等会要用到:
mkdir /root/apache-zookeeper-3.5.9-bin/data
进入到zookeeper配置文件目录,做以下操作:
cd apache-zookeeper-3.5.9-bin/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
打开编辑zoo.cfg,显示如下内容。dataDir改成你自己刚刚创建的目录,根据自己机器内网ip,
增加或更换server.1 2 3 ……的ip,一般机器数量都为3 5 7 等奇数,其他的配置不用变。
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/root/apache-zookeeper-3.5.9-bin/data
clientPort=2181
server.1=172.16.22.100:2888:3888
server.2=172.16.22.101:2888:3888
server.3=172.16.22.102:2888:3888
这里的ip可以更换为主机名
为每台机器配置节点id:
在机器一上执行:
echo 1 >/root/apache-zookeeper-3.5.9-bin/data/myid
在机器二上执行:
echo 2 >/root/apache-zookeeper-3.5.9-bin/data/myid
在机器三上执行:
echo 3 >/root/apache-zookeeper-3.5.9-bin/data/myid
到这里就配置完成了。
(6)启动zookeeper
cd /root/apache-zookeeper-3.5.9-bin/bin
sh zkServer.sh start
(7)查看zookeeper启动状态
#leader节点输出
sh zkServer.sh status
/bin/java
ZooKeeper JMX enabled by default
Using config: /root/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
#其他两个节点输出
sh zkServer.sh status
/bin/java
ZooKeeper JMX enabled by default
Using config: /root/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
(8)启动成功后可以随便选择一个节点尝试连接另一个节点验证
sh zkCli.sh -server 172.16.22.101:2181
[zk: 172.16.22.101:2181(CONNECTED) 0]
3、部署clickhouse集群
ClickHouse提供了非常高级的基于zookeeper的表复制方式,同时也提供了基于Cluster的复制方式,这里先采用基于zookeeper的表复制方式
(1)打开配置文件更改
vim /etc/clickhouse-server/config.xml
配置zookeeper字段,三台机器都一样:
host改成你自己的zookeeper 内网ip地址或者主机名
如果副本采用不同物理机器,需要加入到zookeeper节点中,后期数据同步需要用到。
记得取消注释符号
<!--
-->
再更改
<zookeeper>
<node>
<host>172.16.22.100</host>
<port>2181</port>
</node>
<node>
<host>172.16.22.101</host>
<port>2181</port>
</node>
<node>
<host>172.16.22.102</host>
<port>2181</port>
</node>
</zookeeper>
配置 remote_server字段,三台机器都一样:
找到配置文件中的 remote_server标签,发现它里面有很多的内容,
我们没有都用到,它只是给我一个例子,vim模式下/remote_servers导航,然后43dd,把里面的内容都删除,粘贴上我们自己想要的:
<remote_servers>
<!--ck_cluster是集群名字,自己命名就可以,建库建表需要用到-->
<ck_cluster>
<!-- 数据分片1 -->
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>172.16.22.100</host>
<port>9000</port>
</replica>
</shard>
<!-- 数据分片2 -->
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>172.16.22.101</host>
<port>9000</port>
</replica>
</shard>
<!-- 数据分片3 -->
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>172.16.22.102</host>
<port>9000</port>
</replica>
</shard>
</ck_cluster>
</remote_servers>
配置macros字段,根据每台机器的分片副本配置
记得取消注释符号
<!--
-->
172.16.22.100
<macros>
<shard>01</shard>
<replica>01</replica>
</macros>
172.16.22.101
<macros>
<shard>02</shard>
<replica>01</replica>
</macros>
172.16.22.102
<macros>
<shard>03</shard>
<replica>01</replica>
</macros>
这样配置表示三个分片(shards)和一个副本(replica)
配置文件是热更新的,所以修改配置后无需要重启服务,除非是首次启动 ClickHouse
按照上述部署完成后,所得到的集群是3个分片(shards)和1个副本(replica),这样部署的话只能提供一定的负载均衡和数据水平拆分
如果需要3分片2副本集群,请跳转ClickHouse集群部署实践—3分片2副本集群
4、验证集群
(1)每台机器上用命令查看是否成功
172.16.22.100
clickhouse-client -uroot --password=123456 --query="select * from system.macros;";
replica 01
shard 01
172.16.22.101
clickhouse-client -uroot --password=123456 --query="select * from system.macros;";
replica 01
shard 02
172.16.22.102
clickhouse-client -uroot --password=123456 --query="select * from system.macros;";
replica 01
shard 03
(2)查看集群状态
clickhouse-client -uroot --password=123456 --query="select * from system.clusters;";
ck_cluster 1 1 1 1 172.16.22.100 172.16.22.100 9000 0 default 0 0 0 \N \N \N \N
ck_cluster 2 1 1 1 172.16.22.101 172.16.22.101 9000 0 default 0 0 0 \N \N \N \N
ck_cluster 3 1 1 1 172.16.22.102 172.16.22.102 9000 1 default 0 0 0 \N \N \N \N
当我们配置完成以上之后,基于zookeeper的clickhouse集群就部署完成了
5、测试数据
创建 ReplicatedMergeTree 测试表:
任选一台机器(下面选择172.16.22.100执行),创建一个 ReplicatedMergeTree 引擎的测试表,测试 ZooKeeper 同步功能
clickhouse-client -uroot --password=123456
localhost :) CREATE TABLE test_ck ON CLUSTER ck_cluster (EventDate DateTime, Number UInt32, id UInt32 ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck', '{replica}') PARTITION BY toYYYYMM(EventDate) ORDER BY (Number, EventDate, intHash32(id)) SAMPLE BY intHash32(id);
CREATE TABLE test_ck ON CLUSTER ck_cluster
(
`EventDate` DateTime,
`Number` UInt32,
`id` UInt32
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck', '{replica}')
PARTITION BY toYYYYMM(EventDate)
ORDER BY (Number, EventDate, intHash32(id))
SAMPLE BY intHash32(id)
Query id: 49902d5b-f7c1-4946-95c9-bde9e46a8155
Elapsed: 0.012 sec.
Received exception from server (version 25.7.1):
Code: 139. DB::Exception: Received from localhost:9000. DB::Exception: There is no Zookeeper configuration in server config. (NO_ELEMENTS_IN_CONFIG)
出现上述报错的话,退出去重启一下clickhouse-server服务再执行
建表test_ck
localhost :) CREATE TABLE test_ck ON CLUSTER ck_cluster (EventDate DateTime, Number UInt32, id UInt32 ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck', '{replica}') PARTITION BY toYYYYMM(EventDate) ORDER BY (Number, EventDate, intHash32(id)) SAMPLE BY intHash32(id);
CREATE TABLE test_ck ON CLUSTER ck_cluster
(
`EventDate` DateTime,
`Number` UInt32,
`id` UInt32
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck', '{replica}')
PARTITION BY toYYYYMM(EventDate)
ORDER BY (Number, EventDate, intHash32(id))
SAMPLE BY intHash32(id)
Query id: c78f9e49-5f10-4520-86a8-2930142f63c3
┌─host──────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
1. │ 172.16.22.101 │ 9000 │ 0 │ │ 2 │ 2 │
2. │ 172.16.22.102 │ 9000 │ 0 │ │ 1 │ 1 │
3. │ 172.16.22.100 │ 9000 │ 0 │ │ 0 │ 0 │
└───────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
3 rows in set. Elapsed: 2.428 sec.
建表test_ck1
localhost :) CREATE TABLE test_ck1 ON CLUSTER ck_cluster (EventDate DateTime, Number UInt32, id UInt32 ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck1', '{replica}') PARTITION BY toYYYYMM(EventDate) ORDER BY (Number, EventDate, intHash32(id)) SAMPLE BY intHash32(id);
CREATE TABLE test_ck1 ON CLUSTER ck_cluster
(
`EventDate` DateTime,
`Number` UInt32,
`id` UInt32
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test_ck1', '{replica}')
PARTITION BY toYYYYMM(EventDate)
ORDER BY (Number, EventDate, intHash32(id))
SAMPLE BY intHash32(id)
Query id: bb06aa3c-c2d8-4167-b4c9-e4ad8c115bc6
┌─host──────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
1. │ 172.16.22.102 │ 9000 │ 0 │ │ 2 │ 1 │
2. │ 172.16.22.101 │ 9000 │ 0 │ │ 1 │ 1 │
3. │ 172.16.22.100 │ 9000 │ 0 │ │ 0 │ 0 │
└───────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
3 rows in set. Elapsed: 0.914 sec.
localhost :) SHOW TABLES;
SHOW TABLES
Query id: 6d86dde6-5776-4e40-b3a7-41e085011ad8
┌─name─────┐
1. │ test_ck │
2. │ test_ck1 │
└──────────┘
2 rows in set. Elapsed: 0.005 sec.
在其他机器节点show tables查看表结构是否同步成功
172.16.22.101
localhost :) show tables
SHOW TABLES
Query id: 731a00e7-a2f9-4040-bf7c-466e39bd07e1
┌─name─────┐
1. │ test_ck │
2. │ test_ck1 │
└──────────┘
2 rows in set. Elapsed: 0.005 sec.
172.16.22.102
localhost :) show tables;
SHOW TABLES
Query id: 31038e56-5880-4d48-9e05-4230649639c2
┌─name─────┐
1. │ test_ck │
2. │ test_ck1 │
└──────────┘
2 rows in set. Elapsed: 0.005 sec.
524

被折叠的 条评论
为什么被折叠?



