记录一次ClickHouse数据迁移的过程

迁移的背景:
首先目前的集群是4节点,配置为:4分片1副本(也就是没有数据备份),然后其中一个节点由于服务器问太卡访问太慢,需要去掉该节点,再重新加一个节点,然后再把去掉的节点上的数据迁移到新的节点中,然后整个集群还是4个节点。
方案:
借助clickhouse-copier,这是官网提供的一个数据迁移的工具,它可以做整个集群迁移到另一个集群,也可以只把其中一个分片的数据迁移,也可以在集群内部做数据迁移,这次我们的背景是集群内部做迁移。
具体做法:

  • 首先我的想法是,先新增一个节点,就是集群由4节点变成5节点,当然了,相关的配置文件肯定要修改,主要的就是 metrika.xml,然后重启集群。

  • 然后就是把节点4的数据迁移到节点5,然后把节点4给去掉就行了。

  • 添加配置文件,首先是zookeeper.xml,放在服务器本地,如下:

    <yandex>
    			<logger>
    				<level>trace</level>
    				<size>100M</size>
    				<count>3</count>
    			</logger>
    			<zookeeper>
    				<node index="1">
    					<host>ck01</host>
    					<port>2181</port>
    				</node>
    
    				<node index="2">
    					<host>ck02</host>
    					<port>2181</port>
    				</node>
    				<node index="3">
    					<host>ck03</host>
    					<port>2181</port>
    				</node>
    			</zookeeper>
    		</yandex>
    
  • 在服务器本地先创建一个数据迁移任务的配置文件,task.xml,放在服务器本地,如下:
    具体配置是什么含义,都有注释,其中需要注意的是:sharding_key 这个配置一定要有,不然会报错找不到 sharding_key,所谓的sharding_key就是数据迁移到目标集群后如何做分片,因为我只迁移到一个节点,所以不需要分片策略,直接设置为随机 rand()。

    <yandex>
    			<remote_servers>
    				<!--原集群-->
    				<source_cluster>
    					<shard>
    						<internal_replication>false</internal_replication>
    						<replica>
    							<host>ck04</host>
    							<port>9000</port>
    						</replica>
    					</shard>
    				</source_cluster>
    				<!--目标集群-->
    				<destination_cluster>
    					<shard>
    						<internal_replication>false</internal_replication>
    						<replica>
    							<host>ck05</host>
    							<port>9000</port>
    						</replica>
    					</shard>
    				</destination_cluster>
    			</remote_servers>
    			
    			<!--最大工作线程-->
    			<max_workers>2</max_workers>
    			<!--源拉数据节点-->
    			<settings_pull>
    				<readonly>1</readonly>
    			</settings_pull>
    			<!--目标写数据节点-->
    			<settings_push>
    				<readonly>0</readonly>
    			</settings_push>
    			<!--上面的配置-->
    			<settings>
    				<connect_timeout>3</connect_timeout>
    				<!-- Sync insert is set forcibly, leave it here just in case. -->
    				<insert_distributed_sync>1</insert_distributed_sync>
    			</settings>
    			<!--需要同步的表,每个任务一个表-->
    			<tables>
    				<!--这个节点名称自定义,一个表一个-->
    				<table_SensorReading>
    					<!--源-->
    					<cluster_pull>source_cluster</cluster_pull>
    					<database_pull>test</database_pull>
    					<table_pull>SensorReading</table_pull>
    					<!--目标-->
    					<cluster_push>destination_cluster</cluster_push>
    					<database_push>test</database_push>
    					<table_push>SensorReading</table_push>
    					<engine>
    						ENGINE = MergeTree partition by toYYYYMMDD(eventDate) order by sensorId
    					</engine>
    					<sharding_key>rand()</sharding_key>
    					<!--<where_condition>ID != 0</where_condition>-->
    				</table_SensorReading>
    				<!--下一个表-->
    			</tables>
    		</yandex>
    
  • 需要把任务的配置文件,也就是task.xml上传到zk的节点上,在网上搜到的步骤都是:
    bin/zkCli.sh create /clickhouse/copytasks “”
    bin/zkCli.sh create /clickhouse/copytasks/test “”
    bin/zkCli.sh create /clickhouse/copytasks/test/description “cat task.xml
    但是在最后一步我怎么弄都没办法把文件传上去,最后只能借助zk客户端,手动把配置文件写到节点上。(如果有知道怎么通过 zkCli客户端上传的可以交流)

  • 执行任务:

    clickhouse-copier --daemon --config /home/msy/zookeeper.xml --task-path /clickhouse/copytasks/test --base-dir /tmp/copylogs
    

    这里我一开始是用非root用户执行的,但是查看报错日志后发现为:can not change /opt/… 文件,然后猜测是因为非root用户没有权限操作 /opt下的某个目录,然后迁移数据过程中又需要做这些操作,所以加上了sudo。最后发现执行成功,节点4的数据成功迁移到节点5上来了。

  • 然后把节点4去掉,也就是修改 metrika.xml 配置,然后重启集群。同时查询分布式表,能成功查到节点5上的本地表数据。迁移成功!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值