Use mycat to deploy horizontal sharding table on centos7 of MyCat-09---slicing by date

Continuing from the previous post " Use mycat to deploy horizontal sub-tables on centos7 of MyCat-07---shard enumeration "

1. Goal

Use mycat to deploy horizontal sharding table by date.
What is slicing by date?

Polling according to the date is stored in a node server every few days, and then placed on another node server after the number of days, and then looped, and finally achieve fragmentation and table sharding.

Second, implement mycat sharding table by date

The following configurations are executed on the mycat server mycat31

1. Modify vim /usr/local/mycat/conf/schema.xml and
add a line of content <table name="login_log" dataNode="dn1,dn2" rule="sharding_by_time"></table>, which means this experiment The name of the table that is fragmented by date is login_log. The table is horizontally fragmented to two servers, dn1 and dn2, and the rule used is sharding_by_time. The complete schema.xml code is as follows

<?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="salary" dataNode="dn1,dn2" rule="auto_sharding_long"></table>
           <table name="login_log" dataNode="dn1,dn2" rule="sharding_by_time"></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="10.100.100.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="10.100.100.32:3306" user="root"
                                   password="123123">
                </writeHost>
        </dataHost>
</mycat:schema>

2. Modify /usr/local/mycat/conf/rule.xml

2-1. Add rules

        <tableRule name="sharding_by_time">
                <rule>
                        <columns>logintime</columns>
                        <algorithm>partitionByTime</algorithm>
                </rule>
        </tableRule>

Note: The algorithm rule is named sharding_by_time (corresponding to the retention in schema.xml), the column for sharding to capture data is logintime (the table name is defined in schema.xml), and the algorithm uses partitionByTime (customization will be added below) , The algorithm name must be consistent)

2-2. Add algorithm

        <function name="partitionByTime"
                          class="io.mycat.route.function.PartitionByDate">
                <property name="dateFormat">yyyy-MM-dd</property>
                <property name="sBeginDate">2014-01-01</property>
                <property name="sEndDate">2014-01-04</property>
                <property name="sPartionDay">2</property>
        </function>

Note:
yyyy-MM-dd ---->Date format year month day
sBeginDate ---->The first
piece of data such as 2014-01-01 sPartionDay ---->Slice every two days
sEndDate --- ->The day when the second round of loop ends is 2014-01-04.
It probably means that if the input starts on 2014-01-01, then the 1st and the 2nd will be placed on the first node server, and the 3rd and 4th will be placed on the On the second node server, this round is over. PartionDay tells the rules indirectly. There are two servers in total, and there are 4 data in one round. Assuming that there are 5th and 6th, the polled ones will be placed in the first. On a node server, and so on.

3. Open a mycat31 terminal to start the mycat program

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

4-1. Open another mycat31 terminal to start the mycat data management platform

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

4-2. Create a table login_log for this experiment in mycat

create table login_log(xid int(10) not null unique primary key,name varchar(20) not null,logintime datetime,issuccess varchar(100),note varchar(500));

4-3. Insert the test data used for this time in mycat

insert into login_log(xid,name,logintime,issuccess,note) values (1,"kahn","2014-01-01","success","2014-01-01登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (2,"kahn2","2014-01-02","success","2014-01-02登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (3,"kahn3","2014-01-03","success","2014-01-03登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (4,"kahn4","2014-01-04","success","2014-01-04登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (5,"kahn5","2014-01-05","success","2014-01-05登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (6,"kahn6","2014-01-06","success","2014-01-06登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (7,"kahn7","2020-03-27","success","2020-03-27登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (8,"kahn8","2020-03-28","success","2020-03-28登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (9,"kahn9","2020-03-29","success","2020-03-29登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (10,"kahn10","2020-03-30","success","2020-03-30登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (11,"kahn11","2020-03-31","success","2020-03-31登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (12,"kahn12","2020-04-01","success","2020-04-01登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (13,"kahn13","2014-01-07","success","2014-01-07登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (14,"kahn14","2014-01-08","success","2014-01-08登录成功了");
insert into login_log(xid,name,logintime,issuccess,note) values (15,"kahn15","2014-01-09","success","2014-01-09登录成功了");

Three, test results

1. Use the mycat data management platform to select * from login_log; see the data

2. Go to the two physical mysql servers to observe how the data is distributed select * from login_log;

I was successful, I don't know how you are. Send you a bowl of chicken soup as usual.

---------------------END---------------March 26, 2020 23:06:37-- -------------------------

---------------------Chicken soup: People who are better than you work harder than you. What qualifications do you have to be lazy? ---------

Guess you like

Origin blog.csdn.net/xoofly/article/details/105130498