MySQL 部署分布式架构 MyCAT (四)

分片(水平拆分)

2.取模分片(mod-long)

cd /data/mycat/conf
cp schema.xml schema.xml.rang-long

vi schema.xml




        
    
    
        select user()
    
            
    
    
            
    
    
    
        select user()
    
            
    
    
            
    
    
 
 

vi rule.xml  

                
                2
        

# 创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"

# 重启mycat 
mycat restart 

# 插入数据
use TESTDB;
insert into t4(id,name) values(1,'a');
insert into t4(id,name) values(2,'b');
insert into t4(id,name) values(3,'c');
insert into t4(id,name) values(4,'d');

测试

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t4;"
+----+------+
| id | name |
+----+------+
|  2 | b    |
|  4 | d    |
+----+------+

mysql -S /data/3308/mysql.sock -e "use taobao;select * from t4;"
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  3 | c    |
+----+------+

3.枚举分片

cd /data/mycat/conf/
cp schema.xml schema.xml.mod

vi schema.xml




        
select user() select user()

vi rule.xml

      
            name
            hash-int
      


      partition-hash-int.txt
      1
      0


vi partition-hash-int.txt
bj=0 
sh=1
DEFAULT_NODE=1 

# 说明:columns 标识将要分片的表字段,algorithm 分片函数, 其中分片函数配置中,mapFile标识配置文件名称
# function 函数配置中,type默认值为0,0表示Integer,非零表示String。
# defaultNode 默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点, 默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点
# 如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到不识别的枚举值就会报错。

准备测试数据

mysql -S /data/3307/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
# 重启mycat 
mycat restart 
mysql -uroot -p123456 -h127.0.0.1 -P8066
use TESTDB
insert into t5(id,name) values(1,'bj');
insert into t5(id,name) values(2,'sh');
insert into t5(id,name) values(3,'bj');
insert into t5(id,name) values(4,'sh');
insert into t5(id,name) values(5,'tj');

检查

mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"
+----+------+
| id | name |
+----+------+
|  1 | bj   |
|  3 | bj   |
|  5 | tj   |
+----+------+

mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"
+----+------+
| id | name |
+----+------+
|  2 | sh   |
|  4 | sh   |
+----+------+

你可能感兴趣的:(MySQL 部署分布式架构 MyCAT (四))