Mycat sub-table and sub-library: starting from scratch

1. Introduction to Mycat

Mycat is an open source distributed database system. It is a Server that implements the MySQL protocol. Front-end users can regard it as a database proxy and access it with MySQL client tools and command lines, while its back-end can use MySQL native (Native) protocol communicates with multiple MySQL servers, and JDBC protocol can also be used to communicate with most mainstream database servers. Its core function is to divide tables and databases, that is, to horizontally divide a large table into N small tables and store them in the back-end MySQL server or other database.

Second, Mycat basic environment construction

First, you need to download some of the necessary environments for Mycat: 

  • jdk download (above 1.7) – mycat is developed based on java and requires a java compilation environment 
  • mysql 
  • mycat

1) JDK download 
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 
Note: JDK7 or higher version is required. 
2) MySQL download 
http://dev.mysql. com/downloads/mysql/5.5.html#downloads 
Note: MyCAT supports multiple database access, such as: MySQL, SQLServer, Oracle, MongoDB, etc. It is recommended to use 
MySQL as a cluster. 
3) MyCAT project homepage 
https://github.com/MyCATApache/ 
Note: MyCAT related source code and documents can be downloaded from this address

3. Mycat configuration

In order to quickly run a Mycat demo, we first create the test1 and test2 databases in the local database, create a table named opt, and the fields are id (int) and name (varchar)

The main configuration files of Mycat are under its conf directory, which are server.xml, schema.xml and rule.xml. In order to start mycat quickly, we follow the configuration sequence and the main configuration items. 
1. server.xml service configuration
The user and permission information of mycat is mainly configured here. The account here is used to connect to mycat later. 
The quick start can be simply configured like this: 
schemas is the DB configured in the schema.xml later, for example: I have configured an account with the user name user and the password 666666, which can only access the test DB by default.

<user name="user">
    <property name="password">666666</property>
    <property name="schemas">test</property>
</user>

2. Schema.xml database configuration
The main configuration database information here, a schema is a logical library, which can be understood as a database DB managed by Mycat (actually does not exist, it is a virtual concept) The name attribute corresponds to server.xml. 
table is the logical table of Mycat. Here we configure a logical table named opt with the 
dataNode attribute to bind it to the data node in the real database. Here we configure two dataNodes, namely the test1 database and the test2 database. Note , the real table names of these two databases need to be consistent with the name of the table. The 
dataHost attribute is the mysql connection we are familiar with. Here we use the local as a server, and we can also configure different mysql on different servers. Here we can also configure read and write Separation, let's not consider it. 
The rule attribute is the sharding rule, here is the name, which needs to be defined in rule.xml in the next step

<schema name="test" checkSQLschema="false" sqlMaxLimit="100">
    <!-- 分片表配置 -->
    <table name="opt" dataNode="dn$1-2" rule="rule1" />
</schema>

<dataNode name="dn1" dataHost="localhost1" database="test1" />
<dataNode name="dn2" dataHost="localhost1" database="test2" />

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostS1" url="localhost:3306" user="root"
               password="666666" />
</dataHost>

3. Rule.xml rule configuration
Here we mainly configure sharding rules. Mycat supports many sharding rules. We use a relatively simple modulo rule, that is, according to the id attribute in the columns tag, the modulo and count attribute are used. Defined as the number of data nodes.

    <tableRule name="rule1">
    <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>

Fourth, Mycat starts

1.
To start mycat on the window, you need to use the cmd command to go to the bin directory of mycat, and execute the startup_nowrap.bat command successfully. 


2. Linux startup
Similarly, go to the bin directory and execute the startup_nowrap.sh command successfully 
. When you see the startup success logo shown in the following figure, it means that mycat has started successfully 

. Note: If there is an error, you can view the error message under its logs directory. Generally, misconfigured

Five, Mycat use

After starting mycat successfully, we establish a link to mycat through a local sql connection tool, such as Navicat, and its default port is 8066. If the connection is successful, you can execute an insert statement sql through the command line, such as 
INSERT INTO opt (id, name) VALUES(1,"3434") Check whether it is inserted into the table of the specified sub-library. If successful, it means that Mycat can be used normally. 
 
As shown in the figure: the inserted data is distributed to the specified library 

Note: The insert statement of mycat must have fields of sharding rules, otherwise it is impossible to determine which database to insert.

Six, Mycat more details

The above is just a brief introduction to how to quickly start a Mycat DEMO, starting from scratch, for more detailed configuration information and more complex sharding rules and sql, you can refer to the official introduction document of Mycat 
http://www.mycat.org .cn/document/Mycat_V1.6.0.pdf

Guess you like

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