首页 > 代码库 > Windows环境Mycat数据库分库分表中间件部署
Windows环境Mycat数据库分库分表中间件部署
下载地址MYCAT官方网站
jdk安装配置
- 首先去oracle官网下载并安装jdk8,添加环境变量,JAVA_HOME设置为D:\Worksoftware\Java\jdk1.8
- CLASSPATH设置为.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
- path系统变量追加%JAVA_HOME%\bin;
Mycat安装配置
-
首先添加Windows环境变量,MYCAT_HOME设置为安装目录E:\WorkSoftWare\MycatServer1.5
-
为了降低资源占用,mycat的jvm设置在startup_nowrap.bat,打开后可以清楚看到如下配置:
“%JAVA_CMD%” -server -Xms1G -Xmx2G -XX:MaxPermSize=64M -XX:+AggressiveOpts -XX:MaxDirectMemorySize=1G -DMYCAT_HOME=%MYCAT_HOME% -cp “..\conf;..\lib*” io.mycat.MycatStartup
这里将-Xms1G改成-Xms512M,-Xmx2G改成-Xmx1024M,保存后重新启动即可. - wrapper.conf文件里的改成wrapper.java.command=E:\WorkSoftWare\jdk1.8\bin\java.exe
Mycat启动的配置
schema.xml配置文件,如果分库在不同的服务器,可以配置两个datahost;如果在一个datahost中配置多个writeHost,则为主从配置。type=”global”时,为全局表
1 <?xml version="1.0"?> 2 <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> 3 <mycat:schema xmlns:mycat="http://org.opencloudb/" > 4 5 <!--在这一行参数里面,schema name定义了可以在MyCAT前端显示的逻辑数据库的名字,checkSQLschema这个参数为False的时候,表明MyCAT会自动忽略掉表名前的数据库名,比如说mydatabase1.test1,会被当做test1;sqlMaxLimit指定了SQL语句返回的行数限制--> 6 <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> 7 <!-- 主键范围规则 --> 8 <!-- 这一行代表在MyCAT前端会显示哪些表名,类似几行都代表一样的意思,这里强调的是表,而MyCAT并不会在配置文件里面定义表结构,如果在前端使用show create table ,MyCAT会显示正常的表结构信息,观察Debug日志,可以看到,MyCAT把命令分发给了dn1代表的数据库,然后把dn1的查询结果返回给了前端 可以判断,类似的数据库级别的一些查询指令,有可能是单独分发给某个节点,然后再把某个节点的信息返回给前端。 9 dataNode的意义很简单,这个逻辑表的数据存储在后端的哪几个数据库里面rule代表的是这个逻辑表students的具体切分策略,目前MyCAT只支持按照某一个特殊列,遵循一些特殊的规则来切分,如取模,枚举等,具体的留给之后细说 10 --> 11 <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> 12 13 <table name="company" primaryKey="ID" dataNode="dn3,dn2,dn1" rule="mod-long"/> 14 <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" /> 15 <!--求模分片随机规则 --> 16 <table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,dn3" 17 rule="mod-long" /> 18 19 <table name="employee" primaryKey="ID" dataNode="dn1,dn2" 20 rule="sharding-by-intfile" /> 21 <table name="customer" primaryKey="ID" dataNode="dn1,dn2" 22 rule="sharding-by-intfile"> 23 <!-- 24 childtable我在测试中并没有实际用起来不过在MyCAT的设计文档里面有提到,childtable是一种依赖于父表的结构, 25 这意味着,childtable的joinkey会按照父表的parentKey的策略一起切分,当父表与子表进行连接, 26 且连接条件是childtable.joinKey=parenttable.parentKey时,不会进行跨库的连接. 27 --> 28 <childTable name="orders" primaryKey="ID" joinKey="customer_id" 29 parentKey="id"> 30 <childTable name="order_items" joinKey="order_id" 31 parentKey="id" /> 32 </childTable> 33 <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" 34 parentKey="id" /> 35 </table> 36 37 <!-- 全局表是自动克隆到所有定义的数据节点,这样可以与拆分节点的任何表连接查询,是在同一个数据节点--> 38 <table name="news_table" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" /> 39 </schema> 40 41 <dataNode name="dn1" dataHost="localhost1" database="mycatDB1" /> 42 <dataNode name="dn2" dataHost="localhost1" database="mycatDB2" /> 43 <dataNode name="dn3" dataHost="localhost1" database="mycatDB3" /> 44 45 <!-- 46 dataHost配置的是实际的后端数据库集群,大部分参数简单易懂,这里就不一个个介绍了,只介绍比较重要的两个参数,writeType和balance. 47 --> 48 <!-- writeType和balance是用来控制后端集群的读写分离的关键参数,这里我用了双主双从的集群配置 49 这里的测试过程比较麻烦,所以直接贴结论: 50 1.balance=0时,读操作都在localhost上(localhost失败时,后端直接失败) 51 2.balance=1时,读操作会随机分散在localhost1和两个readhost上面(localhost失败时,写操作会在localhost1,如果localhost1再失败,则无法进行写操作) 52 3.balance=2时,写操作会在localhost上,读操作会随机分散在localhost1,localhost1和两个readhost上面(同上) 53 4.writeType=0时,写操作会在localhost上,如果localhost失败,会自动切换到localhost1,localhost恢复以后并不会切换回localhost进行写操作 54 5.writeType=1时,写操作会随机分布在localhost和localhost1上,单点失败并不会影响集群的写操作,但是后端的从库会无法从挂掉的主库获取更新,会在读数据的时候出现数据不一致 55 举例:localhost失败了,写操作会在localhost1上面进行,localhost1的主从正常运行,但是localhost的从库无法从localhost获取更新,localhost的从库于其他库出现数据不一致 56 --> 57 <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" 58 writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> 59 <heartbeat>select user()</heartbeat> 60 <!-- can have multi write hosts --> 61 <writeHost host="hostM1" url="localhost:3306" user="root" 62 password="123456"> 63 <!-- can have multi read hosts --> 64 <!--<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />--> 65 </writeHost> 66 </dataHost> 67 68 </mycat:schema>
server.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 3 - you may not use this file except in compliance with the License. - You 4 may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 5 - - Unless required by applicable law or agreed to in writing, software - 6 distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 7 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 8 License for the specific language governing permissions and - limitations 9 under the License. --> 10 <!DOCTYPE mycat:server SYSTEM "server.dtd"> 11 <mycat:server xmlns:mycat="http://org.opencloudb/"> 12 <system> 13 <property name="defaultSqlParser">druidparser</property> 14 <!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议--> 15 <!-- <property name="processorBufferChunk">40960</property> --> 16 <!-- 17 <property name="processors">1</property> 18 <property name="processorExecutor">32</property> 19 --> 20 <!--默认是65535 64K 用于sql解析时最大文本长度 --> 21 <!--<property name="maxStringLiteralLength">65535</property>--> 22 <!--<property name="sequnceHandlerType">0</property>--> 23 <!--<property name="backSocketNoDelay">1</property>--> 24 <!--<property name="frontSocketNoDelay">1</property>--> 25 <!--<property name="processorExecutor">16</property>--> 26 <!-- 27 <property name="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序 28 <property name="mutiNodePatchSize">100</property> 亿级数量排序批量 29 <property name="processors">32</property> <property name="processorExecutor">32</property> 30 <property name="serverPort">8066</property> <property name="managerPort">9066</property> 31 <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> 32 <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --> 33 </system> 34 <user name="test"><!--用户名--> 35 <property name="password">test</property><!--密码--> 36 <!--实例名,和schema.xml定义的schema对应,这里的实例名是虚拟名,也就是对mycat服务的一种别名,是应用程序以及客户端连接的入口。--> 37 <property name="schemas">TESTDB</property> 38 </user> 39 40 <user name="user"> 41 <property name="password">user</property> 42 <property name="schemas">TESTDB</property> 43 <property name="readOnly">true</property> 44 </user> 45 <!-- 46 <quarantine> 47 <whitehost> 48 <host host="127.0.0.1" user="mycat"/> 49 <host host="127.0.0.2" user="mycat"/> 50 </whitehost> 51 <blacklist check="false"></blacklist> 52 </quarantine> 53 --> 54 55 </mycat:server>
rule.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 3 - you may not use this file except in compliance with the License. - You 4 may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 5 - - Unless required by applicable law or agreed to in writing, software - 6 distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 7 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 8 License for the specific language governing permissions and - limitations 9 under the License. --> 10 <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> 11 <mycat:rule xmlns:mycat="http://org.opencloudb/"> 12 <tableRule name="rule1"> 13 <rule> 14 <columns>id</columns> 15 <algorithm>func1</algorithm> 16 </rule> 17 </tableRule> 18 19 <tableRule name="rule2"> 20 <rule> 21 <columns>user_id</columns> 22 <algorithm>func1</algorithm> 23 </rule> 24 </tableRule> 25 26 <tableRule name="sharding-by-intfile"> 27 <rule> 28 <columns>sharding_id</columns> 29 <algorithm>hash-int</algorithm> 30 </rule> 31 </tableRule> 32 <tableRule name="auto-sharding-long"> 33 <rule> 34 <columns>id</columns> 35 <algorithm>rang-long</algorithm> 36 </rule> 37 </tableRule> 38 <tableRule name="mod-long"> 39 <rule> 40 <columns>id</columns> 41 <algorithm>mod-long</algorithm> 42 </rule> 43 </tableRule> 44 <tableRule name="sharding-by-murmur"> 45 <rule> 46 <columns>id</columns> 47 <algorithm>murmur</algorithm> 48 </rule> 49 </tableRule> 50 <tableRule name="sharding-by-month"> 51 <rule> 52 <columns>create_date</columns> 53 <algorithm>partbymonth</algorithm> 54 </rule> 55 </tableRule> 56 <tableRule name="latest-month-calldate"> 57 <rule> 58 <columns>calldate</columns> 59 <algorithm>latestMonth</algorithm> 60 </rule> 61 </tableRule> 62 63 <tableRule name="auto-sharding-rang-mod"> 64 <rule> 65 <columns>id</columns> 66 <algorithm>rang-mod</algorithm> 67 </rule> 68 </tableRule> 69 70 <tableRule name="jch"> 71 <rule> 72 <columns>id</columns> 73 <algorithm>jump-consistent-hash</algorithm> 74 </rule> 75 </tableRule> 76 77 <function name="murmur" 78 class="org.opencloudb.route.function.PartitionByMurmurHash"> 79 <property name="seed">0</property><!-- 默认是0 --> 80 <property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --> 81 <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 --> 82 <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 --> 83 <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 84 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --> 85 </function> 86 <function name="hash-int" 87 class="org.opencloudb.route.function.PartitionByFileMap"> 88 <property name="mapFile">partition-hash-int.txt</property> 89 </function> 90 <function name="rang-long" 91 class="org.opencloudb.route.function.AutoPartitionByLong"> 92 <property name="mapFile">autopartition-long.txt</property> 93 </function> 94 <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod"> 95 <!-- how many data nodes --> 96 <property name="count">3</property> 97 </function> 98 99 <function name="func1" class="org.opencloudb.route.function.PartitionByLong"> 100 <property name="partitionCount">8</property> 101 <property name="partitionLength">128</property> 102 </function> 103 <function name="latestMonth" 104 class="org.opencloudb.route.function.LatestMonthPartion"> 105 <property name="splitOneDay">24</property> 106 </function> 107 <function name="partbymonth" 108 class="org.opencloudb.route.function.PartitionByMonth"> 109 <property name="dateFormat">yyyy-MM-dd</property> 110 <property name="sBeginDate">2015-01-01</property> 111 </function> 112 113 <function name="rang-mod" class="org.opencloudb.route.function.PartitionByRangeMod"> 114 <property name="mapFile">partition-range-mod.txt</property> 115 </function> 116 117 <function name="jump-consistent-hash" class="org.opencloudb.route.function.PartitionByJumpConsistentHash"> 118 <property name="totalBuckets">3</property> 119 </function> 120 </mycat:rule>
Mycat连接mysql示例
cmd命令运行:
结果:
Mycat连接mysql结果
上图上面的mycatdb1,mycatdb2,mycatdb3数据库是真实物理数据库,TESTDB是中间件逻辑数据库,此时已经所有工作都准备好。
闲的蛋疼,随便研究一些好东西,.net环境太封闭,每个.net程序员都要扩展视野,技术交流,本人QQ827937686
Windows环境Mycat数据库分库分表中间件部署
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。