Please state the source of the article. Welcome to add Echo wechat (wechat id: T2421499075) for exchange and learning. Invincible, according to not claim to always win, defeat not decadent, according to the ability to forge ahead. That’s how powerful it really is!!
After the installation is complete, we have parsed the important configuration files of Mycat, and now we have a certain understanding of Mycat, so let’s start Mycat and connect, do some simple configuration and use operations.
Mycat quick start
- Preparations:
- * please now server installation Mycat, above installation tutorial: https://blog.csdn.net/xlecho/article/details/102755484
- * Take a look at its core configuration file: [Server. XML explanation] (https://blog.csdn.net/xlecho/article/details/102758706), [schema. The XML explanation] (HTTP: / / https://blog.csdn.net/xlecho/artic Le/Details /102758706), [rule-xml details](https://note.youdao.com/)
- * Prepare three servers, all of which require mysql installation
- * build a separation from MySQL, speaking, reading and writing environment: the main two [Mycat distributed database architecture solutions – separation set up the MySQL, speaking, reading and writing environment — more than a master from] (https://blog.csdn.net/xlecho/article/details/102869048)
- * Download a copy of Mycat source code (on the server is not convenient to write, local change is good, It is more convenient to replace server configuration file) [Mycat download] (https://github.com/MyCATApache/Mycat-download/tree/master/1.6-RELEASE)
Mycat possible commands to configure
- Go to the bin directory of mycat and run the./startup_nowrap.sh command
- Go to the bin directory of mycat installation and run the./mycat stop command
- Check if there is a process running mycat: ps -ef
- To view mycat running logs, go to the logs directory: more mycat.log
- Delete an entire line from the configuration file: vi Open the configuration file and double-click d to delete the line where the cursor is located
- Delete a whole block from the configuration file: click V, then move the left and right keys, the cursor movement will select the move area, and then click X to delete
Configuration Mycat
Using Mycat to achieve read/write separation is relatively simple, just focus on its two configuration files schema.xml/server.xml.
- Schema.xml is mainly responsible for database connection configuration
- Server.xml is responsible for the Mycat login connection configuration
schema
<? The XML version = "1.0"? > <! DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="userDB" checkSQLschema="true" sqlMaxLimit="100"> <table name="user" primaryKey="id" dataNode="dn1" /> </schema> <dataNode name="dn1" dataHost="host1" database="test" /> <! 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="hostM" Url ="192.168.222.132:3306" user="root" password="123456"> <readHost host="hostS1" URL ="192.168.222.133:3306" user="root" Password ="123456" /> <readHost host="hostS2" URL ="192.168.222.134:3306" user="root" password="123456" /> </writeHost> </dataHost> </mycat:schema>Copy the code
server
<? The 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:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <! Mycat has two important tags: user and system --> <! -- The system tag is related to the system configuration --> <system> <! When configuring this attribute, ensure that the mycat character set is the same as the mysql character set. To configure a special character set, for example, UTF8MB4, you can run the index_to_charset.properties command in the ID= character set format, for example: "224 = utf8mb4" -- - > < property name = "charset" > utf8 < / property > <! <property name="useSqlStat">0</property> <! -- 0 indicates that password login is required, and 1 indicates that password login is not required. The default value is 0. <property name="nonePasswordLogin">0</property> < Property name="useHandshakeV10">1</property> <! Property name="useGlobleTableCheck">0</property> <! SQL execution timeout unit: seconds --> <property name="sqlExecuteTimeout">300</property> <! -- Specifies the type of global sequence to use Mycat. 0 is the local file mode, 1 is the database mode, 2 is the timestamp sequence mode, 3 is the distributed ZK ID generator, 4 is the ZK incremental ID generation. --> <property name="sequnceHandlerType">5</property> <! <property name="sequnceHandlerPattern">(? :(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property> <! -- If the sub-query contains associated query, check whether the associated field contains fragment fields. The default false - > < property name = "subqueryRelationshipCheck" > false < / property > < property name="sequenceHanlderClass">io.mycat.route.sequence.handler.HttpIncrSequenceHandler</property> <! -- Defaults to type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool --> <property name="processorBufferPoolType">0</property> <! -- Distributed transaction switch, 0 indicates that distributed transactions are not filtered, 1 indicates that distributed transactions are filtered (if only global tables are involved in distributed transactions, distributed transactions are not filtered), Filter 2 for distributed transactions, but distributed transaction log records - > < property name = "handleDistributedTransactions" > 0 < / property > <! -- off heap for merge/order/group/limit 1 enable 0 disable --> <property name="useOffHeapForMerge">0</property> <! <property name="memoryPageSize"> 64K </property> <! <property name="useStreamOutput">0</property> <! < <property name="useStreamOutput">0</property> <! Property name="systemReserveMemorySize"> 384M </property> <! <property name="useZKSwitch">false</property> <! <property name="strictTxIsolation">false</property> <property name="useZKSwitch">true</property> </system> <! -- name="root" --> <user name="root"> <! --> <property name="password">123456</property> <property name="schemas">userDB</property> </user> </mycat:server>Copy the code
Once the configuration is complete, we can start Mycat:
Go to the bin directory./mycat startCopy the code
Verify that read/write separation has been successfully implemented
First of all, we add two pieces of data to the primary server. Since we implement mysql master/slave replication, the two pieces of data added at this time will be added to the other two secondary servers at the same time. The data are as follows:
The data added to the primary server will be the same on each server due to the MySQL master slave replication, but the data added to the secondary server will not be on the primary server and the other server, so we will use this feature to add data.
Add data to secondary server 192.168.222.134 as follows:
Server 134 has data that other servers do not have. Because we have two write servers in the schema: server 132 and server 133/134, Mycat should read data randomly from one of 133/134. We log in Mycat and open the user table. Refresh a few times and you’ll see that data with ID 5 is up and down
Please check whether Mycat has been successfully started. If not, you can enter the logs directory under Mycat and open the wrapper.log file to check the cause
Note:
- When configuring login users, our server must correspond to the logical library in our Schemas, otherwise an error will be reported.
- If you can’t access Mycat, check port 8066 to see if the port is open as a bottomline blogger