MyCat-07之centos7上使用mycat部署水平分表---分片枚举

一、目标

使用mycat部署分片枚举。
什么叫分片枚举?分片枚举有什么用处?
分片枚举:是mycat水平分表的一种形式,它可以按照指定的规则将表的数据分发到不同的数据库,最终以减轻单台msyql服务器的压力。

二、平台

[[email protected] ~]# uname -a
Linux client 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
Mycat-server-1.6.7.5-test-20200303154735-linux.tar.gz

三、实验拓扑

主机名        ip                          所装必须的软件                                             角色
mycat31     192.168.73.31      java1.8及以上,mycat1.6.7.5,mysql5.7      mycat服务器、mysql库xkahn的部分表存储地
mycat32     192.168.73.32      mysql5.7                                                        mysql库xkahn的部分表存储地

四、说明

本次不再重复以前做过的操作了,写个博客真累,又没人点赞支持。关于基础知识课参考之前的帖子

五、部署mycat分片枚举

1.(mycat31上执行)修改配置文件/usr/local/mycat/conf/server.xml中的默认启动账号从root改为为mycat
(这里的账号mycat是mycat的管理账号,而非linux系统上的账号。)

cp /usr/local/mycat/conf/server.xml{,.bak}
sed -i 's/name="root"/name="mycat"/g' /usr/local/mycat/conf/server.xml

注释:
<user name="mycat" ----->定义mycat的管理账号叫mycat(即登录mycat管理程序的账号,而非linux系统账号)
<property name="password">123456 ----->定义mycat的管理账号的密码
<property name="schemas">TESTDB ----->定义mycat的逻辑数据库名,这里需要和schema.xml中的<schema name="TESTDB"保持一致。

2.(mycat31上执行)修改配置文件/usr/local/mycat/conf/schema.xml

cat > /usr/local/mycat/conf/schema.xml <<EOF
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
           <table name="teacher" dataNode="dn2"></table>
           <table name="student" dataNode="dn1,dn2" rule="mod_rule">
               <childTable name="student_record" primaryKey="xid" joinKey="student_id" parentKey="xid" />
           </table>
           <table name="xuser" dataNode="dn1,dn2" type="global"></table>
           <table name="grade" dataNode="dn1,dn2" rule="sharding_by_intfile"></table>
        </schema>
        <dataNode name="dn1" dataHost="host1" database="xkahn" />
        <dataNode name="dn2" dataHost="host2" database="xkahn" />
        <dataHost name="host1" 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.73.31:3306" user="root"
                                   password="123123">
                </writeHost>
        </dataHost>
        <dataHost name="host2" 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.73.32:3306" user="root"
                                   password="123123">
                </writeHost>
        </dataHost>
</mycat:schema>
EOF

相比之前做的又多了本次的分片枚举参数<table name="grade" dataNode="dn1,dn2" rule="sharding_by_intfile"></table>,#分片枚举:表名叫grade被切分到dn1,dn2两台服务器,切分的规则用./rule.xml中的sharding_by_intfile规则。

3.(mycat31上执行)修改配置文件rule.xml

cp /usr/local/mycat/conf/rule.xml{,.bak}
vim /usr/local/mycat/conf/rule.xml

3-1.(mycat31上执行)添加自定义规则
在<mycat:rule xmlns:mycat="http://io.mycat/">的下面添加7行内容(包含最后一行空白行)

<tableRule name="sharding_by_intfile">
                <rule>
                        <columns>course</columns>
                        <algorithm>hash-int</algorithm>
                </rule>
        </tableRule>

注释,这里定义分片规则叫sharding_by_intfile,分片依靠的列叫course(在schema.xml中有定义它的表名叫grade),分片的方法用hash-int(下面会有介绍)

3-2.(mycat31上执行)修改系统预定义的算法hash-int。
原本的hash-int方法是这样写的
         <function name="hash-int"
                          class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
        </function>
修改为

        <function name="hash-int"
                          class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
                <property name="type">1</property>
                <property name="defaultNode">0</property>
        </function>

注释:
分片用的系统类是io.mycat.route.function.PartitionByFileMap;
分片枚举规则文件是./partition-hash-int.txt;
规则文件中要用来做枚举分片的字段类型<property name="type">1</property>,0代表int类型,1代表string类型。
规则文件中没有指定的字段存放在哪个服务器<property name="defaultNode">0</property>,0代表编程语言的第一个(服务器),若改成了1,那就说明没有指定枚举规则的数据就放到schema.xml中的第二台mysql服务器上。

3-3.还是上一段完整的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 - - http://www.apache.org/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 name="sharding_by_intfile">
                <rule>
                        <columns>course</columns>
                        <algorithm>hash-int</algorithm>
                </rule>
        </tableRule>

        <tableRule name="mod_rule">
                <rule>
                        <columns>xid</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>

        <tableRule name="rule1">
                <rule>
                        <columns>id</columns>
                        <algorithm>func1</algorithm>
                </rule>
        </tableRule>

        <tableRule name="sharding-by-date">
                <rule>
                        <columns>createTime</columns>
                        <algorithm>partbyday</algorithm>
                </rule>
        </tableRule>

        <tableRule name="rule2">
                <rule>
                        <columns>user_id</columns>
                        <algorithm>func1</algorithm>
                </rule>
        </tableRule>

        <tableRule name="sharding-by-intfile">
                <rule>
                        <columns>sharding_id</columns>
                        <algorithm>hash-int</algorithm>
                </rule>
        </tableRule>
        <tableRule name="auto-sharding-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>rang-long</algorithm>
                </rule>
        </tableRule>
        <tableRule name="mod-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>
        <tableRule name="sharding-by-murmur">
                <rule>
                        <columns>id</columns>
                        <algorithm>murmur</algorithm>
                </rule>
        </tableRule>
        <tableRule name="crc32slot">
                <rule>
                        <columns>id</columns>
                        <algorithm>crc32slot</algorithm>
                </rule>
        </tableRule>
        <tableRule name="sharding-by-month">
                <rule>
                        <columns>create_time</columns>
                        <algorithm>partbymonth</algorithm>
                </rule>
        </tableRule>
        <tableRule name="latest-month-calldate">
                <rule>
                        <columns>calldate</columns>
                        <algorithm>latestMonth</algorithm>
                </rule>
        </tableRule>

        <tableRule name="auto-sharding-rang-mod">
                <rule>
                        <columns>id</columns>
                        <algorithm>rang-mod</algorithm>
                </rule>
        </tableRule>

        <tableRule name="jch">
                <rule>
                        <columns>id</columns>
                        <algorithm>jump-consistent-hash</algorithm>
                </rule>
        </tableRule>

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

        <function name="crc32slot"
                          class="io.mycat.route.function.PartitionByCRC32PreSlot">
                <property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
        </function>
        <function name="hash-int"
                          class="io.mycat.route.function.PartitionByFileMap">
                <property name="mapFile">partition-hash-int.txt</property>
                <property name="type">1</property>
                <property name="defaultNode">0</property>
        </function>
        <function name="rang-long"
                          class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">autopartition-long.txt</property>
        </function>
        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                <!-- how many data nodes -->
                <property name="count">2</property>
        </function>

        <function name="func1" class="io.mycat.route.function.PartitionByLong">
                <property name="partitionCount">8</property>
                <property name="partitionLength">128</property>
        </function>
        <function name="latestMonth"
                          class="io.mycat.route.function.LatestMonthPartion">
                <property name="splitOneDay">24</property>
        </function>
        <function name="partbymonth"
                          class="io.mycat.route.function.PartitionByMonth">
                <property name="dateFormat">yyyy-MM-dd</property>
                <property name="sBeginDate">2015-01-01</property>
        </function>


        <function name="partbyday"
                          class="io.mycat.route.function.PartitionByDate">
                <property name="dateFormat">yyyy-MM-dd</property>
                <property name="sNaturalDay">0</property>
                <property name="sBeginDate">2014-01-01</property>
                <property name="sEndDate">2014-01-31</property>
                <property name="sPartionDay">10</property>
        </function>

        <function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
                <property name="mapFile">partition-range-mod.txt</property>
        </function>

        <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
                <property name="totalBuckets">3</property>
        </function>
</mycat:rule>

4.(mycat31上执行)修改分片枚举规则文件partition-hash-int.txt

cp /usr/local/mycat/conf/partition-hash-int.txt{,.bak}
cat > /usr/local/mycat/conf/partition-hash-int.txt <<EOF
language=0
math=1
EOF

注释:这就是分片枚举的规则了。
language指的是数据库(xkahn)中本次的分片枚举表(grade)中的字段course的一个值叫language(语文)
math同上,这个数据库course字段值表示数学。
language=0  ------->让语文成绩放到第0台服务器(实际的schema.xml中定义的第一台服务器)
math=1  --------->让数学成绩放到第1台服务器(实际的schema.xml中定义的第二台服务器)
那其他成绩呢?问的好,刚才在<3.2>中有说明了<function name="hash-int" defaultNode的值是0代表第一台服务器就是默认的其他科目成绩的存放地。

5.小结

是不是有点乱?捋一捋?那就捋一捋。
1.首先mycat定制规则要从schema.xml中开始
2.schema.xml中定义了此次的枚举分片<table name="grade" dataNode="dn1,dn2" rule="sharding_by_intfile"></table>,grade是表名,该表被分到了dn1和dn2两台服务器,规则用的名字叫sharding_by_intfile。
3.那么规则sharding_by_intfile都是放在了规则配置文件./rule.xml中。
4.rule.xml中就增加一个sharding_by_intfile的规则,而它的规则中指定(表grade)列叫course。同时还指定了分片方法叫hash-int
5.在rule.xml中的后部分就有定义hash-int分片方法<function name="hash-int"
6.在分片方法中有定义了具体的分片枚举的配置文件叫partition-hash-int.txt,这个文件中指定了具体按照什么样的枚举去分表,同时还指定了该字段course的字段类型为string(<property name="type">1</property>),同时还声明了,假定分片枚举配置文件partition-hash-int.txt中若没定义的course的值的时候,该值应当存放在mycat的第一个节点服务器上(<property name="defaultNode">0</property>)
7.而具体的分片枚举配置文件partition-hash-int.txt就指定了凡是course值等于语文(language)的就放到第0台服务器(实际的mycati的第一个节点服务器)上,凡是course值等于数学(math)的都放到第1台服务器(实际的mycati的第二个节点服务器)上。

六、往mycat的分片枚举表里灌入数据(mycat31上执行)

1.开一个新终端用于启动mycat

cd /usr/local/mycat/bin
./mycat console

2.再开一个新的终端,并启动mycat的数据管理平台

mysql -umycat -p123456 -h 10.100.100.31 -P 8066
use TESTDB;

3..在mycat数据管理平台创建mycat中建立本次的分片枚举的表

create table grade(xid int(10) not null unique primary key,student_id int(10) not null,course varchar(100) not null,grade float(5,2),note varchar(500));

4.在mycat数据管理平台上往分片枚举的表grade里灌入测试数据

insert into grade(xid,student_id,course,grade,note) values (1,1,"language",95.5,"语文玖拾伍点五分");
insert into grade(xid,student_id,course,grade,note) values (2,1,"math",92.5,"玖拾贰点五分92.5");
insert into grade(xid,student_id,course,grade,note) values (3,2,"language",60,"及格60");
insert into grade(xid,student_id,course,grade,note) values (4,2,"math",35,"忒差了35");
insert into grade(xid,student_id,course,grade,note) values (5,1,"sport",100,"体育100");
insert into grade(xid,student_id,course,grade,note) values (6,2,"sport",99,"体育99");
insert into grade(xid,student_id,course,grade,note) values (7,1,"music",80,"音乐80分");
insert into grade(xid,student_id,course,grade,note) values (8,2,"music",88,"音乐88分");

 

5.在mycat数据管理平台上查询数据是否正常

select * from grade;

七、检验分片枚举

1.分别登陆两台mysql服务器,查看grade表里的数据是否是按照语文(language)和数学(math)去分到了各自自定的服务器上的数据库上了。

select * from grade;

2.同时看看体育和音乐成绩的值都放到了哪个服务器上了

3.还可以回去修改rule.xml中的<property name="defaultNode">0</property>的值改为1,即将默认的值存放到第二台节点服务器上。然后再重启mycat,再插入一些其他科目的数据进去,看看成绩是不是跑到了第二台服务器上了。

下图可能跟本次试验有点差别,因为是用的其他电脑做的测试,刚才开始<property name="defaultNode">的值我设置的是1,本次写博客的时候又改成了0。如果看不懂本剧说的,完全可以忽略这两行废话。

----------------------END----------------2020年3月24日23:35:52-------------------------------------

写帖子真心的累,纯写帖子+截图,1个小时都算快的了,一般1个小时不可能写完。网上的帖子不是这有坑,就是那有陷阱,像保安这样完美的干货技术贴不多。
若是感觉对你有用,随意赞助下,弥补一下我这颗快崩溃的心。
-------------------最后送上一碗鸡汤:要拿执着将那苦逼的命运枷锁打破----------------------------------------

猜你喜欢

转载自blog.csdn.net/xoofly/article/details/105082918