学习资料https://blog.51cto.com/ygqygq2/1973653

Mycat是一个彻底开源面向企业应用开发的大数据库集群软件。

环境:mysql5.6 ip192.168.56.101 root/123456 (密码必须为这个,mycat文件默认定义)
创建3个数据库为下面做实验用
mysql> create database db1;
mysql> create database db2;
mysql> create database db3;

安装Mycat
wget https://raw.githubusercontent.com/MyCATApache/Mycat-download/master/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
cd mycat/
cd bin
./mycat start
mysql -uroot -p -P8066 -h127.0.0.1 -DTESTDB
输入密码123456

设置mycat_home环境变量
vim /etc/profile
export MYCAT_HOME=/root/mycat
使环境变量生效
source /etc/profile

即可登录mycat。跟登录mysql一样的。
创建表和数据,表面上是创建在TESTDB上,时间是创建在mysql db1,db2,db3
mysql> create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(5000001,'Job',20160102,100,10);
mysql> insert into travelrecord(id,user_id,traveldate,fee,days) values(10000001,'Slow',20160103,100,10);

在mysql上面查看,果然被分配到3个不通的数据库了
mysql> select * from db1.travelrecord;
+----+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+----+---------+------------+------+------+
| 1 | Victor | 2016-01-01 | 100 | 10 |
+----+---------+------------+------+------+
1 row in set (0.00 sec)

mysql> select * from db2.travelrecord;
+---------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+---------+---------+------------+------+------+
| 5000001 | Job | 2016-01-02 | 100 | 10 |
+---------+---------+------------+------+------+
1 row in set (0.00 sec)

mysql> select * from db3.travelrecord;
+----------+---------+------------+------+------+
| id | user_id | traveldate | fee | days |
+----------+---------+------------+------+------+
| 10000001 | Slow | 2016-01-03 | 100 | 10 |
+----------+---------+------------+------+------+
1 row in set (0.00 sec)

为什么会被分到这3个数据库,是因为文件autopartition-long.txt 定义的
[root@minion conf]# cat /root/mycat/conf/autopartition-long.txt
.# range start-end ,data node index
.# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2

想看数据具体会被分配到哪里可以用explain路由分析
mysql>explain insert into travelrecord(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);

Mycat重要的配置文件
server.xml是Mycat服务器参数调整和用户授权的配置文件
schema.xml是逻辑库定义和表以及分片定义的配置文件
rule.xml是分片规则的配置文件