共计 8881 个字符,预计需要花费 23 分钟才能阅读完成。
本篇内容介绍了“MyCat 怎么分库分表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
项目环境:
192.168.8.30 mycat
192.168.8.31 node1
192.168.8.32 node2
192.168.8.33 node3
三个节点 MySQL 均为单实例
一、创建测试库
node1
create database testdb01;
create database testdb02;
create database testdb03;
node2
create database testdb13;
create database testdb14;
create database testdb15;
node3
create database testdb25;
create database testdb26;
create database testdb27;
二、配置 schema.xml
?xml version= 1.0 ?
!DOCTYPE mycat:schema SYSTEM schema.dtd
mycat:schema xmlns:mycat= http://io.mycat/
schema name= mycatdb checkSQLschema= false sqlMaxLimit= 100
table name= user03 dataNode= dn$1-3,dn$13-15,dn$25-27 rule= auto-sharding-long /table
/schema
!-- dataNode name= dn1$0-743 dataHost= localhost1 database= db$0-743
/ --
dataNode name= dn1 dataHost= node1 database= testdb01 /
dataNode name= dn2 dataHost= node1 database= testdb02 /
dataNode name= dn3 dataHost= node1 database= testdb03 /
dataNode name= dn4 dataHost= node1 database= testdb04 /
dataNode name= dn5 dataHost= node1 database= testdb05 /
dataNode name= dn6 dataHost= node1 database= testdb06 /
dataNode name= dn7 dataHost= node1 database= testdb07 /
dataNode name= dn8 dataHost= node1 database= testdb08 /
dataNode name= dn9 dataHost= node1 database= testdb09 /
dataNode name= dn10 dataHost= node1 database= testdb10 /
dataNode name= dn11 dataHost= node1 database= testdb11 /
dataNode name= dn12 dataHost= node1 database= testdb12 /
dataNode name= dn13 dataHost= node2 database= testdb13 /
dataNode name= dn14 dataHost= node2 database= testdb14 /
dataNode name= dn15 dataHost= node2 database= testdb15 /
dataNode name= dn16 dataHost= node2 database= testdb16 /
dataNode name= dn17 dataHost= node2 database= testdb17 /
dataNode name= dn18 dataHost= node2 database= testdb18 /
dataNode name= dn19 dataHost= node2 database= testdb19 /
dataNode name= dn20 dataHost= node2 database= testdb20 /
dataNode name= dn21 dataHost= node2 database= testdb21 /
dataNode name= dn22 dataHost= node2 database= testdb22 /
dataNode name= dn23 dataHost= node2 database= testdb23 /
dataNode name= dn24 dataHost= node2 database= testdb24 /
dataNode name= dn25 dataHost= node3 database= testdb25 /
dataNode name= dn26 dataHost= node3 database= testdb26 /
dataNode name= dn27 dataHost= node3 database= testdb27 /
dataNode name= dn28 dataHost= node3 database= testdb28 /
dataNode name= dn29 dataHost= node3 database= testdb29 /
dataNode name= dn30 dataHost= node3 database= testdb30 /
dataNode name= dn31 dataHost= node3 database= testdb31 /
dataNode name= dn32 dataHost= node3 database= testdb32 /
dataNode name= dn33 dataHost= node3 database= testdb33 /
dataNode name= dn34 dataHost= node3 database= testdb34 /
dataNode name= dn35 dataHost= node3 database= testdb35 /
!-- dataNode name= dn4 dataHost= sequoiadb1 database= SAMPLE /
dataNode name= jdbc_dn1 dataHost= jdbchost database= db1 /
dataNode name= jdbc_dn2 dataHost= jdbchost database= db2 /
dataNode name= jdbc_dn3 dataHost= jdbchost database= db3 / --
dataHost name= node1 maxCon= 1000 minCon= 10 balance= 1
writeType= 0 dbType= mysql dbDriver= native switchType= 1 slaveThreshold= 100
heartbeat select user() /heartbeat
!-- can have multi write hosts --
writeHost host= 192.168.8.31 url= 192.168.8.31:3306 user= root password= mysql /writeHost
/dataHost
dataHost name= node2 maxCon= 1000 minCon= 10 balance= 1
writeType= 0 dbType= mysql dbDriver= native switchType= 1 slaveThreshold= 100
heartbeat select user() /heartbeat
!-- can have multi write hosts --
writeHost host= 192.168.8.32 url= 192.168.8.32:3306 user= root password= mysql /writeHost
/dataHost
dataHost name= node3 maxCon= 1000 minCon= 10 balance= 1
writeType= 0 dbType= mysql dbDriver= native switchType= 1 slaveThreshold= 100
heartbeat select user() /heartbeat
!-- can have multi write hosts --
writeHost host= 192.168.8.33 url= 192.168.8.33:3306 user= root password= mysql /writeHost
/dataHost
/mycat:schema
三、配置 rule.xml
mycat:rule xmlns:mycat= http://io.mycat/
tableRule name= auto-sharding-long
rule
columns id /columns
algorithm autopartition-long-user03 /algorithm
/rule
/tableRule
function name= autopartition-long-user03 > 四、配置 autopartition-long-user03.txt
1-10=0
11-20=1
21-30=2
31-40=3
41-50=4
51-60=5
61-70=6
71-80=7
81-10000=8
五、配置 server.xml
user name= root defaultAccount= true
property name= password mysql /property
property name= schemas mycatdb /property
/user
六、启动 mycat
/usr/local/mycat/bin/mycat start
查看 mycat 日志
STATUS | wrapper | 2018/11/22 15:27:14 | -- Wrapper Started as Daemon
STATUS | wrapper | 2018/11/22 15:27:14 | Launching a JVM...
INFO | jvm 1 | 2018/11/22 15:27:14 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO | jvm 1 | 2018/11/22 15:27:16 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2018/11/22 15:27:16 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2018/11/22 15:27:16 |
INFO | jvm 1 | 2018/11/22 15:27:20 | MyCAT Server startup successfully. see logs in logs/mycat.log
七、登录 MySQL 并查看逻辑表
mysql use mycatdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql show tables;
+-------------------+
| Tables in mycatdb |
+-------------------+
| order01 |
| orderdetail01 |
| user01 |
| user02 |
| user03 |
+-------------------+
5 rows in set (0.00 sec)
mysql
mysql
mysql drop table if exists user03;
Query OK, 0 rows affected, 1 warning (0.23 sec)
mysql create table user03 (
- id int not null auto_increment,
- name varchar(64),
- primary key (id)
- );
Query OK, 0 rows affected (0.43 sec)
八、插入测试数据
共插入 50 条数据
insert into user03(id,name) values(1, steven
insert into user03(id,name) values(2, steven
insert into user03(id,name) values(3, steven
insert into user03(id,name) values(4, steven
insert into user03(id,name) values(5, steven
insert into user03(id,name) values(11, steven
insert into user03(id,name) values(12, steven
insert into user03(id,name) values(13, steven
insert into user03(id,name) values(14, steven
insert into user03(id,name) values(15, steven
insert into user03(id,name) values(21, steven
insert into user03(id,name) values(22, steven
insert into user03(id,name) values(23, steven
insert into user03(id,name) values(24, steven
insert into user03(id,name) values(25, steven
insert into user03(id,name) values(31, steven
insert into user03(id,name) values(32, steven
insert into user03(id,name) values(33, steven
insert into user03(id,name) values(34, steven
insert into user03(id,name) values(35, steven
insert into user03(id,name) values(41, steven
insert into user03(id,name) values(42, steven
insert into user03(id,name) values(43, steven
insert into user03(id,name) values(44, steven
insert into user03(id,name) values(45, steven
insert into user03(id,name) values(51, steven
insert into user03(id,name) values(52, steven
insert into user03(id,name) values(53, steven
insert into user03(id,name) values(54, steven
insert into user03(id,name) values(55, steven
insert into user03(id,name) values(61, steven
insert into user03(id,name) values(62, steven
insert into user03(id,name) values(63, steven
insert into user03(id,name) values(64, steven
insert into user03(id,name) values(65, steven
insert into user03(id,name) values(71, steven
insert into user03(id,name) values(72, steven
insert into user03(id,name) values(73, steven
insert into user03(id,name) values(74, steven
insert into user03(id,name) values(75, steven
insert into user03(id,name) values(81, steven
insert into user03(id,name) values(82, steven
insert into user03(id,name) values(83, steven
insert into user03(id,name) values(84, steven
insert into user03(id,name) values(85, steven
insert into user03(id,name) values(91, steven
insert into user03(id,name) values(92, steven
insert into user03(id,name) values(93, steven
insert into user03(id,name) values(94, steven
insert into user03(id,name) values(95, steven
九、数据验证
三个 node 只在 testdb01-03,testdb13-15,testdb25-27,所以除了这 9 个物理库之外,其他库查不到分片。
下面在三个 node 分别验证出分片信息:
node1
mysql select count(*) from testdb01.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb02.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb03.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
node2
mysql select count(*) from testdb13.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb14.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb15.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
node3
mysql select count(*) from testdb25.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb26.user03;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql select count(*) from testdb27.user03;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
“MyCat 怎么分库分表”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!
正文完