加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

Mysql系列五:数据库分库分表中间件mycat的安装和mycat配置详解

发布时间:2020-12-11 23:57:42 所属栏目:MySql教程 来源:网络整理
导读:?一、mycat的安装 环境准备: 准备一台虚拟机192.168.152.128 1. 下载mycat cd /softwarewget http: dl.mycat.io RELEASE Mycat server RELEASE linux.tar.gz 2. 解压mycat tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz ? 3. 剪切mycat到
4. rule.xml

定义分片规则策略文件。

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
    tableRule标签:定义table分片策略
</span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="rule1"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
    rule标签:策略定义标签
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        columns标签:对应的分片字段
        </span><span style="color: #008000"&gt;--></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        algorithm标签:tableRule分片策略对应的function名称
        </span><span style="color: #008000"&gt;--></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>func1<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="rule2"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>user_id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>func1<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="sharding-by-intfile"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>sharding_id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>hash-int<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="auto-sharding-long"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>rang-long<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="mod-long"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>mod-long<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="sharding-by-murmur"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>murmur<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="crc32slot"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>crc32slot<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="sharding-by-month"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>create_time<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>partbymonth<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="latest-month-calldate"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>calldate<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>latestMonth<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="auto-sharding-rang-mod"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>rang-mod<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;tableRule </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="jch"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>id<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;columns</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>jump-consistent-hash<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;algorithm</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;rule</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;tableRule</span><span style="color: #0000ff"&gt;></span>

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
function标签:定义分片函数
</span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="murmur"</span><span style="color: #ff0000"&gt;
    class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByMurmurHash"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="seed"</span><span style="color: #0000ff"&gt;></span>0<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span><span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 默认是0 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="count"</span><span style="color: #0000ff"&gt;></span>2<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span><span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 要分片的数据库节点数量,必须指定,否则没法分片 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="virtualBucketTimes"</span><span style="color: #0000ff"&gt;></span>160<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span><span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <property name="weightMapFile"&gt;weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <property name="bucketMapPath"&gt;/etc/mycat/bucketMapPath</property> 
        用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="crc32slot"</span><span style="color: #ff0000"&gt;
          class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByCRC32PreSlot"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="count"</span><span style="color: #0000ff"&gt;></span>2<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span><span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 要分片的数据库节点数量,必须指定,否则没法分片 </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="hash-int"</span><span style="color: #ff0000"&gt;
    class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByFileMap"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="mapFile"</span><span style="color: #0000ff"&gt;></span>partition-hash-int.txt<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="rang-long"</span><span style="color: #ff0000"&gt;
    class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.AutoPartitionByLong"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="mapFile"</span><span style="color: #0000ff"&gt;></span>autopartition-long.txt<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="mod-long"</span><span style="color: #ff0000"&gt; class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByMod"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; how many data nodes </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="count"</span><span style="color: #0000ff"&gt;></span>3<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="func1"</span><span style="color: #ff0000"&gt; class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByLong"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="partitionCount"</span><span style="color: #0000ff"&gt;></span>8<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="partitionLength"</span><span style="color: #0000ff"&gt;></span>128<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="latestMonth"</span><span style="color: #ff0000"&gt;
    class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.LatestMonthPartion"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="splitOneDay"</span><span style="color: #0000ff"&gt;></span>24<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="partbymonth"</span><span style="color: #ff0000"&gt;
    class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByMonth"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="dateFormat"</span><span style="color: #0000ff"&gt;></span>yyyy-MM-dd<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="sBeginDate"</span><span style="color: #0000ff"&gt;></span>2015-01-01<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="rang-mod"</span><span style="color: #ff0000"&gt; class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByRangeMod"</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="mapFile"</span><span style="color: #0000ff"&gt;></span>partition-range-mod.txt<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;function </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="jump-consistent-hash"</span><span style="color: #ff0000"&gt; class</span><span style="color: #0000ff"&gt;="io.mycat.route.function.PartitionByJumpConsistentHash"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="totalBuckets"</span><span style="color: #0000ff"&gt;></span>3<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;function</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"></<span style="color: #800000">mycat:rule<span style="color: #0000ff">>

rule是定义分片规则的

三、mycat工作原理

四、mycat基础概念

1. mycat支持的数据库

2. 逻辑库(Schema)

在实际应用中,业务开发人员并不需要知道中间件的存在,只需要知道数据库的概念,所以数据库中间件可以被看作是一个或者多个数据库集群构成的逻辑库。

3. 逻辑表

在分布式数据库中,读写数据的表就是逻辑表。逻辑表,可以是数据切分后分布在一个或多个分片库中,也可以不做数据切分不分片,只有一个表构成?

4. DataNode、DataHost、Rule

DataNode:数据切分后,一个大表被分到不同的分片数据库上面,每个分片所在的数据库就是分片节点

DataHost:数据切分后,每个分片节点(DataNode)不一定都会独占一台机器,同一机器上面可以有多个分片数据库。一个或多个分片节点(DataNode)所在的机器就是节点主机(DataHost)

Rule:分片规则,选择合适的分片规则很重要,将极大的避免后续处理数据的难度

5. myca分片原则

5.1 数据拆分之前的思考

分片规则虽然能解决大表对数据库系统的压力,但它并不是万能的,也有一些不利之处。因此首要问题是:

1)分不分库

2)分哪些库

3)什么规则分

4)分多少片

5.2 mycat数据切分原则

1)达到一定数量级才分,一般是单表达到800万数据就要考虑数据拆分了。

2)不到800万,但跟大表(超800万的表)有关联查询的表也要拆分,在此称为大表关联表

3)大表关联表如何拆分:小于100万的使用全局表;大于100万小于800万跟大表使用同样的拆分策略;无法跟大表使用相同规则的,可以考虑从java代码上分步骤查询,不用关联查询,或者破例使用全局表。

4)全局表:如品牌表250万,跟大表order关联,又无法跟大表使用同样的拆分策略,也做成了全局表。全局表必须满足的条件:没有太激烈的并发update(如多线程同时update同一条记录);虽有多线程update,但不是操作同一条记录;批量insert没问题。

5)拆分字段是不可修改的

6)拆分字段只能是一个字段,如果想按照两个字段拆分,必须新建一个冗余字段,冗余字段的值使用两个字段的值拼接而成(如大区+年月拼接成:“区域_年月日” 字段)

7)拆分算法的选择和合理性判断:按照选定的算法拆分后每个库中单表不得超过800万记录

8)能不拆分的尽量不拆分。如果某个表不跟其他表关联查询,数据量又少、直接不拆分,使用单库即可

5.3 Mycat分库分表原则

1)能不分就不分,1000万以内的表,不建议分片,通过合适的索引,读写分离等方式,可以很好的解决性能问题

2)分片数量尽量少,分片尽量均匀分布在多个DataHost上,因为一个查询SQL跨分片越多,则总体性能越差,虽然要好于所有数据在一个分片的结果,只在必要的时候进行扩容,增加分片数量。

3)分片规则需要慎重选择,分片规则的选择,需要考虑数据的增长模式,数据的访问模式,分片关联性问题,以及分片扩容问题,最近的分片策略为范围分片,枚举分片,一致性Hash分片,这几种分片都有利于扩容

4)尽量不要在一个事物中的SQL跨越多个分片,分布式事物一直是一个不好处理的问题

5)查询条件尽量优化,尽量避免select * 的方式,大量数据结果集下,会消耗大量的CPU资源和带宽,查询应尽量避免返回大的结果集,并且应尽量为频繁使用的查询语句建立索引

5.4 mycat常用分片规则

1)时间类:按天分片、自然月分片、单月小时分片

2)哈希类:Hash固定分片、日期范围Hash分片、截取数字Hash求模范围分片、截取数字Hash分片、一致性Hash分片

3)取模类:取模分片、取模范围分片、范围求模分片

4)其他类:枚举分片、范围约定分片、应用指定分片、冷热数据分片

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

?一、mycat的安装

环境准备:准备一台虚拟机192.168.152.128

1. 下载mycat

cd /softwarewget http:dl.mycat.ioRELEASEMycatserverRELEASElinux.tar.gz

2. 解压mycat

tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

?

3. 剪切mycat到/usr/local目录下

mv softwaremycat usrlocal

?

4. 启动mycat

usrlocalmycatbinusrlocalmycatbinmycat status 查看启动状态

说明:这里有个小插曲,启动一会mycat又会自己停止

通过查看日志

vim /usr/local/mycat/logs/wrapper.log

报:Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0

这是因为使用的是jdk8的版本,jdk8以后就没有永久代了,而mycat的wrapper.conf里面配置了jdk8以前的永久代的内存大小,我们只需要注释这个配置就可以了

重新启动mycat:

/usr/local/mycat/bin/mycat start

就不会出现启动一会又自己停止的问题了

连接mycat:

mysql -uroot -p123456 -h192.168.152.128 -P8066

可以看到mycat连接成功

5. linux下mycat常用命令

usrlocalmycatbinusrlocalmycatbinusrlocalmycatbinusrlocalmycatbinusrlocalmycatbinusrlocalmycatbinmycat status 查看启动状态

二、mycat配置详解

1. 首先查看mycat的文件目录

mycat的主要配置文件都在conf目录下,核心的配置文件是schema.xml、server.xml、rule.xml这3个配置文件,下面我们来对这3个配置文件进行详细介绍

2.?schema.xml

Mycat主要管理

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
schema标签:指定Mycat的逻辑库(mycat的Schema,可以指定多个schema标签)

checkSQLschema属性:
    False 过滤schema定义。
    select * from testdb.company;=> select * from company;
    True  不过滤schema定义,当我们把testdb输错了或者没有设置大小写敏感就会报错

sqlMaxLimit属性:
    Limit 自动加入limit,查询语句如果没有加入分页参数,当数据量很大的时候,mycat认为会有问题时就启用这个参数只返回100条。例如:select * from company。    
</span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;schema </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="TESTDB"</span><span style="color: #ff0000"&gt; checkSQLschema</span><span style="color: #0000ff"&gt;="false"</span><span style="color: #ff0000"&gt; sqlMaxLimit</span><span style="color: #0000ff"&gt;="100"</span><span style="color: #0000ff"&gt;></span> 
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 

    table标签:指定Mycat中的逻辑表。最后要做数据分片的表。mycat中的表,可以和实体有具体映射关系,也可以没有具体映射关系(如果没有映射关系会报错)。
    dataNode属性:数据节点,把相应的表存到对应的DB中。
    rule属性:分片规则。对应rule.xml中的规则。
    ruleRequired属性:指定该属性的表是否需要分片规则。True 必须制定,如果没有制定,就会报错。             primaryKey属性:值为具体的物理表的主键id,如果使用非主键进行分片,那么Mycat会缓存主键和具体dataNode的信息,如果下次再使用非主键进行查询的时候,就不用广播所有dn。
    type属性:逻辑表的类型。普通表和全局表,全局表不需要分片,是为了解决跨库join的,全局表一般是数据量比较小、基本不会增长的表
    autoIncrement属性:Mycat根据last_insert_id()返回结果。这个需要和mysql自增长主键配合。
    needAddLimit属性:是否自动在每一条SQL语句后面加上limit限制。
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="travelrecord"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn2,dn3"</span><span style="color: #ff0000"&gt; rule</span><span style="color: #0000ff"&gt;="auto-sharding-long"</span> <span style="color: #0000ff"&gt;/></span>

    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; global table is auto cloned to all defined data nodes,so can join
        with any table whose sharding node is in the same data node </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="company"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; type</span><span style="color: #0000ff"&gt;="global"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn3"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="goods"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; type</span><span style="color: #0000ff"&gt;="global"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn2"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; random sharding using mod sharind rule </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="hotnews"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; autoIncrement</span><span style="color: #0000ff"&gt;="true"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn3"</span><span style="color: #ff0000"&gt;
           rule</span><span style="color: #0000ff"&gt;="mod-long"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
        needAddLimit="false"/&gt; <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
        rule="mod-long" /> </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="employee"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn2"</span><span style="color: #ff0000"&gt;
           rule</span><span style="color: #0000ff"&gt;="sharding-by-intfile"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;table </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="customer"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; dataNode</span><span style="color: #0000ff"&gt;="dn1,dn2"</span><span style="color: #ff0000"&gt;
           rule</span><span style="color: #0000ff"&gt;="sharding-by-intfile"</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;childTable </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="orders"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; joinKey</span><span style="color: #0000ff"&gt;="customer_id"</span><span style="color: #ff0000"&gt;
                    parentKey</span><span style="color: #0000ff"&gt;="id"</span><span style="color: #0000ff"&gt;></span>
            <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;childTable </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="order_items"</span><span style="color: #ff0000"&gt; joinKey</span><span style="color: #0000ff"&gt;="order_id"</span><span style="color: #ff0000"&gt;
                        parentKey</span><span style="color: #0000ff"&gt;="id"</span> <span style="color: #0000ff"&gt;/></span>
        <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;childTable</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;childTable </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="customer_addr"</span><span style="color: #ff0000"&gt; primaryKey</span><span style="color: #0000ff"&gt;="ID"</span><span style="color: #ff0000"&gt; joinKey</span><span style="color: #0000ff"&gt;="customer_id"</span><span style="color: #ff0000"&gt;
                    parentKey</span><span style="color: #0000ff"&gt;="id"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;table</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
        /> </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;schema</span><span style="color: #0000ff"&gt;></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 
dataNode标签:定义数据节点
    dataHost属性: 主机的名称
    database属性: 数据库


</span><span style="color: #008000"&gt;--></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
    /> </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;dataNode </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="dn1"</span><span style="color: #ff0000"&gt; dataHost</span><span style="color: #0000ff"&gt;="localhost1"</span><span style="color: #ff0000"&gt; database</span><span style="color: #0000ff"&gt;="db1"</span> <span style="color: #0000ff"&gt;/></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;dataNode </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="dn2"</span><span style="color: #ff0000"&gt; dataHost</span><span style="color: #0000ff"&gt;="localhost1"</span><span style="color: #ff0000"&gt; database</span><span style="color: #0000ff"&gt;="db2"</span> <span style="color: #0000ff"&gt;/></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;dataNode </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="dn3"</span><span style="color: #ff0000"&gt; dataHost</span><span style="color: #0000ff"&gt;="localhost1"</span><span style="color: #ff0000"&gt; database</span><span style="color: #0000ff"&gt;="db3"</span> <span style="color: #0000ff"&gt;/></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<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" /> </span><span style="color: #008000"&gt;--></span>

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
dataHost标签:主要定义和Mysql集群有关的信息,数据实例、读写分离配置和心跳检测语句。
balance属性:读操作负载均衡配置
      0 代表不开启读写分离,所有的读操作都发送到writeHost上。
      1 writeHost和readHost都要参与select语句的负载均衡。例如:双主双从模式 M1->S1,M2->S2,M1和M2互为主备。M2/S1/S2都要参与select语句的负载均衡。
      2 所有读操作都随机分配给writeHost/readHost
      3 所有的读操作随机分发到writeHost下面的readHost上执行。
writeType属性:写操作负载均衡配置
      0 所有的写操作都分发到第一个writeHost。如果第一个挂了,分发到第二个。
      1 所有的写操作都要随机分发到所有配置的writeHost上。1.5以后不推荐。
dbType属性:支持的数据库类型,mycat支持多种db类型:mysql、oracle等
switchType属性:切换类型,主备切换,主主切换。
      -1 代表不自动切换
      1 默认值,自动切换。对应的心跳检测语句select user();
      2 基于Mysql主从同步的状态决定是否切换。对应的心跳检测语句show slave status;
      3 基于MySQL Galera Cluster切换机制。对应的心跳检测语句show status like ‘wsrep%’;
maxCon属性:单个writeHost/readHost节点的最大连接数
minCon属性:单个writeHost/readHost节点的最小连接数
</span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;dataHost </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="localhost1"</span><span style="color: #ff0000"&gt; maxCon</span><span style="color: #0000ff"&gt;="1000"</span><span style="color: #ff0000"&gt; minCon</span><span style="color: #0000ff"&gt;="10"</span><span style="color: #ff0000"&gt; balance</span><span style="color: #0000ff"&gt;="0"</span><span style="color: #ff0000"&gt;
          writeType</span><span style="color: #0000ff"&gt;="0"</span><span style="color: #ff0000"&gt; dbType</span><span style="color: #0000ff"&gt;="mysql"</span><span style="color: #ff0000"&gt; dbDriver</span><span style="color: #0000ff"&gt;="native"</span><span style="color: #ff0000"&gt; switchType</span><span style="color: #0000ff"&gt;="1"</span><span style="color: #ff0000"&gt;  slaveThreshold</span><span style="color: #0000ff"&gt;="100"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;heartbeat</span><span style="color: #0000ff"&gt;></span>select user()<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;heartbeat</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; can have multi write hosts </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;writeHost </span><span style="color: #ff0000"&gt;host</span><span style="color: #0000ff"&gt;="hostM1"</span><span style="color: #ff0000"&gt; url</span><span style="color: #0000ff"&gt;="localhost:3306"</span><span style="color: #ff0000"&gt; user</span><span style="color: #0000ff"&gt;="root"</span><span style="color: #ff0000"&gt;
               password</span><span style="color: #0000ff"&gt;="123456"</span><span style="color: #0000ff"&gt;></span>
        <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; can have multi read hosts </span><span style="color: #008000"&gt;--></span>
        <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;readHost </span><span style="color: #ff0000"&gt;host</span><span style="color: #0000ff"&gt;="hostS2"</span><span style="color: #ff0000"&gt; url</span><span style="color: #0000ff"&gt;="192.168.1.200:3306"</span><span style="color: #ff0000"&gt; user</span><span style="color: #0000ff"&gt;="root"</span><span style="color: #ff0000"&gt; password</span><span style="color: #0000ff"&gt;="xxx"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;writeHost</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;writeHost </span><span style="color: #ff0000"&gt;host</span><span style="color: #0000ff"&gt;="hostS1"</span><span style="color: #ff0000"&gt; url</span><span style="color: #0000ff"&gt;="localhost:3316"</span><span style="color: #ff0000"&gt; user</span><span style="color: #0000ff"&gt;="root"</span><span style="color: #ff0000"&gt;
               password</span><span style="color: #0000ff"&gt;="123456"</span> <span style="color: #0000ff"&gt;/></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/&gt; </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;dataHost</span><span style="color: #0000ff"&gt;></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
    <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"&gt;
    <heartbeat>         </heartbeat>
     <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng"     password="jifeng"&gt;</writeHost>
     </dataHost>

  <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0"     dbType="oracle" dbDriver="jdbc"&gt; <heartbeat>select 1 from dual</heartbeat>
    <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
    <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base"     password="123456" > </writeHost> </dataHost>

    <dataHost name="jdbchost" maxCon="1000"     minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"&gt;
    <heartbeat>select     user()</heartbeat>
    <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>

    <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"&gt;
    <heartbeat> </heartbeat>
     <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng"     password="jifeng"&gt;</writeHost> </dataHost> </span><span style="color: #008000"&gt;--></span>

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"
    dbDriver="jdbc"&gt; <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
    url="jdbc:mysql://localhost:3306" user="root" password="123456"&gt; </writeHost>
    </dataHost> </span><span style="color: #008000"&gt;--></span>

<span style="color: #0000ff"></<span style="color: #800000">mycat:schema<span style="color: #0000ff">>

3.?server.xml

Mycat这个文件主要和

0 0
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="sequnceHandlerType"</span><span style="color: #0000ff"&gt;></span>2<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
  <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;  <property name="useCompression"&gt;1</property></span><span style="color: #008000"&gt;--></span> <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;1为开启mysql压缩协议</span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;  <property name="fakeMySQLVersion"&gt;5.6.20</property></span><span style="color: #008000"&gt;--></span> <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;设置模拟的MySQL版本号</span><span style="color: #008000"&gt;--></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; <property name="processorBufferChunk"&gt;40960</property> </span><span style="color: #008000"&gt;--></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 
<property name="processors"&gt;1</property> 
<property name="processorExecutor"&gt;32</property> 
 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena</span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="processorBufferPoolType"</span><span style="color: #0000ff"&gt;></span>0<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;默认是65535 64K 用于sql解析时最大文本长度 </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<property name="maxStringLiteralLength"&gt;65535</property></span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<property name="sequnceHandlerType"&gt;0</property></span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<property name="backSocketNoDelay"&gt;1</property></span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<property name="frontSocketNoDelay"&gt;1</property></span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;<property name="processorExecutor"&gt;16</property></span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        <property name="serverPort"&gt;8066</property> <property name="managerPort"&gt;9066</property> 
        <property name="idleTimeout"&gt;300000</property> <property name="bindIp"&gt;0.0.0.0</property> 
        <property name="frontWriteQueueSize"&gt;4096</property> <property name="processors"&gt;32</property> </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志</span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="handleDistributedTransactions"</span><span style="color: #0000ff"&gt;></span>0<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

        <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        off heap for merge/order/group/limit      1开启   0关闭
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="u<a href="https://www.52php.cn/tag/SEO/" title="SEO">SEO</a>ffHeapForMerge"</span><span style="color: #0000ff"&gt;></span>1<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        单位为m
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="memoryPageSize"</span><span style="color: #0000ff"&gt;></span>1m<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        单位为k
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="spillsFileBufferSize"</span><span style="color: #0000ff"&gt;></span>1k<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="useStreamOutput"</span><span style="color: #0000ff"&gt;></span>0<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
        单位为m
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="systemReserveMemorySize"</span><span style="color: #0000ff"&gt;></span>384m<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>


    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;是否采用zookeeper协调切换  </span><span style="color: #008000"&gt;--></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="useZKSwitch"</span><span style="color: #0000ff"&gt;></span>true<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>


<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;system</span><span style="color: #0000ff"&gt;></span>

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 
Firewall标签:全局SQL防火墙设置
    定义访问控制策略:如白名单/黑名单
</span><span style="color: #008000"&gt;--></span>
<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt; 
<firewall> 
   <whitehost>
      <host host="127.0.0.1" user="mycat"/&gt;
      <host host="127.0.0.2" user="mycat"/&gt;
   </whitehost>
   <blacklist check="false"&gt;
   </blacklist>
</firewall>
</span><span style="color: #008000"&gt;--></span>

<span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
User标签:定义可访问mycat的用户名称/密码/是否只读
</span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;user </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="root"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="password"</span><span style="color: #0000ff"&gt;></span>123456<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="schemas"</span><span style="color: #0000ff"&gt;></span>TESTDB<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>

    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;
    Privileges标签:表级 DML 权限设置
      控制DML:insert/update/select/delete
      单独给select权限:0010
      单独给insert权限:1000
    </span><span style="color: #008000"&gt;--></span>
    <span style="color: #008000"&gt;<!--</span><span style="color: #008000"&gt;         
    <privileges check="false"&gt;
        <schema name="TESTDB" dml="0110" >
            <table name="tb01" dml="0000"&gt;</table&gt;
            <table name="tb02" dml="1111"&gt;</table&gt;
        </schema>
    </privileges>        
     </span><span style="color: #008000"&gt;--></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;user</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;user </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="user"</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="password"</span><span style="color: #0000ff"&gt;></span>user<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="schemas"</span><span style="color: #0000ff"&gt;></span>TESTDB<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
    <span style="color: #0000ff"&gt;<</span><span style="color: #800000"&gt;property </span><span style="color: #ff0000"&gt;name</span><span style="color: #0000ff"&gt;="readOnly"</span><span style="color: #0000ff"&gt;></span>true<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;property</span><span style="color: #0000ff"&gt;></span>
<span style="color: #0000ff"&gt;</</span><span style="color: #800000"&gt;user</span><span style="color: #0000ff"&gt;></span>

<span style="color: #0000ff"></<span style="color: #800000">mycat:server<span style="color: #0000ff">>

属性

1234 ZK

</td>

</tr>
<tr>
<td valign="top" width="208">

useCompression

</td>
<td valign="top" width="256">

mysql

</td>
<td valign="top" width="261">

1

</td>

</tr>
<tr>
<td valign="top" width="208">

fakeMySQLVersion

</td>
<td valign="top" width="256">

伪装的

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

processorBufferChunk

</td>
<td valign="top" width="256">

每次分配

</td>
<td valign="top" width="261">

默认

</td>

</tr>
<tr>
<td valign="top" width="208">

processors

</td>
<td valign="top" width="256">

系统可用线程数

</td>
<td valign="top" width="261">

默认

</td>

</tr>
<tr>
<td valign="top" width="208">

processorExecutor

</td>
<td valign="top" width="256">

NIOProcessor

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

processorBufferPoolType

</td>
<td valign="top" width="256">

每次分配

</td>
<td valign="top" width="261">

默认是

</td>

</tr>
<tr>
<td valign="top" width="208">

maxStringLiteralLength

</td>
<td valign="top" width="256">

sql

</td>
<td valign="top" width="261">

默认是

</td>

</tr>
<tr>
<td valign="top" width="208">

backSocketNoDelay

</td>
<td valign="top" width="256">

TCP

</td>
<td valign="top" width="261">

默认值

</td>

</tr>
<tr>
<td valign="top" width="208">

frontSocketNoDelay

</td>
<td valign="top" width="256">

TCP

</td>
<td valign="top" width="261">

默认值

</td>

</tr>
<tr>
<td valign="top" width="208">

serverPort

</td>
<td valign="top" width="256">

指定服务端口

</td>
<td valign="top" width="261">

默认

</td>

</tr>
<tr>
<td valign="top" width="208">

managerPort

</td>
<td valign="top" width="256">

指定管理端口

</td>
<td valign="top" width="261">

默认

</td>

</tr>
<tr>
<td valign="top" width="208">

idleTimeout

</td>
<td valign="top" width="256">

连接空闲时间

</td>
<td valign="top" width="261">

默认

</td>

</tr>
<tr>
<td valign="top" width="208">

bindIp

</td>
<td valign="top" width="256">

Mycat

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

frontWriteQueueSize

</td>
<td valign="top" width="256">

前端连接写队列长度

</td>
<td valign="top" width="261">

为了让用户知道是否队列过长(

</td>

</tr>
<tr>
<td valign="top" width="208">

handleDistributedTransactions

</td>
<td valign="top" width="256">

分布式事务开关

</td>
<td valign="top" width="261">

012

</td>

</tr>
<tr>
<td valign="top" width="208">

uSEOffHeapForMerge

</td>
<td valign="top" width="256">

是否让

</td>
<td valign="top" width="261">

1

</td>

</tr>
<tr>
<td valign="top" width="208">

memoryPageSize

</td>
<td valign="top" width="256">

内存分页大小

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

useStreamOutput

</td>
<td valign="top" width="256">

是否使用流输出

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

systemReserveMemorySize

</td>
<td valign="top" width="256">

系统保留内存大小

</td>
<td valign="top" width="261">

</td>

</tr>
<tr>
<td valign="top" width="208">

useZKSwitch

</td>
<td valign="top" width="256">

是否采用

</td>
<td valign="top" width="261">

true/false

</td>

</tr>

    推荐文章
      热点阅读