環(huán)境說明
操作系統(tǒng):ubuntu16.04
節(jié)點1:
192.168.18.61:ubuntu61
部署服務:clickhouserver(cs01-01)和 clickhouserver(cs01-02)
節(jié)點2:
192.168.18.63:ubuntu63
部署服務:clickhouserver(cs02-01)和 clickhouserver(cs02-02)
節(jié)點3:
192.168.18.62:ubuntu62
部署服務:zookeeper
cat /etc/hosts
192.168.18.61 ubuntu61
192.168.18.62 ubuntu62
192.168.18.63 ubuntu63
每個節(jié)點分別安裝ClickHouse
略
單節(jié)點服務多實例
復制配置文件:
sudo cp /etc/clickhouse-server/config.xml /etc/clickhouse-server/config9001.xml
sudo cp /etc/clickhouse-server/user.xml /etc/clickhouse-server/user9001.xml
sudo cp /etc/systemd/system/clickhouse-server.service /etc/systemd/system/clickhouse-server9001.service
chmod 777 /etc/clickhouse-server/config9001.xml
chown clickhouse:clickhouse /etc/clickhouse-server/config9001.xml
chown clickhouse:clickhouse /etc/clickhouse-server/user9001.xml
修改配置:
<log>/var/log/clickhouse-server/clickhouse-server9001.log</log>
<errorlog>/var/log/clickhouse-server/clickhouse-server9001.err.log</errorlog>
<http_port>8124</http_port>
<tcp_port>9001</tcp_port>
<mysql_port>9014</mysql_port>
<postgresql_port>9015</postgresql_port>
<interserver_http_port>9019</interserver_http_port>
<interserver_http_host>ubuntu61</interserver_http_host>
<listen_host>::</listen_host>
<path>/var/lib/clickhouse9001/</path>
<tmp_path>/var/lib/clickhouse9001/tmp/</tmp_path>
<user_files_path>/var/lib/clickhouse9001/user_files/</user_files_path>
<users_xml>
<path>users9001.xml</path>
</users_xml>
<local_directory>
<path>/var/lib/clickhouse9001/access/</path>
</local_directory>
注意:
每個節(jié)點填寫各自的hostname
<interserver_http_host>ubuntu61</interserver_http_host>
修改啟動服務:
vi /etc/systemd/system/clickhouse-server9001.service
ExecStart=/usr/bin/clickhouse-server --config=/etc/clickhouse-server/config9001.xml --pid-file=/run/clickhouse-server/clickhouse-server9001.pid
啟動服務:
systemctl start clickhouse-server9001
systemctl status clickhouse-server9001
集群配置
vi /etc/clickhouse-server/config.xml
<remote_servers>
<cluster_3s_1r>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>ubuntu61</host>
<port>9000</port>
<user>default</user>
<password>123456</password>
</replica>
<replica>
<host>ubuntu63</host>
<port>9001</port>
<user>default</user>
<password></password>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>ubuntu63</host>
<port>9000</port>
<user>default</user>
<password></password>
</replica>
<replica>
<host>ubuntu61</host>
<port>9001</port>
<user>default</user>
<password></password>
</replica>
</shard>
</cluster_3s_1r>
</remote_servers>
<zookeeper>
<node>
<host>ubuntu62</host>
<port>2181</port>
</node>
</zookeeper>
<macros>
<layer>01</layer>
<shard>01</shard>
<replica>cluster01-01-1</replica>
</macros>
每個節(jié)點的配置文件區(qū)別:
ubuntu61分片1、副本1
<macros>
<layer>01</layer>
<shard>01</shard>
<replica>cluster01-01-1</replica>
</macros>
ubuntu61分片2抗悍、副本2
<macros>
<layer>01</layer>
<shard>02</shard>
<replica>cluster01-02-2</replica>
</macros>
ubuntu63分片2艰山、副本1
<macros>
<layer>01</layer>
<shard>02</shard>
<replica>cluster01-02-1</replica>
</macros>
ubuntu63分片1、副本2
<macros>
<layer>01</layer>
<shard>01</shard>
<replica>cluster01-01-2</replica>
</macros>
重啟服務后款青,查看集群配置是否成功
clickhouse-client --port 9000 --password 123456
select * from system.clusters;
測試集群
1纲酗、分別登陸每個實例client辩涝,執(zhí)行命令贸伐,創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE monchickey;
show databases;
2、每個實例怔揩,創(chuàng)建復制表
CREATE TABLE monchickey.image_label ( label_id UInt32, label_name String, insert_time Date) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-01/image_label','cluster01-01-1',insert_time, (label_id, insert_time), 8192);
CREATE TABLE monchickey.image_label ( label_id UInt32, label_name String, insert_time Date) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-02/image_label','cluster01-02-2',insert_time, (label_id, insert_time), 8192);
CREATE TABLE monchickey.image_label ( label_id UInt32, label_name String, insert_time Date) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-02/image_label','cluster01-02-1',insert_time, (label_id, insert_time), 8192);
CREATE TABLE monchickey.image_label ( label_id UInt32, label_name String, insert_time Date) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-01/image_label','cluster01-01-2',insert_time, (label_id, insert_time), 8192);
3捉邢、創(chuàng)建分布式表
分布式表只是作為一個查詢引擎,本身不存儲任何數(shù)據(jù)商膊,查詢時將sql發(fā)送到所有集群分片伏伐,然后進行進行處理和聚合后將結果返回給客戶端
不需要在每個節(jié)點上都創(chuàng)建,按實際需要在指定的節(jié)點行創(chuàng)建即可
use monchickey;
CREATE TABLE image_label_all AS image_label ENGINE = Distributed(cluster_3s_1r, monchickey, image_label, rand());
4晕拆、插入數(shù)據(jù)藐翎,進行驗證
在節(jié)點1,分片1上執(zhí)行插入
use monchickey;
INSERT INTO monchickey.image_label (label_id,label_name,insert_time) VALUES (1,'aaa','2021-07-13');
INSERT INTO monchickey.image_label (label_id,label_name,insert_time) VALUES (2,'bbb','2021-07-13');
此時在其他分片節(jié)點上進行查詢实幕,能看到數(shù)據(jù)吝镣,說明集群搭建成功
在其他節(jié)點分片上進行插入
INSERT INTO monchickey.image_label (label_id,label_name,insert_time) VALUES (3,'ccc','2021-07-14');
INSERT INTO monchickey.image_label (label_id,label_name,insert_time) VALUES (4,'ddd','2021-07-15');
INSERT INTO monchickey.image_label (label_id,label_name,insert_time) VALUES (5,'eee','2021-07-16');
查看結果,觀察數(shù)據(jù)變化
select * from image_label;
select * from image_label_all;