clickhouse三节点三分片双副本环绕部署


clickhouse安装部署

rpm安装过程以及注意事项

01节点 metrika.xml 配置文件信息

<?xml version="1.0"?>
<yandex>
    <remote_servers>
        <_3s2r_cluster> <!-- 集群名称-->
            <shard> <!--集群的第一个分片-->
				<weight>1</weight>
                <internal_replication>true</internal_replication>
                <!--该分片的第一个副本-->
                <replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_1</default_database>
                </replica>
				<!--该分片的第二个副本-->
				<replica>
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_1</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第二个分片-->
				<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_2</default_database>
                </replica>
				<!--该分片的第二个副本-->
				<replica>
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_2</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第三个分片-->
				<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_3</default_database>
                </replica>
				<!--该分片的第二个副本-->
				<replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
					<default_database>mycluster_shard_3</default_database>
                </replica>
            </shard>
        </_3s1r_cluster>
    </remote_servers>
    <zookeeper-servers>
        <node index="1">
            <host>foton1</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>foton2</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>foton3</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>
</yandex>


02节点 metrika.xml 配置文件信息

<?xml version="1.0"?>
<yandex>
    <remote_servers>
        <_3s2r_cluster> <!-- 集群名称-->
            <shard> <!--集群的第一个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <!--该分片的第一个副本-->
                <replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_1</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_1</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第二个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_2</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_2</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第三个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_3</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_3</default_database>
                </replica>
            </shard>
        </_3s1r_cluster>
    </remote_servers>
    <zookeeper-servers>
        <node index="1">
            <host>foton1</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>foton2</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>foton3</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>

	<macros>
		<shard>01</shard>
		<replica>cluster01-01-2</replica>
	</macros>
	<macros>
		<shard>03</shard>
		<replica>cluster01-02-1</replica>
	</macros>

</yandex>

03节点 metrika.xml 配置文件信息

<?xml version="1.0"?>
<yandex>
    <remote_servers>
        <_3s2r_cluster> <!-- 集群名称-->
            <shard> <!--集群的第一个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <!--该分片的第一个副本-->
                <replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_1</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_1</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第二个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton2</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_2</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_2</default_database>
                </replica>
            </shard>
            <shard> <!--集群的第三个分片-->
		<weight>1</weight>
                <internal_replication>true</internal_replication>
                <replica>   <!--该分片的第一个副本-->
                    <host>foton3</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_3</default_database>
                </replica>
		<!--该分片的第二个副本-->
		<replica>
                    <host>foton1</host>
                    <port>9002</port>
                    <user>default</user>
                    <password>xxx</password>
		    <default_database>mycluster_shard_3</default_database>
                </replica>
            </shard>
        </_3s1r_cluster>
    </remote_servers>
    <zookeeper-servers>
        <node index="1">
            <host>foton1</host>
            <port>2181</port>
        </node>
        <node index="2">
            <host>foton2</host>
            <port>2181</port>
        </node>
        <node index="3">
            <host>foton3</host>
            <port>2181</port>
        </node>
    </zookeeper-servers>

	<macros>
		<shard>01</shard>
		<replica>cluster01-02-2</replica>
	</macros>
	<macros>
		<shard>03</shard>
		<replica>cluster01-03-2</replica>
	</macros>
</yandex>

macros 实例信息编辑

01节点实例信息
在这里插入图片描述

02节点实例信息
在这里插入图片描述

03节点实例信息
在这里插入图片描述


查看结果

在这里插入图片描述


建表测试

在ck001服务器上通过客户端执行

create database mycluster_shard_1;
create database mycluster_shard_2;
create database mycluster_shard_3;

在ck002服务器上通过客户端执行

create database mycluster_shard_1;
create database mycluster_shard_2;
create database mycluster_shard_3;

在ck003服务器上通过客户端执行

create database mycluster_shard_1;
create database mycluster_shard_2;
create database mycluster_shard_3;

//在ck001上执行

create table mycluster_shard_1.tbl_rep( 
  c1 Int32, 
  mydate Date 
) engine = ReplicatedMergeTree('/clickhouse/tables/1/dtbl', '1', mydate, (mydate), 8192);

//在ck002上执行

create table mycluster_shard_1.tbl_rep ( 
  c1 Int32,
  mydate Date
) engine = ReplicatedMergeTree('/clickhouse/tables/1/dtbl', '2', mydate, (mydate), 8192);

//在ck002上执行

create table mycluster_shard_2.tbl_rep (
  c1 Int32,
  mydate Date
) engine = ReplicatedMergeTree('/clickhouse/tables/2/dtbl', '1', mydate, (mydate), 8192);

//在ck003上执行

create table mycluster_shard_2.tbl_rep (
  c1 Int32,
  mydate Date
) engine = ReplicatedMergeTree('/clickhouse/tables/2/dtbl', '2', mydate, (mydate), 8192);

//在ck003上执行

create table mycluster_shard_3.tbl_rep (
  c1 Int32,
  mydate Date
) engine = ReplicatedMergeTree('/clickhouse/tables/3/dtbl', '1', mydate, (mydate), 8192);

//在ck001上执行

create table mycluster_shard_3.tbl_rep (
  c1 Int32,
  mydate Date
) engine = ReplicatedMergeTree('/clickhouse/tables/3/dtbl', '2', mydate, (mydate), 8192);

//在ck001上执行

use default;
create table dtbl (
  c1 Int32,
  mydate Date
) engine=Distributed('mycluster', '', tbl_rep, rand());

insert into table dtbl values(2, '2019-04-19')
insert into table dtbl values(1, '2019-04-19')
insert into table dtbl values(3, '2019-04-19')
select * from dtbl;

执行结果如下:
三节点三分片双副本

首先查看分布式表,三分片
在这里插入图片描述

然后查看本地表数据,由于插入了3条数据,如下每条数据都有2个副本,分别散在各个节点上。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

ClickHouse 中进行分片副本的配置可以通过以下步骤实现: 1. 首先需要在 ClickHouse 集群中创建一个 ZooKeeper 集群。可以使用 Apache ZooKeeper 来实现。需要确保 ZooKeeper 在集群中的所有节点上都已经安装并正在运行。 2. 然后需要在 ClickHouse 集群中创建一个分布式表。可以使用以下命令来创建一个名为 my_table 的表: ``` CREATE TABLE my_table ( key UInt32, value String ) ENGINE = Distributed('my_cluster', default, my_table', rand()); ``` 其中,my_cluster 是 ClickHouse 集群中的名称,default 是默认分片键,rand() 是随机分片函数。 3. 接下来需要在 ClickHouse 集群中创建分片。可以使用以下命令来创建分片: ``` CREATE TABLE my_table_1 ( key UInt32, value String ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/my_table', '{replica}') PARTITION BY key ORDER BY key SETTINGS index_granularity = 8192; CREATE TABLE my_table_2 ( key UInt32, value String ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/my_table', '{replica}') PARTITION BY key ORDER BY key SETTINGS index_granularity = 8192; CREATE TABLE my_table_3 ( key UInt32, value String ) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/my_table', '{replica}') PARTITION BY key ORDER BY key SETTINGS index_granularity = 8192; ``` 其中,/clickhouse/tables 是 ClickHouse 集群中的默认表路径,{shard} 是分片的名称,{replica} 是副本的名称。 4. 最后需要在每个分片中创建副本。可以使用以下命令来创建副本: ``` ALTER TABLE my_table_1 ADD REPLICA 'node1:9000'; ALTER TABLE my_table_1 ADD REPLICA 'node2:9000'; ALTER TABLE my_table_1 ADD REPLICA 'node3:9000'; ALTER TABLE my_table_2 ADD REPLICA 'node2:9000'; ALTER TABLE my_table_2 ADD REPLICA 'node3:9000'; ALTER TABLE my_table_2 ADD REPLICA 'node1:9000'; ALTER TABLE my_table_3 ADD REPLICA 'node3:9000'; ALTER TABLE my_table_3 ADD REPLICA 'node1:9000'; ALTER TABLE my_table_3 ADD REPLICA 'node2:9000'; ``` 其中,node1、node2、node3 是 ClickHouse 集群中的节点名称,9000 是节点的端口号。 完成以上步骤后,ClickHouse 集群将会具有分片和每个分片副本
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值