赞
踩
1.先将mycat服务通过./mycat stop停掉
2.在slave服务中配置三个数据库,这三个数据库中都创建t_order表
3.配置配置文件 schema.xml和server.xml
4.跳转bin目录下通过./mycat start 开启mycat,查看进程ps -ef | grep mycat
5.这个时候mysql的mycat中的t_order已经同步
6.当我们在mycat的表中添加一条数据时,id会生成一个20位的时间戳
7.id通过配置的count数进行取模,放到对应的数据库中
8.idea中进行测试
指定主键生成策略
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- - you may not use this file except in compliance with the License. - You
- may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - - Unless required by applicable law or agreed to in writing, software -
- distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
- License for the specific language governing permissions and - limitations
- under the License. -->
- <!DOCTYPE mycat:server SYSTEM "server.dtd">
- <mycat:server xmlns:mycat="http://io.mycat/">
- <system>
- <property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
- <property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
- <!-- <property name="sequnceHandlerType">2</property>
- 指定使用Mycat全局序列的类型:
- 0为本地文件方式,1为数据库方式,2为时间戳序列方式
- 对于读写分离而言,是不需要考虑主键生成方式的,也就是不需要配置全局序列号的
- -->
- <property name="sequnceHandlerType">2</property>
- <!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
- <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
- <!-- <property name="processorBufferChunk">40960</property> -->
- <!--
- <property name="processors">1</property>
- <property name="processorExecutor">32</property>
- -->
- <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
- <property name="processorBufferPoolType">0</property>
- <!--默认是65535 64K 用于sql解析时最大文本长度 -->
- <!--<property name="maxStringLiteralLength">65535</property>-->
- <!--<property name="sequnceHandlerType">0</property>-->
- <!--<property name="backSocketNoDelay">1</property>-->
- <!--<property name="frontSocketNoDelay">1</property>-->
- <!--<property name="processorExecutor">16</property>-->
- <!--
- <property name="serverPort">8066</property> <property name="managerPort">9066</property>
- <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
- <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
- <!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
- <property name="handleDistributedTransactions">0</property>
-
- <!--
- off heap for merge/order/group/limit 1开启 0关闭
- -->
- <property name="useOffHeapForMerge">1</property>
-
- <!--
- 单位为m
- -->
- <property name="memoryPageSize">1m</property>
-
- <!--
- 单位为k
- -->
- <property name="spillsFileBufferSize">1k</property>
-
- <property name="useStreamOutput">0</property>
-
- <!--
- 单位为m
- -->
- <property name="systemReserveMemorySize">384m</property>
-
-
- <!--是否采用zookeeper协调切换 -->
- <property name="useZKSwitch">true</property>
-
-
- </system>
-
- <!-- 全局SQL防火墙设置 -->
- <!--
- <firewall>
- <whitehost>
- <host host="127.0.0.1" user="mycat"/>
- <host host="127.0.0.2" user="mycat"/>
- </whitehost>
- <blacklist check="false">
- </blacklist>
- </firewall>
- -->
-
- <user name="mycat">
- <property name="password">mycat</property>
- <property name="schemas">mycatdb</property> <!--逻辑库-->
-
- <!-- 表级 DML 权限设置 -->
- <!--
- <privileges check="false">
- <schema name="TESTDB" dml="0110" >
- <table name="tb01" dml="0000"></table>
- <table name="tb02" dml="1111"></table>
- </schema>
- </privileges>
- -->
- </user>
-
- <user name="user">
- <property name="password">user</property>
- <property name="schemas">mycatdb</property>
- <property name="readOnly">true</property>
- </user>
-
- </mycat:server>

指定逻辑库,分片结点,结点主机等
- <?xml version="1.0"?>
- <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
- <mycat:schema xmlns:mycat="http://io.mycat/">
-
- <schema name="mycatdb" checkSQLschema="true" sqlMaxLimit="100">
- <!-- 要实现分库分表,那么就需要在<schema>标签下配置表了,现在是水平切分,表示要对哪张表进行切分 -->
- <table name="t_orders" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
- rule="mod-long" />
- <!-- rule="mod-long":切分规则(按照id进行切分),mod:取模的意思 -->
- </schema>
- <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
- /> -->
- <dataNode name="dn1" dataHost="localhost1" database="test002" />
- <dataNode name="dn2" dataHost="localhost1" database="test003" />
- <dataNode name="dn3" dataHost="localhost1" database="test004" />
- <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
- <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
- <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
- <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> -->
- <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
- writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
- <heartbeat>select user()</heartbeat>
- <!-- can have multi write hosts -->
- <writeHost host="hostM1" url="192.168.245.71:3306" user="root" password="12345678">
- </writeHost>
-
- </dataHost>
- </mycat:schema>

然后每个数据库都创建一张t_orders表
查看进程ps -ef | grep mycat
这个时候mysql的mycat中的t_order已经同步
这个时候呢我们可以去从库查看我们创建的那三个数据库,看这条数据存到了哪里
这条数据存放到了test004这个库里,而其他的库里没有,它只会存在一个数据库里边,这就是水平拆分的意义。通过id取模生成的数据,一个数据库存储两百万条数据的话,三个数据库去存放的数据量肯定是比一个数据库存储的多的多。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。