Mycat Getting Started Tutorial

Introduction to mycat

  • The introduction has a more detailed introduction on the official website. It is not interesting to copy and paste here. You can read it on the official website.
  • Official website link

Precondition

This tutorial is run in the window environment, and the actual production is recommended to run on Linux. 
Prerequisites (install it by yourself, if you can't install it, please lay the foundation first and learn it):

  • JDK: Recommended is 1.7 and above.
  • MySQL: Must be 5.5 and above.

Topology

  • Two tables users and item, three databases db01, db02, db03 (three databases on one database instance)
  • users are only stored in db01.
  • The item table is split into db02 and db03 for storage.
create database db01;

 CREATE TABLE users (  
    id INT NOT NULL AUTO_INCREMENT,  
    name varchar(50) NOT NULL default '',  
    indate DATETIME NOT NULL default '0000-00-00 00:00:00',  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;  

Create item tables in db02 and db03 respectively, the SQL script is as follows

create database db02;  
 CREATE TABLE item (  
    id INT NOT NULL AUTO_INCREMENT,  
    value INT NOT NULL default 0,  
    indate DATETIME NOT NULL default '0000-00-00 00:00:00',  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;

create database db03;  
CREATE TABLE item (  
    id INT NOT NULL AUTO_INCREMENT,  
    value INT NOT NULL default 0,  
    indate DATETIME NOT NULL default '0000-00-00 00:00:00',  
    PRIMARY KEY (id)  
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;

start using

First download the installation package from the official website of mycat, the website is mycat and the  download diagram is as follows: http://dl.mycat.io/1.6-RELEASE/

  • Then edit the three files service.xml, rule.xml, and schema.xml in the conf directory.
  • service.xml mainly configures the parameters of the mycat service, such as the port number, the logical database used by the myact username and password, etc.
  • role.xml mainly configures the routing strategy, mainly including the shard key of the shard, and the split strategy (modulo or division by interval, etc.)
  • The schema.xml file mainly configures the information of the database, such as the logical database name, the physical real data source, the correspondence between the table and the data source, and the routing strategy.
  • The configuration looks like this:

Service.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE mycat:server SYSTEM "server.dtd">  
<mycat:server xmlns:mycat="http://io.mycat/">  
        <system>  
            <!--   
                <property name="processors">32</property>  
                <property name="processorExecutor">32</property>   
                <property name="bindIp">0.0.0.0</property>   
                <property name="frontWriteQueueSize">4096</property>  
                <property name="idleTimeout">300000</property>  
                <property name="mutiNodePatchSize">100</property>  
            -->  
                <property name="defaultSqlParser">druidparser</property>  
                <property name="mutiNodeLimitType">1</property>  
                <property name="serverPort">8066</property>  
                <property name="managerPort">9066</property>   
        </system>  
        <!-- Arbitrarily set the user name, password, database for logging in to mycat-->  
        <user name="test">  
                <property name="password">test</property>  
                <property name="schemas">TESTDB</property>  
        </user>  

        <user name="user">  
                <property name="password">user</property>  
                <property name="schemas">TESTDB</property>  
                <property name="readOnly">true</property>  
        </user>  
        <!--   
        <quarantine>   
           <whitehost>  
              <host host="127.0.0.1" user="mycat"/>  
              <host host="127.0.0.2" user="mycat"/>  
           </whitehost>  
       <blacklist check="false"></blacklist>  
        </quarantine>  
        -->  
</mycat:server>  

Role.xml

The routing table indicates that mod2 is used for routing. It can be seen from the following that the id key of the item table is modulo divided into db02 and db03, and the users table is directly routed to db01.

<?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="role1">
        <rule>
            <columns>id</columns>
            <algorithm>mod-long</algorithm>
        </rule>
    </tableRule>

    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
        <!-- how many data nodes -->
        <property name="count">2</property>
    </function>
</mycat:rule>

The url, usename, and password of the schema.xml database are filled in according to the actual situation.

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

    <!-- Set the storage method of the table.schema name="TESTDB" is consistent with the TESTDB setting in server.xml-->  
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">  
        <table name="users" primaryKey="id"  dataNode="node_db01" />  
        <table name="item" primaryKey="id" dataNode="node_db02,node_db03" rule="role1" />  

    </schema>  

    <!-- Set the database corresponding to dataNode, and the address dataHost connected to mycat -->  
    <dataNode name="node_db01" dataHost="dataHost01" database="db01" />  
    <dataNode name="node_db02" dataHost="dataHost01" database="db02" />  
    <dataNode name="node_db03" dataHost="dataHost01" database="db03" />  

    <!-- The physical host corresponding to the mycat logical host dataHost. The corresponding mysql login information is also set -->  
    <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">  
            <heartbeat>select user()</heartbeat>  
            <writeHost host="server1" url="127.0.0.1:3306" user="root" password="123456"/>  
    </dataHost>  
</mycat:schema>

start test

Switch to the bin directory on the command line and execute the following command: 

After the correct startup, the following command will be displayed: 

Then it means that we started the service successfully.

To access the mycat logical database on the command line, use the following command:

mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB

Now query the database and tables through the database, and find that there is only the logical database TESTDB instead of db01, db02, db03; and the tables are also displayed uniformly, rather than distributed in different actual databases. The reference pictures are as follows: 

Now access Mycat to insert data in the database to see if the data can be divided into tables according to the routing rules configured earlier.

Now execute the following SQL statement to insert data.

insert into users(name,indate) values('kk',now());
insert into users(name,indate) values('ss',now());
insert into item(id,value,indate) values(1,100,now());
insert into item(id,value,indate) values(2,100,now());

Then check whether the insertion is successful on mycat. The following figure shows that the insertion is successful. 

Then log in to the actual database to see if the sub-table is successful. The following figure shows that the split table is successful. 

  • The figure shows that the data in the inserted users table is all in db01, while the data in the item table is evenly distributed in db02 and db03 after modulo by Id. In this way, the table is divided according to the actual routing policy.
  • Finished test! ! ~Data has been sub-database and sub-table!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325236724&siteId=291194637