1、安装JDK
1.首先从官网下载 [jdk-8u131-linux-x64.tar](http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz)
java -version
2.创建一个存放jdk压缩包的目录
mkdir /usr/local/java/
3.把压缩包放到/usr/local/java/下面,并输入下面命令进行解压
tar -zxvf jdk-8u131-linux-x64.tar
重命名:
mv jdk1.8.0_281/ jdk1.8
4.配置[环境变量]
vi /etc/profile
在此文件末尾添加如下命令
/usr/local/java/
```js
#export JAVA_HOME=/home/soft/tools/jdk1.8
export JAVA_HOME=/usr/local/java/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
5.重新加载配置文件,使配置文件生效。
source /etc/profile
6.输入命令检查是否安装成功
java -version
2、安装zookeeper
1、创建目录:
mkdir -p /data/server/zookeeper #创建安装目录
mkdir -p /data/server/zookeeper/data #创建数据目录
mkdir -p /data/server/zookeeper/logs #创建日志目录
2、解压:
tar zxvf apache-zookeeper-3.8.1-bin.tar.gz -C /data/server/zookeeper --strip-components 1
cp /data/server/zookeeper/conf/zoo_sample.cfg /data/server/zookeeper/conf/zoo.cfg
vi /data/server/zookeeper/conf/zoo.cfg
3、#拷贝配置文件
cp /data/server/zookeeper/conf/zoo_sample.cfg /data/server/zookeeper/conf/zoo.cfg
vi /data/server/zookeeper/conf/zoo.cfg #修改添加,注意端口等参数不要重复添加,否则启动失败
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/server/zookeeper/data #数据目录
dataLogDir=/data/server/zookeeper/logs #日志目录
clientPort=2181
4、#添加zookeeper节点
#服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
#本机的地址也可以写成server.1=0.0.0.0:2888:3888
server.1=192.168.3.34:2888:3888
server.2=192.168.3.35:2888:3888
server.3=192.168.3.36:2888:3888
server.4=192.168.3.37:2888:3888
:#wq! #保存退出
5、机器配置:vi /etc/profile #配置环境变量
#set zookeeper environment
export ZOOKEEPER_HOME=/data/server/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
:wq! #保存退出
source /etc/profile #使配置立即生效
设置hostname
hostnamectl set-hostname test01
hostnamectl set-hostname test02
hostnamectl set-hostname test03
hostnamectl set-hostname test04
配置:
vi /data/server/zookeeper/conf/zoo.cfg
dataDir=/data/server/zookeeper/data #数据目录
dataLogDir=/data/server/zookeeper/logs #日志目录
6、台服务器分别执行:
echo "1" >/data/server/zookeeper/data/myid
echo "2" >/data/server/zookeeper/data/myid
echo "3" >/data/server/zookeeper/data/myid
echo "4" >/data/server/zookeeper/data/myid
配置好zoo.cfg后,复制到其他服务器
scp /data/server/zookeeper/conf/zoo.cfg test03@192.168.3.35:/data/server/zookeeper/conf/
scp /data/server/zookeeper/conf/zoo.cfg test02@192.168.3.36:/data/server/zookeeper/conf/
scp /data/server/zookeeper/conf/zoo.cfg test01@192.168.3.37:/data/server/zookeeper/conf/
在3台机器配置:vi /etc/profile #配置环境变量
#set zookeeper environment
export ZOOKEEPER_HOME=/data/server/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
:wq! #保存退出
source /etc/profile #使配置立即生效
启动命令:/data/server/zookeeper/bin/zkServer.sh start
停止命令:/data/server/zookeeper/bin/zkServer.sh stop
重启命令:/data/server/zookeeper/bin/zkServer.sh restart
状态查看命令:/data/server/zookeeper/bin/zkServer.sh status
查看状态:
[root@localhost conf]# /data/server/zookeeper/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /data/server/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
ps -ef |grep zookeeper #查看进程
telnet 192.168.3.34 2181 #查看端口
7、添加防火墙端口:
firewall-cmd --permanent --add-port=2181/tcp
firewall-cmd --zone=pulic --add-port=2181/tcp --permanent
firewall-cmd --reload--加载生效
8、添加开机启动
vi /etc/rc.d/rc.local #添加开机启动
/bin/sh /data/server/zookeeper/bin/zkServer.sh start
:wq! #保存退出
\#使用普通用户myuser启动,注意需要设置zookeeper的目录所有者为myuser用户
su - myuser -c "/bin/sh /data/server/zookeeper/bin/zkServer.sh start"
默认/etc/rc.local没有执行权限,需要手动添加执行权限
chmod +x /etc/rc.d/rc.local
sh /data/server/zookeeper/bin/zkServer.sh start #启动zookeeper,注意zookeeper集群需要所有节点同时启动
3、安装clickhouse
1、关闭selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
2、配置系统打开文件数限制
vi /etc/security/limits.conf #文件句柄数量的配置
*soft nofile 65536
*hard nofile 65536
*soft nproc 131072
*hard nproc 131072
vi /etc/security/limits.d/20-nproc.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
3、添加hosts解析
vi /etc/hosts #编辑配置文件
192.168.3.34 Node01
192.168.3.34 Node02
192.168.3.34 Node03
192.168.3.34 Node04
4、创建目录:
mkdir -p /data/soft-install/databases/
rm -rf /data/soft-install/databases
复制文件夹过去
cd /home/datanode05/Templates/jdkj-data
cp -r databases /data/soft-install/
5、服务器拷贝数据文件包:
scp -r clickhouse-client-23.6.2.18-amd64.tgz root@ip:/data/soft-install/databases
scp -r clickhouse-keeper-23.6.2.18-amd64.tgz root@ip:/data/soft-install/databases
scp -r clickhouse-common-static-23.6.2.18-amd64.tgz root@ip:/data/soft-install/databases
scp -r clickhouse-common-static-dbg-23.6.2.18-amd64.tgz root@ip:/data/soft-install/databases
6、解压安装包
cd /data/soft-install/databases/
tar -xzvf "clickhouse-common-static-23.6.2.18-amd64.tgz"
tar -xzvf "clickhouse-common-static-dbg-23.6.2.18-amd64.tgz"
tar -xzvf "clickhouse-client-23.6.2.18-amd64.tgz"
tar -xzvf "clickhouse-server-23.6.2.18-amd64.tgz"
7、启动安装 :
./clickhouse-common-static-23.6.2.18/install/doinst.sh
./clickhouse-common-static-dbg-23.6.2.18/install/doinst.sh
./clickhouse-client-23.6.2.18/install/doinst.sh
./clickhouse-server-23.6.2.18/install/doinst.sh
configure
sudo /etc/init.d/clickhouse-server start
8、配置目录说明
/etc/clickhouse-server #服务端的配置文件目录,包括全局配置config.xml和用户配置users.xml等
/etc/clickhouse-client #客户端配置,包括conf.d文件夹和config.xml文件
/var/lib/clickhouse #默认的数据存储目录,建议修改路径到大容量磁盘
/var/log/clickhouse-server #默认保存日志的目录,建议修改路径到大容量磁盘
9、新建存储目录
mkdir -p /data/server/clickhouse #数据存储目录
mkdir -p /data/server/clickhouse/log #日志存放目录
mkdir -p /data/server/clickhouse/tmp_path
mkdir -p /data/server/clickhouse/user_files_path
mkdir -p /data/server/clickhouse/format_schema_path
给新建目录权限:
chown clickhouse:clickhouse /data/server/clickhouse -R
chown clickhouse:clickhouse /data/server/clickhouse/log -R
chown clickhouse:clickhouse /data/server/clickhouse/tmp_path -R
chown clickhouse:clickhouse /data/server/clickhouse/user_files_path -R
chown clickhouse:clickhouse /data/server/clickhouse/format_schema_path -R
chmod +x /data/server/clickhouse -R
chmod +x /data/server/clickhouse/log -R
chmod +x /data/server/clickhouse/tmp_path -R
chmod +x /data/server/clickhouse/user_files_path -R
chmod +x /data/server/clickhouse/format_schema_path -R
chown clickhouse:clickhouse /etc/clickhouse-server/config.xml
chown clickhouse:clickhouse /etc/clickhouse-server/users.xml
chown clickhouse:clickhouse /etc/clickhouse-server/config.d/metrika-shard.xml
chmod +x /etc/clickhouse-server/config.xml
chmod +x /etc/clickhouse-server/users.xml
chmod +x /etc/clickhouse-server/config.d/metrika-shard.xml
10、配置/etc
cp /etc/clickhouse-server/config.xml /etc/clickhouse-server/config.xml.bak
cp /etc/clickhouse-server/users.xml /etc/clickhouse-server/users.xml.bak
11、修改密码:
vi /etc/clickhouse-server/users.xml
<password>123456</password>
12.1、修改全局配置信息
vi /etc/clickhouse-server/config.xml
<timezone>Asia/Shanghai</timezone> #修改时区
<listen_host>::</listen_host> #开启外部访问
<path>/data/server/clickhouse/</path> #修改数据存放路径,默认是<path>/var/lib/clickhouse</path>
<tmp_path>/data/server/clickhouse/tmp/</tmp_path>
<user_files_path>/data/server/clickhouse//user_files/</user_files_path>
<format_schema_path>/data/server/clickhouse/format_schemas/</format_schema_path>
<include_from>/etc/clickhouse-server/config.d/metrika-shard.xml</include_from>
<level>none</level> #不记录日志
<!-- <log>/data/server/clickhouse/log/clickhouse-server.log</log> --> #不记录日志
<!-- <errorlog>/data/server/clickhouse/log/clickhouse-server.err.log</errorlog> --> #不记录日志
<log>/data/server/clickhouse/log/clickhouse-server/clickhouse-server.log</log>
<errorlog>/data/server/clickhouse/log/clickhouse-server/clickhouse-server.err.log</errorlog>
<max_open_files>1048576</max_open_files> #文件句柄数量的配置
<http_port>8123</http_port> #http默认端口
<tcp_port>9000</tcp_port> #tcp默认端口
:wq! #保存退出
12.2、在metrika-shard.xml里面 标签内配置副本节点信息
<?xml version="1.0"?>
<yandex>
<remote_servers>
#分片名称,自定义
<ck_cluster>
<shard>#1
<internal_replication>true</internal_replication>
<replica>
<host>192.168.3.34</host>
<port>9000</port>
<user>default</user>
<password>123456</password>
</replica>
<replica>
<host>192.168.3.35</host>
<port>9100</port>
<user>default</user>
<password>123456</password>
</replica>
</shard>
<shard>#2
<internal_replication>true</internal_replication>
<replica>
<host>192.168.3.35</host>
<port>9000</port>
<user>default</user>
<password>123456</password>
</replica>
<replica>
<host>192.168.3.36</host>
<port>9100</port>
<user>default</user>
<password>123456</password>
</replica>
</shard>
<shard>#3
<internal_replication>true</internal_replication>
<replica>
<host>192.168.3.36</host>
<port>9000</port>
<user>default</user>
<password>123456</password>
</replica>
<replica>
<host>192.168.3.37</host>
<port>9100</port>
<user>default</user>
<password>123456</password>
</replica>
</shard>
<shard>#4
<internal_replication>true</internal_replication>
<replica>
<host>192.168.3.37</host>
<port>9000</port>
<user>default</user>
<password>123456</password>
</replica>
<replica>
<host>192.168.3.34</host>
<port>9100</port>
<user>default</user>
<password>123456</password>
</replica>
</shard>
</ck_cluster>
</remote_servers>
<zookeeper>
<node>
<host>192.168.3.34</host>
<port>2181</port>
</node>
<node>
<host>192.168.3.35</host>
<port>2181</port>
</node>
<node>
<host>192.168.3.36</host>
<port>2181</port>
</node>
<node>
<host>192.168.3.37</host>
<port>2181</port>
</node>
</zookeeper>
<macros>
<shard>02</shard>
<replica>rep_2_1</replica>
</macros>
</yandex>
启动clickhouse
clickhouse status
clickhouse start
clickhouse stop
clickhouse restart
clickhouse status
clickhouse-client
13、测试链接
测试链接
clickhouse-client --password -m
clickhouse-client --host ip --port=9000
clickhouse-client --host ip --port=9000
clickhouse-client --host ip --port=9000
clickhouse-client --host ip --port=9000
查看集群:
select * from system.clusters;
SELECT * FROM system.zookeeper WHERE path = '/clickhouse';
select * from system.macros;
14、测试数据库
CREATE DATABASE db_test_S2RAB ON CLUSTER 'ck_cluster';
CREATE TABLE IF NOT EXISTS db_test_S2RAB.TEST_0A_LOCAL ON CLUSTER 'ck_cluster'
(
`id` String DEFAULT 'NULL' COMMENT '编号1',
`repo` String DEFAULT 'NULL' COMMENT '编号2',
A VARCHAR(30),
PRIMARY KEY(A)
) ENGINE =
ReplicatedMergeTree('/clickhouse/tables/{shard}/db_test_S2RAB/TEST_0A_LOCAL','{replica}')
--ReplicatedMergeTree('/clickhouse/tables/{database}/{table}/{shard}', '{replica}')
--ReplicatedMergeTree('/clickhouse/tables/db_test_S2RAB/db_test_S2RAB/{shard}', '{replica}')
ORDER BY(A) SETTINGS index_granularity = 8192;
CREATE TABLE IF NOT EXISTS db_test_S2RAB.TEST_0A ON CLUSTER 'ck_cluster' AS
db_test_S2RAB.TEST_0A_LOCAL ENGINE = Distributed(ck_cluster,db_test_S2RAB,TEST_0A_LOCAL,rand());
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '1');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '2');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '3');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '4');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '5');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '6');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '7');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '8');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '9');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '10');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '11');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '12');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '13');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '14');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '15');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '16');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '17');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '18');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '19');
INSERT INTO db_test_S2RAB.TEST_0A(id, repo, A)VALUES('a', 'b', '20');
SELECT * FROM db_test_S2RAB.TEST_0A;
SELECT * FROM db_test_S2RAB.TEST_0A_LOCAL;
SELECT COUNT(1) FROM db_test_S2RAB.TEST_0A;
SELECT COUNT(1) FROM db_test_S2RAB.TEST_0A_LOCAL;
查询分布式表:
SELECT * FROM db_test_S2RAB.TEST_0A;
|id |repo|A |
|---|----|---|
|a |b |14 |
|a |b |15 |
|a |b |16 |
|a |b |19 |
|a |b |4 |
|a |b |8 |
|a |b |17 |
|a |b |14 |
|a |b |15 |
|a |b |16 |
|a |b |19 |
|a |b |4 |
|a |b |8 |
|a |b |12 |
|a |b |10 |
|a |b |11 |
|a |b |5 |
|a |b |6 |
|a |b |7 |
|a |b |9 |
某个节点查询本地表:
SELECT * FROM db_test_S2RAB.TEST_0A_LOCAL;
|id |repo|A |
|---|----|---|
|a |b |14 |
|a |b |15 |
|a |b |16 |
|a |b |19 |
|a |b |4 |
|a |b |8 |