mycat

MYCAT
//////////////////////////////////////////////////////////
============================================================
1.安装: 下载tar.gz 解压 进入bin目录
http://code.taobao.org/svn/openclouddb/downloads/
2.启动 停止
mycat start/stop/restart/status/pause
============================================================
3.使用测试
================================
1.本地安装mysql 启动mysql
2.在mysql中创建三个数据库
CREATE database db1;
CREATE database db2;
CREATE database db3;
3.编辑mycat/conf/schema.xml
修改writeHost/readHost中的location,user,password的值符合你所采用的Mysql的连接信息
4.重启mycat (默认数据端口为8066,管理端口为9066)
5.命令运行:mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB
-utest -ptest -P8066 -DTESTDB固定,可以在navicat中配置
JDBC则将mysql的URL中的端口3306改为8066即可
访问MyCAT的用户账号和授权信息是在conf/server.xml文件中配置,
而MyCAT用来连接后端MySQL库的用户名密码信息则在conf/schema.xml中,这是两套完全独立的系统
6.以下操作都在5执行的命令行里执行
按照schema.xml中配置的employee表执行下语句

travelrecord根据ID主键的范围进行分片:
create travelrecord: create table travelrecord (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
这个ID就存放在分片0上了
insert into travelrecord (id,user_id,traveldate,fee,days) values(1,'wang','2014-01-05',510.5,3);

company表是根据规则auto-sharding-long(主键范围)进行分片
create table company(id int not null primary key,name varchar(100));
insert into company(id,name) values(1,'hp');
insert into company(id,name) values(2,'ibm');
insert into company(id,name) values(3,'oracle');

hotnews表用取摸的方式随机分配到dn1,dn2,dn3上
create table hotnews(id int  not null primary key ,title varchar(400) ,created_time datetime);
在分片1上 而Id为5,则到dn3上,5%3=2 ,即对应dn3的 index
insert into hotnews(id,title,created_time) values(1,'first',now());

employee表,是根据规则sharding-by-intfile (分片字段为sharding_id)进行分片
create table employee (id int not null primary key,name varchar(100),sharding_id int not null);
insert into employee(id,name,sharding_id) values(1,'leader us',10000);
insert into employee(id,name,sharding_id) values(2, 'me',10010);
insert into employee(id,name,sharding_id) values(3, 'mycat',10000);
insert into employee(id,name,sharding_id) values(4, 'mydog',10010);

7.发现mycat中employee并没有数据 数据按照schema.xml中配置的employee的分片规则分配到了db1 db2上 规则中没有配置db3

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">

<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" />

<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,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" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="localhost" url="localhost:3306" user="root"
password="111111">
<!-- can have multi read hosts -->
<!-- <readHost host="hostS1" url="localhost:3306" user="root" password="123456"/> -->
</writeHost>
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
</mycat:schema>

猜你喜欢

转载自samson870830.iteye.com/blog/2386969