window环境下运行的,实际生产推荐在Linux上运行
使用前软件环境搭建
下载安装mysql:mysql-5.7.36-winx64
下载安装jdk-8u251-windows-x64
下载安装Mycat-server-1.3.0.3-release-20150527095523-win
create database yy01;
create database yy02;
create database yy03;
select 'yy01',a.* from yy01.item a union all
select 'yy02',a.* from yy02.item a union all
select 'yy03',a.* from yy03.item a ;
explain select * from item ;
DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
<system>
<property name="defaultSqlParser">druidparserproperty>
system>
<user name="test">
<property name="password">testproperty>
<property name="schemas">TESTDBproperty>
user>
<user name="user">
<property name="password">userproperty>
<property name="schemas">TESTDBproperty>
<property name="readOnly">trueproperty>
user>
mycat:server>
DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://org.opencloudb/">
<tableRule name="role1">
<rule>
<columns>idcolumns>
<algorithm>mod-longalgorithm>
rule>
tableRule>
<tableRule name="rule1">
<rule>
<columns>idcolumns>
<algorithm>func1algorithm>
rule>
tableRule>
<tableRule name="rule2">
<rule>
<columns>user_idcolumns>
<algorithm>func1algorithm>
rule>
tableRule>
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_idcolumns>
<algorithm>hash-intalgorithm>
rule>
tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>idcolumns>
<algorithm>rang-longalgorithm>
rule>
tableRule>
<tableRule name="mod-long">
<rule>
<columns>idcolumns>
<algorithm>mod-longalgorithm>
rule>
tableRule>
<tableRule name="sharding-by-murmur">
<rule>
<columns>idcolumns>
<algorithm>murmuralgorithm>
rule>
tableRule>
<function name="murmur" class="org.opencloudb.route.function.PartitionByMurmurHash">
<property name="seed">0property>
<property name="count">2property>
<property name="virtualBucketTimes">160property>
function>
<function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txtproperty>
function>
<function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txtproperty>
function>
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<property name="count">3property>
function>
<function name="func1" class="org.opencloudb.route.function.PartitionByLong">
<property name="partitionCount">8property>
<property name="partitionLength">128property>
function>
mycat:rule>
DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="users" primaryKey="id" dataNode="dn1,dn2,dn3" />
<table name="item" primaryKey="id" dataNode="dn1,dn2,dn3" rule="role1" />
<table name="users2" primaryKey="id" dataNode="dn1,dn2,dn3" rule="role1" />
<table name="orders" primaryKey="id" dataNode="dn1,dn2,dn3" rule="role1" />
schema>
<dataNode name="dn1" dataHost="localhost1" database="yy01" />
<dataNode name="dn2" dataHost="localhost1" database="yy02" />
<dataNode name="dn3" dataHost="localhost1" database="yy03" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()heartbeat>
<writeHost host="hostM1" url="localhost:3306" user="root"
password="123456">
writeHost>
dataHost>
mycat:schema>
验证结果
在命令行访问mycat逻辑数据库,采用如下的命令:
mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB
现在通过数据库查询数据库和表,发现只有逻辑数据库TESTDB而不是yy01, yy02, yy03;而且表也是统一显示的,而不是分布在不同的实际数据库中。参考图片如下:
在TESTDB库中创建item表用以测试
drop table if exists item ;
CREATE TABLE item (
id INT NOT NULL AUTO_INCREMENT,
d_name varchar(100) ,
d_date varchar(100) default '0000-00-00 00:00:00',
PRIMARY KEY (id)
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into item (id,d_name,d_date) values(01,'秦时明月',now());
insert into item (id,d_name,d_date) values(02,'秦时明月',now());
insert into item (id,d_name,d_date) values(03,'秦时明月',now());
insert into item (id,d_name,d_date) values(04,'秦时明月',now());
insert into item (id,d_name,d_date) values(05,'秦时明月',now());
insert into item (id,d_name,d_date) values(06,'秦时明月',now());
insert into item (id,d_name,d_date) values(07,'秦时明月',now());
insert into item (id,d_name,d_date) values(08,'秦时明月',now());
insert into item (id,d_name,d_date) values(09,'秦时明月',now());
insert into item (id,d_name,d_date) values(10,'秦时明月',now());
insert into item (id,d_name,d_date) values(11,'秦时明月',now());
insert into item (id,d_name,d_date) values(12,'秦时明月',now());
insert into item (id,d_name,d_date) values(13,'秦时明月',now());
select 'yy01',a.* from yy01.item a union all
select 'yy02',a.* from yy02.item a union all
select 'yy03',a.* from yy03.item a ;
explain select * from item ;