This article is an introduction to MyCat. But the focus is not so much on how to use Mycat and how to configure it. Mycat configuration related content I believe readers on the Internet and Mycat official group can find the corresponding answers. The focus of this article is on the implementation of the logic behind Mycat. This article is the author from the network to learn the summary of the results, I hope to have certain help to each reader. If there is inappropriate content, welcome to leave a message in the comments.
-
Mycat is a database middleware edited based on Java language. It complies with Mysql native protocol and is a universal middleware agent for cross-language, cross-platform and cross-database.
-
Mycat works by intercepting SQL statements sent by users and analyzing SQL statements: fragment analysis, route analysis, read/write separation analysis, cache analysis, etc. The SQL is then sent to the real database at the back end.
-
Three configuration files
- Schema.xml defines logical libraries, tables, shard nodes, and so on
- Rule-xml defines sharding rules
- Server.xml defines variables related to users and system variables.
-
Data segmentation method:
- Vertical sharding (database sharding) : Shards tables in different business scenarios to different databases based on business.
- Horizontal shred: Split the data in the same table into different databases according to the logical relationship between the data in the table.
-
Database and table problems:
- Introducing distributed transactions
- Cross node join problem
- Merge sort paging problems across nodes
- Multi-data source problem
-
Mycat join problem
-
Table grouping: The records of the child Table and the associated parent Table are stored in the same data fragment, that is, the child Table depends on the parent Table. The Table Group ensures that data Join will not operate across libraries.
-
Mycat global table:
- The insertion and update of the global table will be performed on all nodes in real time to keep the data consistency of each shard
- Query operations for global tables are retrieved from only one node
- A global table can JOIN any table
-
Share join
- Currently, joins of two tables are supported. The principle is to parse SQL statements, split into a single table SQL statement execution, and then the data of each node together.
-
-
Mycat query: The routing result of myCAT is determined by the sharding field and sharding method.
- If a fragment field exists in the query criteria, routes are directly routed to a specific fragment
- If there is no fragment field in the query condition, MyCat cannot calculate the route and sends the query to all nodes for execution, and then returns the result and aggregates it.
-
Mycat paging sort:
- Pure LIMIT: MyCat distributes queries to individual DB nodes for execution. However, Mycat response results depend on which DB node returns results to Mycat first.
- Limit and sort: MyCat distributes queries to individual DB nodes for execution. After receiving the results from each DB node, a minimum heap operation is performed on all the results. The result is then returned.
- Limit with offset and sort:
- For SQL statements with limit m,n, Mycat will rewrite them to limit 0, m+n to ensure logical correctness of the query result.
- Then send the rewritten SQL to each DB node to execute.
- After receiving the results from all DB nodes, a minimum heap operation is performed on all the results. It is then returned to the front-end user
- Disadvantages: it consumes resources. For K DB nodes, the amount of data MyCat needs to process is (m+n) * T. Mycat is not suitable for paging sort operations.
-
Mycat transaction implementation.
- When the application starts a transaction, MyCat identifies the connection as non-automatic
- Mycat executes subsequent SQl over a connection that is not automatically committed.
- If all nodes are successfully executed, MyCat marks the connection as Prepare Ready. If one node fails to be executed, the node is in the RollBack state.
- MyCat waits to reference subsequent commit or rollBack commands. For the commit command: If the current connection is in the Prepare Ready state, the commit command is sent to each DB node.
- If a DB fails during commit and the other DB nodes commit successfully, mycat waits for the failed DB node to return the result until TIMEOUT. The transaction consistency is broken. Therefore, Mycat transactions are weakly consistent.
-
Fragmentation rules
- modulus
- Fragment enumeration: You can configure fragments by configuring possible enumeration ids in the configuration file.
- Sharding by scope
- Sharding by date
-
MyCat global sequence.
-
MyCat configures the sequence into a file. For each insert, MyCat updates the current sequence value in the sequence_conf.properties file in classpath.
- Advantages: Local load, fast read.
- Disadvantages: Poor risk resistance, unable to read local files after MyCat active outage.
-
Database mode: Use a table of the database for accumulation. MyCat preloads a portion of the number segment into MyCat’s memory. The number segment is fetched from the sequence first, and then from the database if MyCat runs out of number segments in memory.
- If MyCat crashes, a number segment is wasted. Mycat restarts to get the next number segment.
-
Timestamp mode: UUID=42 bits ms +5 bits MACHINE ID+5 service code +12 repeated accumulation
- Advantages: Simple configuration
- Disadvantages: The 18-bit ID is too long
- Heavily dependent on the machine clock, clock callback will cause serialization duplication.
-
Autonomously produce global sequences.
-