mycat-range分片:
拆表在mycat中只需三步,1在schema.xml配置文件中添加一行代码:
<table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
代码含义:按照rule策略将表分拆给sh1和sh2两个分片里
2编写rule策略:vim /application/mycat/conf/autopartition-long.txt
3重启mycat
具体详细解释和操作过程如下:
例如上图的order表数据量极大,访问这张表的速度很慢,并且业务量也很大,经常需要读写操作,所以我们要将这个大表进行拆分
分片策略介绍:
range:例如把2000万行的表按照数字列的范围进行拆分
mod:取余,将5000万行数据用取余的方式分到4个分片里,任何数和4取余的值范围都是0,1,2,3
1/4余1,2/4余2,3/4余3,4/4余1,5/4余1。。。。。。以此类推
和节点数量取值,模值就是你要节点的编号
枚举:按照每个省进行取模,使用枚举类型的前提是列里面的个数是有限的个数
哈希:
时间:按照数据产生的时间进行分片,例如按月份进行分片
所有的分片都有一个编号,例如oldguo1和oldguo2:
myct会给这2个分片分配一个号码,第一个叫0号分片,第二个叫1号分片,
假如说有10行数据,将1-5行分给0,将6-10分给1分片,
操作过程:
1,在scheml配置文件中添加一行配置:
[root@db01 ~]# vim /application/mycat/conf/schema.xml
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> <table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" /> </schema> <dataNode name="sh1" dataHost="oldguo1" database= "taobao" /> <dataNode name="sh2" dataHost="oldguo2" database= "taobao" /> <dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3307" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3309" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3307" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3309" user="root" password="123" /> </writeHost> </dataHost> <dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1"> <heartbeat>select user()</heartbeat> <writeHost host="db1" url="10.0.0.201:3308" user="root" password="123"> <readHost host="db2" url="10.0.0.201:3310" user="root" password="123" /> </writeHost> <writeHost host="db3" url="10.0.0.202:3308" user="root" password="123"> <readHost host="db4" url="10.0.0.202:3310" user="root" password="123" /> </writeHost> </dataHost> </mycat:schema> |
解释:按照rule="auto-sharding-long" 策略,将t3表分配给sh1和sh2两个分片里 |
|
2修改分表策略文件:
1到5行分配给sh1这个0片,6到10分配给sh2这个1分片
[root@db01 ~]# vim /application/mycat/conf/autopartition-long.txt # range start-end ,data node index # K=1000,M=10000. #0-500M=0 #500M-1000M=1 #1000M-1500M=2 1-5=0 6-10=1 ~ |
3在两个节点中创建表并重启mycat进行测试:
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" [root@db01 ~]# mycat restart Stopping Mycat-server... Stopped Mycat-server. Starting Mycat-server... [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111) [root@db01 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.5-release-20180122220033 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use TESTDB 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_taobao | +------------------+ | t3 | | user | +------------------+ 2 rows in set (0.10 sec)
mysql> insert into t3 values(1); ERROR 1064 (HY000): partition table, insert must provide ColumnList mysql> insert into t3(id,name) values(1,'a'); Query OK, 1 row affected (2.24 sec)
mysql> insert into t3(id,name) values(2,'b'); Query OK, 1 row affected (0.11 sec)
mysql> insert into t3(id,name) values(3,'c'); Query OK, 1 row affected (0.07 sec)
mysql> insert into t3(id,name) values(7,'x'); Query OK, 1 row affected (0.87 sec)
mysql> insert into t3(id,name) values(8,'y'); Query OK, 1 row affected (0.12 sec)
mysql> insert into t3(id,name) values(9,'z'); Query OK, 1 row affected (0.03 sec)
mysql> select * from t3; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 7 | x | | 8 | y | | 9 | z | +----+------+ 6 rows in set (0.62 sec)
mysql> exit Bye [root@db01 ~]# mysql -S /data/3307/mysql.sock -e "select * from taobao.t3" +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ [root@db01 ~]# mysql -S /data/3308/mysql.sock -e "select * from taobao.t3" +----+------+ | id | name | +----+------+ | 7 | x | | 8 | y | | 9 | z | +----+------+ [root@db01 ~]# |