MyCat for MySQL Middleware

Keywords: Linux mycat MySQL Database xml

I. MyCat Basic Concepts

1. What is MyCat

A thoroughly open source, large database cluster for enterprise application development
Transaction enabled, ACID enabled, enhanced database that can replace MySQL
An enterprise database that can be considered a MySQL cluster to replace the expensive Oracle cluster
A new type of SQL Server which combines memory cache technology, NoSQL technology and HDFS large data
New Generation Enterprise Database Product Combining Traditional Database with New Distributed Data Warehouse
A Novel Database Middleware Product

2. Why use MyCat

With the development of the Internet, the magnitude of data is also supporting the growth of index, from GB to TB to PB.The traditional relational database can no longer meet the needs of fast query and insertion of data.At this time, the advent of NoSQL temporarily solved the crisis.It achieves performance improvements by reducing data security, transaction support, and support for complex queries.However, there are occasions when NoSQL compromises are not sufficient for use scenarios (for example, some use scenarios have absolute transaction and security metrics).NoSQL is certainly unsatisfactory at this time).So you still need to use relational databases.How do I use relational databases to solve the problem of mass storage?In order to improve query performance, you need to do database clustering to disperse data from one database into different databases. To cope with this problem, MyCat

MyCat effect:

Can satisfy the large storage of database data; improve query performance
Read-Write Separation
Data slices are split vertically (library), horizontally (table), vertically + horizontally (library table)
Integration of Multiple Data Sources

3. Database middleware comparison

(1) Cobar (formerly amoaba) belongs to Alibaba B2B business group. It served in Ali for more than 3 years and took over the schema of 3000+ MySQL databases. The cluster handles over 5 billion online SQL requests on a daily basis.Cobar stopped maintenance due to the resignation of its sponsors.
(2) Mycat is a secondary development of the open source community on the basis of Alcobar, which solves the problem of cobar and adds many new functions to it.Blue is better than blue.
(3) OneProxy was developed using c based on the official proxy idea of MySQL, and OneProxy is a commercially charged middleware.Discarded some features, focused on performance and stability.
(4) kingshard was developed by small teams in go language, which needs further development and continuous improvement.
Vitess is used in Youtube production and has a complex architecture.MySQL native protocol is not supported and requires a lot of upgrade costs to use.
Atlas is a 360-team rewrite based on mysql proxy, which still needs to be improved with high concurrency and instability.
MaxScale is a middleware developed by mariadb, a version maintained by the original author of MySQL
MySQLRoute is the middleware released by MySQL's official Oracle company

4. Supported databases

Some mainstream databases such as MySQL ORACLE SQLServer are supported

5. Core Technology

Database fragmentation: Database fragmentation refers to the fragmentation of data stored in one database into different databases (hosts) under certain conditions, so as to distribute the load of a single device. According to the slicing rules, it can be divided into the following two slicing modes. MyCAT implements fragmentation by defining the slicing rules for tables, and each table can be bundled togetherEach fragmentation rule specifies a fragmentation field and binds a function to implement a dynamic fragmentation algorithm

1) Schema: A logical library corresponding to a Database in MySQL, in which the included tables are defined.
2) Table: A logical table, that is, a table stored in a physical database, unlike a traditional database, where a table needs to declare the logical data node DataNode it stores.Here you can specify the rules for table fragmentation.
3) The logical data node of DataNode:MyCAT, which is the specific physical node that stores table s, also known as a fragmented node, is associated to a specific database on the back end through DataSource
4) DataSource: Defines the access address of a physical library used to bundle to a Datanode
5) Fragmentation rules: As mentioned earlier, when a large table is divided into several fragmented tables, certain rules are needed. The rule to divide data into a fragmentation according to a certain business rule is the fragmentation rule. It is very important to select an appropriate fragmentation rule for data slicing, which will greatly avoid the difficulties of subsequent data processing.

2. MyCat installation and deployment

Environmental Science:

JDK: Requires JDK to be version 1.7 and above
 MySQL: It is recommended that MySQL be at least version 5.5
 MyCat's official website: http://www.mycat.org.cn/
Host operating system IP Address
mysql01(master01) CentOS 7.3 192.168.1.1
mysql02(slave) CentOS 7.3 192.168.1.8
mycat CentOS 7.3 192.168.1.3
PS:mysql uses an out-of-the-box host (dual-master + keepalived, master-slave as well).If you don't have mysql, check out a blog deployment.

1. Download and install it, using the official website address above.


Copy link, wget Download

[root@localhost ~]# wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

Decompress the package.It is recommended that mycat be placed in the / usr/local/mycat directory.

[root@localhost ~]# tar zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
[root@localhost ~]# cd mycat/

Modify profile:

server.xml: Define users and system-related variables, such as ports
schema.xml: Define logical libraries, tables, tiled nodes, etc.
rule.xml: Define fragmentation rules

[root@mycat mycat]# vim conf/server.xml  
80         <user name="mycat">      //Define users who connect to the middleware
81                 <property name="password">123456</property>  //Password
82                 <property name="schemas">TESTDB</property>      //Logical Library
[root@mycat mycat]# cp conf/schema.xml conf/schema.xml.bak //Backup Profile
[root@mycat mycat]# vim conf/schema.xml 
<?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">     //Indicate the logical library, and the nodes
        </schema>
        <dataNode name="dn1" dataHost="host1" database="test_db" />     //Specify the real library in the back-end server
        <dataHost name="host1" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="192.168.1.1:3306" user="root"    //Define Write Server (Primary) and IP Address, User Password
                                   password="123.com">
                        <!-- can have multi read hosts -->
                        <readHost host="hostS2" url="192.168.1.8:3306" user="root" password="123.com" />    //Define read server (slave) and IP address, user password
                </writeHost>
        </dataHost>
</mycat:schema>

This profile has deleted unnecessary configuration items and comments

Restart mycat service
Let mycat have mysql commands

[root@mysql02 ~]# scp /usr/local/mysql/bin/mysql 192.168.1.3:/usr/local/sbin/

Two mysql authorizations to allow mycat to log on remotely

mysql> grant all on *.* to root@'192.168.1.%' identified by '123.com';

MyCat remote login test:

[root@mycat mycat]# mysql -uroot -h 192.168.1.1 -P 3306 -p123.com
[root@mycat mycat]# mysql -uroot -h 192.168.1.8 -P 3306 -p123.com

Start the mycat service:

[root@mycat mycat]# ./bin/mycat help
Usage: ./bin/mycat { console | start | stop | restart | status | dump }
[root@mycat mycat]# ./bin/mycat console 

Starting with console takes up a terminal to view the log contents (login, query, exit) in real time. Starting with start does not take up any real-time logs or terminals.

[root@mycat ~]# ss -anplt |grep java


###mycat port 8806

Test to see if the data is synchronized:

[root@mycat ~]# mysql -umycat -p123456 -h 192.168.1.3 -P8066
mysql> show databases;

mysql> use TESTDB
mysql> show tables;

mysql> select * from tab1;

3. MyCat Read-Write Separation

Mycat's read-write separation is based on the master-slave replication of Mysql

Modify the configuration file schema.xml

[root@mycat ~]# cd mycat/
[root@mycat mycat]# vim conf/schema.xml

1. Set balance="1" and writeType="0"

Balance parameter settings:
    The modified balance attribute, which configures read-write separated type load balancing types, currently has four values: 
_balance="0", read-write separation mechanism is not enabled, all read operations are sent to the currently available writeHost. 
_balance="1". All readHost s and stand by writeHost participate in the load balancing of the select statement. Simply put, when the dual-master-dual-slave mode (M1->S1, M2->S2, and M1 and M2 are mutually prepared), under normal circumstances, M2,S1,S2 all participate in the load balancing of the select statement.
_balance="2", all read operations are randomly distributed on writeHost, readhost.
_balance='3', all read requests are randomly distributed to readhost for execution, and writerHost does not bear reading pressure
 WriteType parameter settings:
_writeType="0", all writes are sent to the available writeHost.
_writeType='1', all writes are randomly sent to readHost.
_writeType="2". All write operations are randomly distributed on the writeHost, readhost scores.

"readHost belongs to writeHost, which means it gets synchronized data from that writeHost, so when its writeHost goes down, it no longer participates in the read-write separation, i.e."Not working", because its data is now"unreliable".For this reason, in the current versions of mycat 1.3 and 1.4, if you want to support the standard MySQL primary-slave configuration and read data from the primary node when it is down, you need to configure two writeHosts and set banlance=1 in Mycat."

2. Set switchType="2" and slaveThreshold="100"

There are currently three options for switchType:
-1: Indicates no automatic switching
 _1: Default value, automatic switch
 _2: Decide whether to switch based on the status of MySQL master-slave synchronization

"The Mycat heartbeat check statement is configured to show slave status, and two new properties are defined on the dataHost: switchType="2"and slaveThreshold="100", which means opening the read-write separation and switching mechanism for MySQL master-slave replication state binding.Mycat's heartbeat mechanism detects three fields in show slave status: Seconds_Behind_Master, Slave_IO_Running, and Slave_SQL_Running.
Determines the current state of master-slave synchronization and the replication delay of Seconds_Behind_Master master-slave.

3. Modify the configuration file and start the program

[root@mycat mycat]# vim conf/schema.xml

(1) Console startup: execute under mycat/bin directory. /mycat console
(2) Background boot: under mycat/bin directory. /mycat start
In order to see the boot log for the first time and locate the problem easily, select 1 to start the console.

[root@mycat mycat]# ./bin/mycat console

Logon Background Administration Window This login is used to manage maintenance Mycat

[root@mycat ~]# mysql -umycat -p123456 -h 192.168.1.3 -P8066

4. Verify read-write separation my.cnf binlog_format=STATEMENT

(1) Insert data on the write host:

[root@mysql02 ~]# mysql -uroot -p123.com
mysql> use test1;
mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.01 sec)

mysql> insert into tab1 values(4);
Query OK, 1 row affected (0.02 sec)

mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

Master-slave host data inconsistent

[root@mysql01 ~]# mysql -uroot -p123.com
mysql> use test1
mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

(2) Query in Macat:

mysql> use TESTDB
mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)

4. Vertical Split--Branch

A database consists of many tables, each corresponding to a different business. Vertical slicing refers to classifying tables by business and distributing them over different databases, thereby sharing data or pressure among different libraries
The principle of how to divide tables into libraries is that closely related tables should be in one library, and tables that are not related to each other can be divided into different libraries.

In this case, a new mysql is required for the repository operation.

Host operating system IP Address
mysql01(master01) CentOS 7.3 192.168.1.1
mysql02(slave) CentOS 7.3 192.168.1.8
mycat CentOS 7.3 192.168.1.3
mysql03(master02) CentOS 7.3 192.168.1.9

Delete the data inserted in the previous case first

[root@mysql02 ~]# mysql -uroot -p123.com
mysql> delete from tab1 where id=4;
Query OK, 1 row affected (0.01 sec)

mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

[root@mycat ~]# mysql -umycat -p123456 -h 192.168.1.3 -P8066
mysql> use TESTDB

mysql> select * from tab1;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

The following four tables:
Customer tables are divided into one database, and the other three require associated queries, which are divided into another database.

#Customer table rows:200,000
CREATE TABLE customer(
id INT AUTO_INCREMENT,
NAME VARCHAR(200),
PRIMARY KEY(id)
);
#Order form rows: 6 million
CREATE TABLE orders(
id INT AUTO_INCREMENT,
order_type INT,
customer_id INT,
amount DECIMAL(10,2),
PRIMARY KEY(id)
);
#Order Detail Rows: 6 million
CREATE TABLE orders_detail(
id INT AUTO_INCREMENT,
detail VARCHAR(2000),
order_id INT,
PRIMARY KEY(id)
);
#Order Status Dictionary Table rows:20
CREATE TABLE dict_order_type(
id INT AUTO_INCREMENT,
order_type VARCHAR(200),
PRIMARY KEY(id)
);

Implement repositories:

1. Modify schema configuration file

[root@mycat mycat]# vim conf/schema.xml

<?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="customer" dataNode="dn2"> </table>
        </schema> 
        <dataNode name="dn1" dataHost="host1" database="test1" />
        <dataNode name="dn2" dataHost="host2" database="test1" />
        <dataHost name="host1" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="192.168.1.1:3306" user="root"
                                   password="123.com">
                        <readHost host="hostS2" url="192.168.1.8:3306" user="root" password="123.com" />
                </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="hostM2" url="192.168.1.9:3306" user="root"
                                   password="123.com">
                </writeHost> 
        </dataHost>

</mycat:schema>

Authorize MyCat to remotely log on to mysql03

[root@mysql03 ~]# mysql -uroot -p123.com
mysql> grant all on *.* to root@'192.168.1.%' identified by '123.com';

Because the real library mysql03 corresponding to the logical library in the configuration file does not exist, it needs to be created in mysql03

mysql> create database test1;

2. Restart mycat, access MyCat, create tables

[root@mycat mycat]# mysql -umycat -p123456 -h 192.168.1.3 -P8066

mysql> use TESTDB
mysql> CREATE TABLE customer(
    -> id INT AUTO_INCREMENT,
    -> NAME VARCHAR(200),
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE orders(
    -> id INT AUTO_INCREMENT,
    -> order_type INT,
    -> customer_id INT,
    -> amount DECIMAL(10,2),
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE orders_detail(
    -> id INT AUTO_INCREMENT,
    -> detail VARCHAR(2000),
    -> order_id INT,
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE dict_order_type(
    -> id INT AUTO_INCREMENT,
    -> order_type VARCHAR(200),
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-----------------     +
| Tables_in_test1 |
+-----------------     +
| customer           |
| dict_order_type |
| orders               |
| orders_detail    |
| tab1                  |
+-----------------    +
5 rows in set (0.05 sec)

View the table using mysql01:

mysql> show tables;
+-----------------    +
| Tables_in_test1 |
+-----------------    +
| dict_order_type |
| orders               |
| orders_detail    |
| tab1                  |
+-----------------    +
4 rows in set (0.00 sec)

View the table using mysql03:

mysql> use test1;
mysql> show tables;
+-----------------     +
| Tables_in_test1 |
+-----------------     +
| customer           |
+-----------------     +
1 row in set (0.00 sec)

View structure

mysql> desc customer;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id          | int(11)           | NO    | PRI | NULL    | auto_increment |
| NAME  | varchar(200) | YES  |     | NULL       |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

5. Horizontal Split--Split Table

In contrast to vertical splitting, horizontal splitting does not categorize tables, but spreads them across libraries according to some rule of a field, each containing a portion of the data.Simply put, we can think of horizontal slicing of data as slicing by rows of data, where some rows in a table are sliced into one database and others into another.
There is a bottleneck in choosing the number of data bars to split the table MySQL form into tables. Expressing to 10 million data bars will reach the bottleneck, which will affect the query efficiency and need to be optimized horizontally.For example, orders and orders_detail in the previous case have reached 6 million rows of data and need to be optimized for tabular breakdown.Tabulation Fields Take the order table as an example, which can be sorted according to different fields

number Table Fields Effect
1 id (primary key, or creation time) Query orders are time-sensitive and historical orders are queried less often. Such fragmentation can result in more visits to one node, less visits to one node, and uneven.
2 customer_id (customer ID) Divided by customer id, two nodes have average access and all orders for a customer are in the same node

1. Modify the configuration file schema.xml

[root@mycat mycat]# vim conf/schema.xml

<?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="customer" dataNode="dn2"> </table>
                <table name="orders" dataNode="dn1,dn2" rule="mod_rule">
                        <childTable name="orders_detail" primaryKey="id" joinKey="order_id" parentKey="id" />
                </table>
        </schema> 
        <dataNode name="dn1" dataHost="host1" database="test1" />
        <dataNode name="dn2" dataHost="host2" database="test1" />
        <dataHost name="host1" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="192.168.1.1:3306" user="root"
                                   password="123.com">
                        <readHost host="hostS2" url="192.168.1.8:3306" user="root" password="123.com" />
                </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="hostM2" url="192.168.1.9:3306" user="root"
                                   password="123.com">
                </writeHost> 
        </dataHost>

2. Modify the configuration file rule.xml

[root@mycat mycat]# vim conf/rule.xml

#Add a new slicing rule mod_rule to the rule configuration file and specify that the applicable field of the rule is customer_id.
#There is also an optional slicing algorithm, mod-long (modulo operations on fields), where customer_id mods two nodes and slices them according to the results

 38         <tableRule name="mod_rule">
 39                 <rule>
 40                         <columns>customer_id</columns>
 41                         <algorithm>mod-long</algorithm>
 42                 </rule>
 43         </tableRule>
 ......
 105         <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
 106                 <!-- how many data nodes -->
 107                 <property name="count">2</property>
#Configuration algorithm mod-long parameter count is 2, two nodes
 108         </function>

3. Because there is no order table on dn2(mysql03:192.168.1.9), create order table on data node dn2 and restart mycat service

[root@mysql03 ~]# mysql -uroot -p123.com
mysql> use test1;
mysql> CREATE TABLE orders(
    -> id INT AUTO_INCREMENT,
    -> order_type INT,
    -> customer_id INT,
    -> amount DECIMAL(10,2),
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.00 sec)

4. Insert data into orders table in MyCat, insert field cannot be omitted

[root@mycat mycat]# mysql -umycat -p123456 -P8066 -h192.168.1.3
mysql> use TESTDB

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES (1,101,100,100100);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(2,101,100,100300);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(3,101,101,120000);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(4,101,101,103000);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(5,102,101,100400);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(6,102,100,100020);
Query OK, 1 row affected (0.01 sec)

#View orders table data in mycat, dn1, dn2, table breakdown succeeded

dn1(mysql01:192.168.1.1):

[root@mysql01 ~]# mysql -uroot -p123.com
mysql> use test1;
mysql> select * from orders;
+----+------------+-------------+-----------+
| id | order_type | customer_id | amount    |
+----+------------+-------------+-----------+
|  2 |        101 |         100 | 100300.00 |
|  4 |        101 |         101 | 103000.00 |
|  6 |        102 |         100 | 100020.00 |
+----+------------+-------------+-----------+
3 rows in set (0.00 sec)

dn2(mysql03:192.168.1.9):

[root@mysql03 ~]# mysql -uroot -p123.com
mysql> use test1;

mysql> select * from orders;
+----+------------+-------------+-----------+
| id | order_type | customer_id | amount    |
+----+------------+-------------+-----------+
|  1 |        101 |         100 | 100100.00 |
|  3 |        101 |         101 | 120000.00 |
|  5 |        102 |         101 | 100400.00 |
+----+------------+-------------+-----------+
3 rows in set (0.00 sec)

Create orders_detail table in dn2(mysql03:192.168.1.9)

mysql> CREATE TABLE orders_detail(
    -> id INT AUTO_INCREMENT,
    -> detail VARCHAR(2000),
    -> order_id INT,
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.02 sec)

Restart Mycat to access Mycat to insert data into orders_detail table

[root@mycat mycat]# mysql -umycat -p123456 -h 192.168.1.3 -P8066
mysql> use TESTDB
mysql> insert into orders_detail(id,detail,order_id) values (1,'detail',1);
Query OK, 1 row affected (0.03 sec)

mysql> insert into orders_detail(id,detail,order_id) values (2,'detail',2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into orders_detail(id,detail,order_id) values (3,'detail',3);
Query OK, 1 row affected (0.01 sec)

mysql> insert into orders_detail(id,detail,order_id) values (4,'detail',4);
Query OK, 1 row affected (0.01 sec)

mysql> insert into orders_detail(id,detail,order_id) values (5,'detail',5);
Query OK, 1 row affected (0.00 sec)

mysql> insert into orders_detail(id,detail,order_id) values (6,'detail',6);
Query OK, 1 row affected (0.01 sec)

mysql> select o.*,od.detail from orders as o inner join orders_detail as od on o.id=od.order_id;
+----+------------+-------------+-----------+--------+
| id | order_type | customer_id | amount    | detail |
+----+------------+-------------+-----------+--------+
|  1 |        101 |         100 | 100100.00 | detail |
|  3 |        101 |         101 | 120000.00 | detail |
|  5 |        102 |         101 | 100400.00 | detail |
|  2 |        101 |         100 | 100300.00 | detail |
|  4 |        101 |         101 | 103000.00 | detail |
|  6 |        102 |         100 | 100020.00 | detail |
+----+------------+-------------+-----------+--------+
6 rows in set (0.03 sec)

View using dn1, dn2:

mysql> select * from orders_detail;

mysql> select * from orders_detail;

6. Global Tables

In the case of fragmentation, when a business table is fragmented due to size, the association between the business table and these affiliated dictionary tables becomes a tricky issue, considering that dictionary tables have the following characteristics:

Change infrequently 
The total amount of data does not change much 
Data is small, with few records exceeding hundreds of thousands 

For this reason, Mycat defines a special table called a Global Table, which has the following characteristics:

Insertions and updates of global tables are performed on all nodes in real time, maintaining data consistency across fragments
 Query operations for global tables, obtained from only one node 
Global tables can be defined as global tables by JOIN operations with any table, which solves the problem of data JOIN. 

Mycat can meet more than 80% of enterprise application development through global table + fragmentation based on E-R relationship

[root@mycat mycat]# vim conf/schema.xml

                <table name="dict_order_type" dataNode="dn1,dn2" type="global"> </table>

Create dict_order_type table in dn2(mysql03:192.168.1.9)

mysql> CREATE TABLE dict_order_type(
    -> id INT AUTO_INCREMENT,
    -> order_type VARCHAR(200),
    -> PRIMARY KEY(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

Restart MyCat and access MyCat to insert data into dict_order_type table

[root@mycat mycat]# mysql -umycat -p123456 -h 192.168.1.3 -P8066
mysql> use TESTDB

mysql>  insert into dict_order_type (id,order_type) values (101,'type1');
Query OK, 1 row affected (0.03 sec)

mysql>  insert into dict_order_type (id,order_type) values (102,'type2');
Query OK, 1 row affected (0.01 sec)

mysql> select * from dict_order_type;
+-----+------------+
| id  | order_type |
+-----+------------+
| 101 | type1      |
| 102 | type2      |
+-----+------------+
2 rows in set (0.03 sec)

Common slicing rules

Modularization: This rule is to find the operation for piecewise fields.It is also the most common rule for horizontal subtables.In the 5.1 configuration sub-table, this rule is used in the orders table.
Fragmentation enumeration: Configure fragmentation yourself by configuring the possible enumeration IDS in the configuration file. This rule applies to specific scenarios, such as when some businesses need to be saved by provinces or counties, while provinces, counties and counties throughout the country are fixed. This rule is used for such businesses.

Posted by mike1313 on Sun, 16 Feb 2020 15:40:39 -0800