MyCat

一、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、求余,如图

 二、MyCat的安装

2.1 下载mycat

官方网站:MyCat2

github地址:https://github.com/MyCATApache

 2.2 安装mycat

①把MyCat的压缩包上传到linux服务器

②解压缩,得到mycat目录

[root@centos upload]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/loca

③启动和关闭MyCat

进入mycat/bin,启动MyCat
启动命令:./mycat start
停止命令:./mycat stop
重启命令:./mycat restart
查看状态:./mycat status

注意:可以使用mysql的客户端直接连接mycat服务。默认服务端口为8066

三、Mycat分库分表和读写分离

3.1 需求

把表分片存储到三个数据节点上。

3.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

3.3 MyCat重要概念

1、逻辑库(schema):一个包含了所有数据库的逻辑上的数据库

2、逻辑表(table):一个包含了所有表的逻辑上的表

3、数据主机(dataHost):数据库软件安装到哪个服务器上

4、数据节点(dataNode):服务器上的mysql

5、分片规则(rule):拆分规则

3.4. 配置schema.xml

3.4.1.Schema.xml介绍

Schema.xml作为MyCat中重要的配置文件之一,管理着MyCat的逻辑库、表、分片规则、DataNode以及DataHost。

3.4.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>

3.5. 配置server.xml

3.5.1.server.xml介绍

server.xml几乎保存了所有mycat需要的系统配置信息。最常用的是在此配置用户名、密码及权限。

3.5.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>

3.6.配置rule.xml

3.6.1.分片规则

3.6.1.1.auto-sharding-long 规则

以 500 万为单位,实现分片规则:
1-500 万保存在 db1 中, 500 万零 1 到 1000 万保存在 db2 中,1000 万零 1 到 1500 万保存在 db3 中.

3.6.1.2.crc32slot 规则

在 CRUD 操作时,根据具体数据的 crc32 算法计算,数据应该保存在哪一个dataNode 中

3.6.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>

3.7.测试

3.7.1 创建库

在主数据库中分别创建db1、db2、db3

3.7.2 创建表并插入数据

配置完毕后,重新启动mycat。

使用mysql客户端连接mycat,创建表并插入数据。

 3.7.3 分表测试

3.7.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. 测试

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值