mycat的分库分表

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

mycat的分库分表
1、什么是分库分表
2、分库分表的⼤致分配
3、Linux下配置mycat服务器
1、什么是分库分表?
在同⼀个系统(项⽬)中,把⼀张数据⽐较多的表拆分到不同的集群节点上,
这种模式叫做分库分表
2、分库分表的实现:
使⽤mycat实现分库分表(HDFS:namenode datanode secondarynamdenode)
在整个mycat中也会⽤到namenode和datanode
(1)先配置⼀台mycat服务器(192.168.23.164)
(2)然后配置两台mysql服务器:mysql01(192.168.23.129)和mysql02(192.168.23.130)
在mysql01服务器下有⼀个teach数据库,在teach下有⼀个student表
在mysql02服务器下有⼀个teach数据库,在teach下有⼀个student表
当使⽤mycat把进⾏存储是,数据会⾃动往mysql01或mysql02中存储.
3、配置mycat
3.1 规定Linux的⽤户名和全名不能叫mycat,否则mycat不会⽣效(原因是影响了Linux系统的环境变量) 3.2 vim /etc/sysconfig/network也不能叫mycat
3.3 mycat使⽤的是Java语⾔编写-->所以必须要在Linux上配置jdk
(要求jdk是64位,因为mycat是在64位的jdk8 上进⾏编译的)
3.4 配置jdk
3.5 使⽤xftp把mycat的压缩包上传到Linux服务器上(/home/apps/)
3.6 解压mycat压缩包
tar -zxvf xxxx
3.7 配置mycat的环境变量(与jdk的环境变量配置⼀样)
3.8 启动mycat(两种⽅式)
(1)进⼊mycat的bin⽬录: ./mycat console(推荐使⽤)
或者在其他的地⽅的⽬录:mycat console
(2)./mycat start
⼀旦启动mycat就关不掉,kill -9 进程号也关不掉,除⾮重启或者关机,
据说可以使⽤Ctrl+C
3.9 下⾯就是三个核⼼配置⽂件了(最重要的地⽅):
所有的读写分离/分库分表都必须要在这三个配置⽂件完成
在mycat的conf⽬录下:
server.xml:配置了mycat的⽤户
rule.xml:mycat的规则
schema.xml:实现了读写分离和分库分表的具体配置
server:
<?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 - - /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="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班⼀致性检测、0为关闭 -->
<property name="sequnceHandlerType">2</property>
<!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>-->
<!--必须带有MYCATSEQ_或者 mycatseq_进⼊序列匹配流程注意MYCATSEQ_有空格的情况-->
<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
<property name="subqueryRelationshipCheck">false</property> <!-- ⼦查询中存在关联查询的情况下,检查关联字段中是否有分⽚字段 .默认 false -->
<!-- <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 | type 2 NettyBufferPool -->
<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">0</property>
<!--
单位为m
-->
<property name="memoryPageSize">64k</property>
<!--
单位为k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
单位为m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采⽤zookeeper协调切换 -->
<property name="useZKSwitch">false</property>
<!-- XA Recovery Log⽇志路径 -->
<!--<property name="XARecoveryLogBaseDir">./</property>-->
<!-- XA Recovery Log⽇志名称 -->
<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
<!--如果为 true的话严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接-->
<property name="strictTxIsolation">false</property>
<property name="useZKSwitch">true</property>
</system>
<!--
从system标签开始到结束中的内容配置了
连接最⼤连接数最⼩连接数连接超时时间隔离机制事务切换....
相当于在mybatis中配置数据库的最⼤最⼩连接数连接超时时间
-->
<!-- 全局SQL防⽕墙设置 -->
<!--⽩名单可以使⽤通配符%或着*-->
<!--例如<host host="127.0.0.*" user="root"/>-->
<!--例如<host host="127.0.*" user="root"/>-->
<!--例如<host host="127.*" user="root"/>-->
<!--例如<host host="1*7.*" user="root"/>-->
<!--这些配置情况下对于127.0.0.1都能以root账户登录-->
<!--
<firewall>
<whitehost>
<host host="1*7.0.0.*" user="root"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
-->
<!--
整个user标签中就是配置了mycat的账号和密码
name===》就是mycat的账号
defaultAccount====》表⽰该账号为管理员账号,⽽且只能出现⼀次
property:属性标签
password===》就是name的密码
⽆论是账号还是密码都可以随意定制,开⼼就好
readOnly:只读(该账号只能查询数据,不能写⼊数据)
schemas:指向的是mycat的数据库(mycat的虚拟库这个库并不是真实存在的,是需要通过mysql的数据库进⾏映射的),⼀个mycat中可以有多个schema 隐藏mysql的真实库的名字
连接mysql:
jdbc:mysql:localhost:3306/数据库名
连接mycat:
jdbc:mysql:ip地址:端⼝号/mycat的虚拟库名
schemas中的配置和mysql的数据库可以⼀样,也可以不⼀样
-->
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">TEACH</property>
</user>
</mycat:server>
rule.xml:
<?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 - - /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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<!--
所有的规则配置⼀个tableRule对应⼀个function,如果需要进⾏配置必须要配置⼀对,不能单独配置⼀个(如果单独配置了⼀个的情况下,mycat启动的时候会直接报错)⼀共有⼗种规则:
homework1:
百度把这10种规则全部弄明⽩,然后整理⼀份⽤⾃⼰语⾔描述的电⼦⽂档
mod-long:(轮询)
mysql01
mysql数据库
teach
student
mysql02
mysql数据库
teach
student
当客户端发送请求===插⼊数据===》mycat===请求mysql===》会把第⼀条数据插⼊进mysql01,
会把第⼆条数据插⼊进mysql02
依次循环
-->
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!--
代表了datanode的节点数量
如果只有两台datanode就必须要写2
-->
<property name="count">2</property>
</function>
</mycat:rule>
schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!--
schema标签的name属性值必须要和server.xml中的schames保持⼀致
schema就类似于mysql的数据库
checkSQLschema:检查SQL语句是否错误(select * from user wher id = 1)
默认值为false,不需要改为true,把对SQL语句的检查交给mysql处理
sqlMaxLimit:最⼤执⾏SQL语句的数量
⼀次性可以执⾏多少条SQL语句
在schame标签中有table标签,这个标签就是定义了mycat中的表
类似于mysql数据库中的表
和mysql中的表名可以重复也可以不⼀致
dataNode:数据节点
在HDFS中存放数据
在mycat中映射的就是真实mysql数据库(可以有多个,也可以只有⼀个),名字可以随意起
今天的⽬标是实现分库分表,需要⽤到两台mysql数据库,所以只需要配置dn1和dn2
rule:配置规则
配置⼀定要和rule.xml中保持⼀致
-->
<schema name="TEACH" checkSQLschema="false" sqlMaxLimit="100">
<!-- auto sharding by id (long) -->
<table name="mycat_student" dataNode="dn1,dn2" rule="mod-long" />
</schema>
<!--
datanode标签对应的是table标签中的datanode属性
也就是说datanode属性值必须要和dataNode的标签name属性值保值⼀致
dn1:代表了mysql01服务器==》mysql数据库
dn2:代表了mysql02服务器==》mysql数据库
dataHost:才是真正的配置了分库分表和读写分离的核⼼配置
database:映射的是真实mysql的数据库名
-->
<dataNode name="dn1" dataHost="localhost1" database="mysql01" />
<dataNode name="dn2" dataHost="localhost2" database="mysql02" />
<!--
dataHost标签必须要和dataNode标签中的dataHost属性保持⼀致
如果dataHost属性名重复了,映射的是同⼀个dataHost
每⼀个dataHost标签就是⼀个真实的mysql数据库
maxCon:最⼤连接数
minCon:最⼩连接数
homework2:
把balance,writeType,switchType弄明⽩,并且⽤⾃⼰的语⾔描述每个值代表什么意思,记录电⼦⽂档 balance:负载均衡
writeType:写⼊的类型(读写分离使⽤)
switchType:转换的类型(读写分离所使⽤的数据)
dbType:数据库的类型(在最早的版本mycat只⽀持mysql⼀种数据库,后来发展⽀持很多种数据库)
dbDriver:数据库驱动(使⽤本地驱动,使⽤的是mycat所提供的驱动)
slaveThreshold:主从复制时延
master:主节点
slave:从节点
threshold:⼊⼝
-->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!--
heartbeat:⼼跳
mycat⼀直在监视mysql的user表(⼼跳),如果⼀旦发现mysql的user不再有⼼跳,就会认为这⼀台mysql处于宕机状态,也就不会再向这⼀台mysql进⾏做增删改查的操作 -->
<heartbeat>select user()</heartbeat>
<!--
writeHost:标签是该台mysql数据库为读和写的数据库
readHost:标签是该台mysql数据库为读的数据库
writeHost如果单独存在,标识该台mysql数据库为读和写的操作
如果⼀旦和readHost⼀起使⽤,writeHost就只能做些的操作,所有的读操作都使⽤readHost完成
readHost必须要配置在writeHost的内容,不允许单独存在
host:属性只是标识了该台数据库的操作的内容,可以随意定义
url:数据库的url
jdbc:mysql://localhost:3306
如果配置了两个writeHost,那么host的值就必须不能重复
-->
<writeHost host="write01" url="192.168.23.129:3306" user="root"
password="root" />
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="write02" url="192.168.23.130:3306" user="root"
password="root" />
</dataHost>
<!--
mycat中所映射的真实mysql的表必须要mycat所声明的table的名字保持⼀致
也就是说mycat的表名必须要和mysql的表名⼀模⼀样,否则映射不到
-->
</mycat:schema>
配置完成:
进⼊到Navicat中,先新建连接,分别连接mysql01和mysql02,然后分别在mysql01和mysql02
中分别建⽴两个数据库(库名可以不⼀致),再分别建⽴mycat_student表,
然后建⽴mycat01的连接,(在建⽴连接时,端⼝号为8066,即通讯端⼝号,不能使⽤9066,为管理端⼝号)
在mycat01中会有⼀个TEACH数据库,在库中有⼀个mycat_student表,
这是在配置⽂件中的配置⽣效了。

⾄此,配置结束。

使⽤Java代码进⾏实现:只需要修改数据源就⾏了(只⽤连接mycat01数据库)
当进⾏存储的时候,由于我们使⽤的是轮询规则,数据会⾃动轮流存储到mysql01和mysql02.。

相关文档
最新文档