文章目录
1.MyCat
1.1.MyCat简介
MyCAT是一个数据库中间件。国产开源项目,前身是cobar项目。
1.2.Mycat对多数据库的支持
1.3.MyCAT架构
如图所示:MyCAT使用Mysql的通讯协议模拟成了一个Mysql服务器,所有能使用Mysql的客户端以及编程语言都能将MyCAT当成是Mysql Server来使用,不必开发新的客户端协议。
1.4.MyCat分库分表
垂直分割(分库):指按照业务将表进行分类,分布到不同的数据库上面,这样也就将数据或者说压力分担到不同的库上面,如下图:
把usian拆成3个库
水平分割(分表):一个表格的数据按照行分割到多个节点上,如图:
把tb_order表拆分到3个数据库里
典型的分片规则:
根据主键编号进行hash、求余,如图
1.5.MyCat下载与安装
1.5.1.下载mycat
github地址:https://github.com/MyCATApache
1.5.2.安装Mycat
1、把MyCat的压缩包上传到linux服务器
2、解压缩,得到mycat目录
[root@centos upload]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local
3、启动和关闭MyCat
进入mycat/bin,启动MyCat
启动命令:./mycat start
停止命令:./mycat stop
重启命令:./mycat restart
查看状态:./mycat status
注意:可以使用mysql的客户端直接连接mycat服务。默认服务端口为8066
2.Mycat分库分表和读写分离
2.1.需求
把表分片存储到三个数据节点上。
2.2.安装环境
mysql节点1环境
操作系统版本 : centos6.5
数据库版本 :mysql-5.6
数据库名 : db1
ip:192.168.25.134
mysql节点2环境
操作系统版本 :centos6.5
数据库版本 :mysql-5.6
数据库名 : db2
ip:192.168.25.135
mysql节点3环境
操作系统版本 :centos6.5
数据库版本 :mysql-5.6
数据库名 : db3
ip:192.168.25.136
mycat节点环境
操作系统版本 :centos6.5
mycat版本:1.6release
ip:192.168.25.137
2.4.MyCat重要概念
1、逻辑库(schema):一个包含了所有数据库的逻辑上的数据库
2、逻辑表(table):一个包含了所有表的逻辑上的表
3、数据主机(dataHost):数据库软件安装到哪个服务器上
4、数据节点(dataNode):服务器上的mysql
5、分片规则(rule):拆分规则
2.5. 配置schema.xml
2.5.1.Schema.xml介绍
Schema.xml作为MyCat中重要的配置文件之一,管理着MyCat的逻辑库、表、分片规则、DataNode以及DataHost。
2.5.2.Schema.xml配置
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="power_shop" checkSQLschema="false" sqlMaxLimit="100">
<table name="tb_content" dataNode="dn1,dn2,dn3" rule="crc32slot" />
<table name="tb_content_category" dataNode="dn1,dn2,dn3" rule="crc32slot1"/>
<table name="tb_item" dataNode="dn1,dn2,dn3" rule="crc32slot2" />
<table name="tb_item_cat" dataNode="dn1,dn2,dn3" rule="crc32slot3" />
<table name="tb_item_desc" dataNode="dn1,dn2,dn3" rule="crc32slot4" />
<table name="tb_item_param" dataNode="dn1,dn2,dn3" rule="crc32slot5" />
<table name="tb_item_param_item" dataNode="dn1,dn2,dn3" rule="crc32slot6" />
<table name="tb_order" dataNode="dn1,dn2,dn3" rule="crc32slot7" />
<table name="tb_order_item" dataNode="dn1,dn2,dn3" rule="crc32slot8" />
<table name="tb_order_shipping" dataNode="dn1,dn2,dn3" rule="crc32slot9" />
<table name="tb_user" dataNode="dn1,dn2,dn3" rule="crc32slot10" />
<table name="de_duplication" dataNode="dn1,dn2,dn3" rule="crc32slot11" />
<table name="local_message" dataNode="dn1,dn2,dn3" rule="crc32slot12" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" 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>
<writeHost host="hostM1" url="192.168.233.137:3306" user="root" password="1111">
<readHost host="hostS2" url="192.168.233.138:3306" user="root" password="1111" />
</writeHost>
</dataHost>
</mycat:schema>
2.6. 配置server.xml
2.6.1.server.xml介绍
server.xml几乎保存了所有mycat需要的系统配置信息。最常用的是在此配置用户名、密码及权限。
2.6.2.server.xml配置
<user name="root">
<property name="password">1111</property>
<property name="schemas">power_shop</property>
</user>
<user name="user">
<property name="password">1111</property>
<property name="schemas">power_shop</property>
<property name="readOnly">true</property>
</user>
2.7.配置rule.xml
2.7.1.分片规则
(1).auto-sharding-long 规则
以 500 万为单位,实现分片规则:
1-500 万保存在 db1 中, 500 万零 1 到 1000 万保存在 db2 中,1000 万零 1 到 1500 万保存在 db3 中.
(2).crc32slot 规则
在 CRUD 操作时,根据具体数据的 crc32 算法计算,数据应该保存在哪一个dataNode 中
2.7.2.rule.xml配置
1)<columns>id</columns>中推荐配置主键列
2)所有的 tableRule 只能使用一次。如果需要为多个表配置分片规则,那么需要在此重新定义该规则。
3) 要分片的数据库节点数量,必须指定,否则没法分片
<tableRule name="crc32slot1">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot2">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot3">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot4">
<rule>
<columns>item_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot5">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot6">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot7">
<rule>
<columns>order_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot8">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot9">
<rule>
<columns>order_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot10">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot11">
<rule>
<columns>tx_no</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot12">
<rule>
<columns>tx_no</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<function name="crc32slot" class="io.mycat.route.function.PartitionByCRC32PreSlot">
<property name="count">3</property><!-- 要分片的数据库数量,必须指定,否则没法分片 -->
</function>
2.8.测试
2.8.1.创建库
在主数据库中分别创建db1、db2、db3
2.8.2.创建表并插入数据
配置完毕后,重新启动mycat。
使用mysql客户端连接mycat,创建表并插入数据。
2.8.3.分表测试
2.8.4.项目测试
1、修改数据库url
spring:
application:
name: power-shop-item
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.233.139:8066/power_shop?characterEncoding=UTF-8
username: root
password: 1111
type: com.alibaba.druid.pool.DruidDataSource
2、测试