mycat同机多库分表

一个逻辑表分到一台主机的多个数据库中。

建表

在一台主机上见3个数据库,在三个数据库中分别建立TongPerson表

USE `tongjifenku1`;

/*Table structure for table `TongPerson` */

DROP TABLE IF EXISTS `TongPerson`;

CREATE TABLE `TongPerson` (
  `Id` int(11) NOT NULL,
  `Name` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
  `UserId` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

USE `tongjifenku2`;

/*Table structure for table `TongPerson` */

DROP TABLE IF EXISTS `TongPerson`;

CREATE TABLE `TongPerson` (
  `Id` int(11) NOT NULL,
  `Name` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
  `UserId` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

USE `tongjifenku3`;

/*Table structure for table `TongPerson` */

DROP TABLE IF EXISTS `TongPerson`;

CREATE TABLE `TongPerson` (
  `Id` int(11) NOT NULL,
  `Name` varchar(50) COLLATE utf8_swedish_ci DEFAULT NULL,
  `UserId` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

配置server.xml

给root用增加逻辑库tongjifenbiao

<user name="root" defaultAccount="true">
       <property name="password">123456</property>
       <property name="schemas">Test1,dankufenbiao,tongjifenbiao</property>
</user>

配置scheam.xml

<schema name="tongjifenbiao" checkSQLschema="false" sqlMaxLimit="100">
        <table name="TongPerson"  dataNode="dn3,dn4,dn5" rule="tongjifenbiao-mod-long" />
</schema>
<dataNode name="dn3" dataHost="localhost3" database="tongjifenku1" />
<dataNode name="dn4" dataHost="localhost3" database="tongjifenku2" />
<dataNode name="dn5" dataHost="localhost3" database="tongjifenku3" />
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="0"  writeType="0" dbType="mysql" 
         dbDriver="native" switchType="1" slaveThreshold="100">
         <heartbeat>select user()</heartbeat>

         <writeHost host="hostS4" url="192.168.101.149:3306" user="root" password="123456" />
</dataHost>

配置rule.xml

<dataHost name="localhost3" maxCon="1000" minCon="10" balance="0"  writeType="0" dbType="mysql" 
         dbDriver="native" switchType="1" slaveThreshold="100">
         <heartbeat>select user()</heartbeat>

         <writeHost host="hostS4" url="192.168.101.149:3306" user="root" password="123456" />
</dataHost>
<function name="tongjifenbiao-mod-long" class="io.mycat.route.function.PartitionByMod">
         <!-- how many data nodes -->
        <property name="count">3</property>
</function>

验证

EXPLAIN INSERT INTO TongPerson (`Id`,`Name`,`UserId` ) VALUES(11,'2345623',11);
DATA_NODESQL
dn5INSERT INTO TongPerson (Id,Name,UserId ) VALUES(11,’2345623’,11)
EXPLAIN INSERT INTO TongPerson (`Id`,`Name`,`UserId` ) VALUES(11,'2345623',12);
DATA_NODESQL
dn3INSERT INTO TongPerson (Id,Name,UserId ) VALUES(11,’2345623’,12)
EXPLAIN INSERT INTO TongPerson (`Id`,`Name`,`UserId` ) VALUES(11,'2345623',13);
DATA_NODESQL
dn4INSERT INTO TongPerson (Id,Name,UserId ) VALUES(11,’2345623’,13)
EXPLAIN select * from TongPerson 
DATA_NODESQL
dn3SELECT * FROM TongPerson LIMIT 100
dn4SELECT * FROM TongPerson LIMIT 100
dn5SELECT * FROM TongPerson LIMIT 100
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值