前言
按套路这里应该先安利一下mycat,还是自行百度吧,留个网站附加一张原理图就很明了了。
网站:http://mycat.io/
mycat就可以简单理解为企业级mysql集群工具,基于阿里的Cobar发展而来的,用于解决数据存储和业务规模迅速增长情况下的数据瓶颈问题。
准备工作
建库建表
有3张表 users,item,item_detail (item 的子表) 和 3 个数据库 db01,db02,db03
表 users 存储在数据库 db01, 表 item 和 item_detail 分布存储在数据 db01 和 db02
现在在3个数据库都创建相同的表
create database db01;
create database db02;
create database db03;
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL default '',
indate DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE item (
id INT NOT NULL AUTO_INCREMENT,
value INT NOT NULL default 0,
indate DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE item_detail (
id INT NOT NULL AUTO_INCREMENT,
value INT NOT NULL default 0,
name varchar(50) NOT NULL default '',
item_id INT NOT NULL,
PRIMARY KEY (id),
key (item_id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
mycat安装
下载mycat.tar包Mycat-server-1.4-RELEASE-20150901112004-linux.tar.gz
解压
tar -xzvf Mycat-server-1.4-RELEASE-20150901112004-linux.tar.gz
mv mycat /usr/local
配置home
vim /etc/profile
export MYCAT_HOME=/usr/localmycat
查看mycat结构,修改配置
2018-01-26_113623.pngbin 可执行文件
conf 配置文件
lib 依赖包
logs 日志文件
修改配置
图片.pngserver.xml
【server.xml】是MyCAT对外的“虚拟数据库”配置文件。所谓的“虚拟数据库”是说,MyCAT将多个Mysql集群整合起来对外提供服务,提供服务的接口仍然采用Mysql的形式,因此,通过仿造Mysql接口,让调用程序以为自己是在访问Mysql数据库,就是所谓的“虚拟数据库”。
添加一个账户,账号为root,密码为123,并且其schema值为lvfangTest
<user name="test">
<property name="password">test</property>
<property name="schemas">TESTDB</property>
</user>
<user name="root">
<property name="password">123</property>
<property name="schemas">lvfangTest</property>
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
2018-01-26_114126.png
同样我们要添加对等的schema:lvfangTest,其配置在schema.xml文件中配置,
schema.xml
【schema.xml】中主要配置数据库连接信息和表的分片规则
[root@hadoop2005 conf]# vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<!--添加lvfangTestd的schema配置 -->
<schema name="lvfangTest" checkSQLschema="false" sqlMaxLimit="100">
<!-- users表为全局表 放在dn1上 -->
<table name="users" primaryKey="id" type="global" dataNode="dn1" />
<!-- item表存在dn2和dn3上,主键为id,分片规则算法为mod-long -->
<table name="item" primaryKey="id" dataNode="dn2,dn3" rule="mod-long">
<!-- 配置item_detail为item的子表,并配置主外键关系 -->
<childTable name="item_detail" primaryKey="id" joinKey="item_id" parentKey="id" />
</table>
</schema>
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<!-- auto sharding by id (long) -->
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<!-- global table is auto cloned to all defined data nodes ,so can join
with any table whose sharding node is in the same data node -->
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,dn3"
rule="mod-long" />
<!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
rule="mod-long" /> -->
<table name="employee" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile" />
<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
rule="sharding-by-intfile">
<childTable name="orders" primaryKey="ID" joinKey="customer_id"
parentKey="id">
<childTable name="order_items" joinKey="order_id"
parentKey="id" />
</childTable>
<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
parentKey="id" />
</table>
</schema>
<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
/> -->
<!-- 配置db01,db02,db03库-->
<dataNode name="dn1" dataHost="localhost1" database="db01" />
<dataNode name="dn2" dataHost="localhost1" database="db02" />
<dataNode name="dn3" dataHost="localhost1" database="db03" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- 数据库连接信息 -->
<writeHost host="hadoop2005" url="192.168.90.225:3306" user="root"
password="123">
</writeHost>
</dataHost>
</mycat:schema>
注意:如果分片规则中用到其他分片算法,需要去rule.xml中配置相应算法,如果有则不需要配置,直接引用即可
另外在配置数据库连接信息时,记得url要写ip切勿写localhost之类的
启动mycat并连接
开启关闭
mycat start
mycat stop
mycat restart
连接
mysql -uroot -p123 -hhadoop2005 -P8066 -DlvfangTest
-u用户
-p密码
-h主机
-P8066(mycat默认端口为8066)
-D链接的库(这里不是数据库中的物理库,而是mycat配置中的逻辑库,即用户下对应的schema值)
2018-01-26_120351.png
这里注意在操作时最好要切换
现在我们添加数据,看数据会不会根据我们的分片规则进行分表入库
insert into users(name,indate) values('lvfang',now());
insert into item(id,value,indate) values(1,100,now());
insert into item_detail(value,name,item_id) values('pad',40,1);
insert into item_detail(value,name,item_id) values('phone',50,1);
insert into item(id,value,indate) values(999,100,now());
insert into item_detail(value,name,item_id) values('pad',40,999);
insert into item_detail(value,name,item_id) values('phone',50,999);
2018-01-26_121143.png
去查看是否数据进行分库
mysql> select * from db01.users;
+----+--------+---------------------+
| id | name | indate |
+----+--------+---------------------+
| 2 | lvfang | 2018-01-25 20:11:09 |
+----+--------+---------------------+
1 row in set (0.00 sec)
mysql> select * from db02.users;
Empty set (0.00 sec)
mysql> select * from db03.users;
Empty set (0.02 sec)
mysql> select * from db01.item;
Empty set (0.00 sec)
mysql> select * from db02.item;
+-----+-------+---------------------+
| id | value | indate |
+-----+-------+---------------------+
| 999 | 100 | 2018-01-25 20:11:09 |
+-----+-------+---------------------+
1 row in set (0.00 sec)
mysql> select * from db03.item;
+----+-------+---------------------+
| id | value | indate |
+----+-------+---------------------+
| 1 | 100 | 2018-01-25 20:11:09 |
+----+-------+---------------------+
1 row in set (0.00 sec)
mysql> select * from db01.item_detail;
Empty set (0.00 sec)
mysql> select * from db02.item_detail;
+----+-------+------+---------+
| id | value | name | item_id |
+----+-------+------+---------+
| 3 | 0 | 40 | 999 |
| 4 | 0 | 50 | 999 |
+----+-------+------+---------+
2 rows in set (0.00 sec)
mysql> select * from db03.item_detail;
+----+-------+------+---------+
| id | value | name | item_id |
+----+-------+------+---------+
| 3 | 0 | 40 | 1 |
| 4 | 0 | 50 | 1 |
+----+-------+------+---------+
2 rows in set (0.00 sec)
mysql>
2018-01-26_121820.png
我们可以看到,成功
网友评论