mysql数据迁移到mycat_Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)...

一、使用zookeeper管理远程Mycat配置文件

环境准备:

虚拟机192.168.152.130:

虚拟机192.168.152.128:

本机:

搭建好zookeeper的客户端工具ZooInspector ,具体参考文章Zookeeper数据查看工具ZooInspector简介

1. 启动ZK

./zkServer.sh start

be770ec099be7afc51e3cc0e49bbfae4.png

2. 修改/usr/local/mycat/conf/myid.properties

vim /usr/local/mycat/conf/myid.properties

loadZk=true

zkURL=192.168.152.130:2181clusterId=mycat-cluster-1myid=mycat_fz_01

clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_04

#server booster ; booster installon db same server,will reset all minCon to 1type=server

boosterDataHosts=dn2,dn3

myid.properties配置说明:

loadZk:默认值false。代表mycat集群是否使用ZK,true表示使用

zkURL:zk集群的地址

clusterId:mycat集群名字

myid:当前的mycat服务器名称

clusterNodes:把所有集群中的所有mycat服务器罗列进行以逗号隔开,比如:clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_03

3. 使用Mycat脚本初始化mycat在ZK中的节点数据

/usr/local/mycat/bin/init_zk_data.sh

50f16b29a4f7bd98157474b00865e089.png

2178c03e566b8dc6161fe2e68d11eb80.png

4. 启动mycat

/usr/local/mycat/bin/mycat start

52f28d9a17aa177816d3f2cdb8cfa6fa.png

5. 连接mycat,查看当前数据库中表的情况

mysql -uroot -pdigdeep -P8066 -h192.168.152.128

use TESTDB;

show tables;

8d79fd8a4be2de492456c8eef3a8ca0e.png

6. 使用zookeeper的客户端工具ZooInspector连接zookeeper修改mycat配置文件

6.1 进入目录E:\software\devtools\ZK\ZooInspector\build,运行zookeeper-dev-ZooInspector.jar;

9357f537786f42f981d62a8268856182.png

java -jar zookeeper-dev-ZooInspector.jar

连接远程zookeeper

32e8446ffdc5a7b519d431a6157fe308.png

连接成功以后可以看到mycat的树节点和mycat的安装后的conf目录下的schema.xml和rule.xml文件是对应的

bf25690942e80e74ed5e77ba56aa973b.png

6.2 在zookeeper管理工具里面动态修改schema配置文件新增一张逻辑表travelrecord2

823d074af058976b1c345b4540e22348.png

保存修改,查看mycat的schema.xml配置文件,可以看到修改已经同步过来了

3a55434f6821cd35bb3b0e6797a73b8b.png

重启mycat,可以看到TESTDB里面有新配置的travelrecord2表

/usr/local/mycat/bin/mycat restart

60cc5ab8732925522473c7cd225e5031.png

二、Mycat监控

1. 下载Mycat-web上传到虚拟机中

http://dl.mycat.io/mycat-web-1.0/

48fa5f4f2f98475405e8926024350a7d.png

fd6f224d10399092b6bdfedb749cd0d2.png

2. 解压Mycat-web

tar -zxvf Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz

2a084fd4bbb095eca79f4e5c7be0ccfb.png

3 修改mycat-web项目下/WEB-INF/classes/mycat.properties中zookeeper属性

vim /software/mycat-web/mycat-web/WEB-INF/classes/mycat.properties

#Mon Jan 16 15:37:36 CST 2012

show.period=3000000

zookeeper=192.168.152.130:2181

mycat_warn_mail=[{"cc"\:"sohudo@mycat.io","index"\:1,"mangerPort"\:"465","smtpHost"\:"smtp.139.com","smtpPassword"\:"123456","smtpProtocol"\:"smtp","smtpUser"\:"agile_louie@139.com","to"\:"9183838@qq.com"}]

##sql\u4E0A\u7EBF\u76F8\u5173\u914D\u7F6E

sqlonline.server=192.168.80.128

sqlonline.user=root

sqlonline.passwd=123456

4. 启动mycat-web

/software/mycat-web/start.sh

d889a855ed2feaa20ac8d2a6b3da41f2.png

5. 访问mycat-web

访问地址:http://192.168.152.128:8082/mycat/

52141f97d80485bcc7580151fa520dbb.png

6. 在Mycat管理界面手动初始化Mycat配置

6.1 Mycat-配置管理。增加mycat服务器配置

29300c4723d8ffd1eb7768b221b73adf.png

保存,新增成功

9749bd5f00344d5ca54150604e8d08a9.png

6.2 mycat-VM管理。配置JMX服务项

03005a4ed31b4cc6abdf43cb18cfa406.png

保存新增成功

b3ed20d9feb8e4d0303db3162d32721e.png

6.3 MySQL配置管理

323d3dcdd761ff7b60ad7bb322997c8d.png

700e865a00da8d5efdbce3ee4af6f1e8.png

分别保存,新增成功

3bbb1a7dbf17705048ccaa6be3ab19fd.png

初始化完前面的三步,就可以对Mycat进行监控了

三、Mycat数据迁移(扩容)

以employee表作数据迁移

1. 准备工作:

1)修改employee表的分片规则为一致性hash,vim schema.xml

2)修改分布式全局唯一id方式为本地文件方式,vim server.xml

0

3)关掉ZK配置,vim myid.properties

loadZk=false

zkURL=192.168.152.130:2181

clusterId=mycat-cluster-1

myid=mycat_fz_01

clusterNodes=mycat_fz_01,mycat_fz_02,mycat_fz_04

#server booster ; booster install on db same server,will reset all minCon to 1

type=server

boosterDataHosts=dn2,dn3

4) 重启mycat

../bin/mycat restart

5) 连接mycat,并创建employee表,并插入数据

mysql -uroot -pdigdeep -P8066 -h192.168.152.128

67a7cf37b722c96b6f1946a7d0a0376a88d.jpg

8803b6ea68c2caf950af80f3a8d8ef7e2ed.jpg

useTESTDB;create tableemployee(

idbigint(20) not null primary keyauto_increment,

namevarchar(20),

sharding_idbigint(20),

company_idbigint(20)

);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack001', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack002', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack003', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack004', 10010, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack005', 10010, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack006', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack007', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack008', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack009', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack010', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack011', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack012', 10010, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack013', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack014', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack015', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack016', 10000, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack017', 10010, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack018', 10000, 3);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack019', 10010, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack021', 10000, 1);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack022', 10010, 2);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack023', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack024', 10000, 4);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack025', 10000, 5);insert into employee(id, name, sharding_id, company_id) values(next value for MYCATSEQ_GLOBAL, 'Jack026', 10010, 3);

View Code

可以看到插入了25条数据到employee里面

50f7240ab0c0466e93e68d14a57bed07.png

然后在主库(192.168.152.130)的两个分片dn1(db1),dn2(db2)上查看employee的数据

a08787bcac6e74a19325984b5e79b36d.png

64a4437548b1e3b857532eaf7d2f09f5.png

可以看到dn1上有15条数据,dn2上有10条数据

2. 开始做数据迁移

当前employee表的数据分布在dn1,dn2,迁移后要分布在dn1,dn2,dn3

1)生成一个新的schema文件,文件名newSchema.xml,同时修改dataNode="dn1,dn2,dn3"(新的分片定义)

cp schema.xml newSchema.xml

vim newSchema.xml

以前的schema.xml文件:

8355316517b95c483fc2801580ce6ae5.png

新的newSchema.xml:

439909215813cb96d28d41d02b383878.png

2)生成一个新的rule文件,文件名newRule.xml,同时修改分片数为3(新的分片规则)

cp rule.xml newRule.xml

vim newRule.xml

以前的rule.xml:

c37f32810d1b38602a83b82a79d8194b.png

新的newRule.xml:

83440f093cdd3d6ca95938ee3758cf55.png

3)修改/usr/local/mycat/conf/migrateTables.properties文件。写明要迁移的schema和表,多张表用逗号隔开

vim migrateTables.properties

#schema1=tb1,tb2,...

#schema2=all(写all或者不写将对此schema下拆分节点变化的拆分表全部进行重新路由)

#...

#sample

#TESTDB=travelrecord,company,goods

TESTDB=employee

4)停止mycat(也可以不停止mycat)

5)执行dataMigrate.sh开始迁移(扩容)。

[root@centos1 bin]# ./dataMigrate.sh

51ba26b02adfa53a5d7f170702e982b1.png

遇到的错误1:

开始执行./dataMigrate.sh 的时候报没有适合的驱动

No suitable driver found for jdbc:mysql://192.168.152.130:3306/db1 java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.152.130:3306/db1

解决方法1:

下载jar包mysql-connector-java-5.1.6.jar,然后上传到/usr/local/mycat/lib

d2a13f85541a9fc5160c166bd621d678.png

查看主库(192.168.152.130)三个分片dn1(db1),dn2(db2),dn3(db3)上employee表的数据

1703c7b65009131cdcc779b90e68113f.png

054f701a47fd13a6c9fb01116247b195.png

71c87b1de75f88de21fb532a493e36e9.png

可以看到数据迁移成功

6)修改newRule.xml成为rule.xml,修改newSchema.xml为schema.xml。重启mycat

./bin/mycat restart

连接mycat,查看employee表的数据

mysql -uroot -pdigdeep -P8066 -h192.168.152.128

19c3a5dd061b37562254d941cdd1ea60.png

参考文章:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值