Mycat installation and configuration files

Mycat installation requires JDK;

    Download and unzip the official website: http://www.mycat.io/ ( Note Mycat library: http://dl.mycat.io/)

    Start and shutdown Mycat commands:

        Enter the commands in the bin directory: ./mycat start and ./mycat stop

Configuration: (personally understand some important configuration files)

The Mycat directory format refers to the common format (bin/conf/logs/lib)

server.xml: Mycat server parameter adjustment and user authorization

schema.xml: configuration file for logic library definition and table and shard definition (set logic library, table, data node information)

rule.xml: The configuration file of the sharding rule, the specific parameter information of the sharding rule is stored separately as a file

Log file: The log is stored in logs/log, one file per day, the configuration of the day is in conf/log4j.xml, the output level can be adjusted as needed (debug is recommended, more information can be output)

3. Mycat uses: (configuration file analysis)

3.1 server.xml:

3.1.1 <system> tag:

Configure all mycat parameter settings (resolver, port (default 8066), etc.), use the <property> tag configuration

Note: the crossed out at the beginning of q is the personal interpretation version, to be verified

<system>
        <property name="nonePasswordLogin">0</property> <!-- 0 is for logging in with a password, 1 is for logging in without a password, the default is 0-->
	<property name="useHandshakeV10">1</property><!-- q: Send handshake packet-->
	<property name="useSqlStat">0</property> <!-- 1 is to enable real-time statistics, 0 is to disable-->
	<property name="useGlobleTableCheck">0</property> <!-- 1 is to enable full overtime consistency check, 0 is to disable -->
	<property name="sequnceHandlerType">2</property><!--q:"0" means to use the local file to configure the mycat global serial number, corresponding to the sequence_conf.properties file; "1" means to use the database table to configure mycat global serial number -->
    <!-- <property name="useCompression">1</property>--> <!--1 is to enable mysql compression protocol-->
    <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--Set the simulated MySQL version number-->
	<!-- <property name="processorBufferChunk">40960</property> -->
	<!--
	<property name="processors">1</property>
	<property name="processorExecutor">32</property> -->
	<property name="processorBufferPoolType">0</property>
    <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool-->		
	<!--The default is 65535 64K maximum text length for sql parsing-->
	<!--<property name="maxStringLiteralLength">65535</property>-->
	<!--<property name="sequnceHandlerType">0</property>-->
	<!--<property name="backSocketNoDelay">1</property>-->
	<!--<property name="frontSocketNoDelay">1</property>-->
	<!--<property name="processorExecutor">16</property>-->
	<!--
		<property name="serverPort">8066</property>
		<property name="managerPort">9066</property>
		<property name="idleTimeout">300000</property>
		<!-- The default idle timeout time of the specified connection is 30 minutes. When configuring, the data source timeout time should be less than the idle timeout time of mycat -->
		<property name="processors">32</property> --> <!--Distributed transaction switch, 0 means not filtering distributed transactions, 1 means filtering distributed transactions (if only global tables are involved in distributed transactions, Then do not filter), 2 is not to filter distributed transactions, but to record distributed transaction logs -->
	<property name="handleDistributedTransactions">0</property>
	<!--
		off heap for merge/order/group/limit 1 on 0 off
	-->
	<property name="useOffHeapForMerge">1</property>
	<!--The unit is m-->
    <property name="memoryPageSize">64k</property>
	<!--The unit is k-->
	<property name="spillsFileBufferSize">1k</property>
	<property name="useStreamOutput">0</property>
	<!--The unit is m-->
	<property name="systemReserveMemorySize">384m</property>
	<!--Whether using zookeeper to coordinate switching-->
	<property name="useZKSwitch">true</property>
	<!-- XA Recovery Log log path-->
	<!--<property name="XARecoveryLogBaseDir">./</property>-->
	<!-- XA Recovery Log log name-->
	<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
	<!-- Additional properties:
		charset : The initial character set of the connection, the default is utf8
		txlsolation: The initialization transaction isolation level of the front-end connection, the default is the same as the repeated_read repeatable read isolation level of mysql
		sqlExecuteTimeout: sql execution timeout, the default is 300 seconds
	-->
</system>

3.1.2<user>

Configure user parameters

 
 
        <user name="root">
		<property name="password">123456</property>
		<property name="schemas">TESTDB</property>
		<!-- Table-level DML permission settings-->
		<!-- 		
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>		
		 -->
	</user>

3.1.3 Other tags

Firewall:

<!-- Global SQL Firewall Settings-->
	<!--Whitelist can use wildcard % or *-->
<!--
	<firewall>
	   <whitehost>
	      <host host="1*7.0.0.*" user="root"/>
	   </whitehost>
       <blacklist check="false">
       </blacklist>
	</firewall>
-->
3.2 schema.xml

3.2.1 <schema> tag

    There can be multiple logical library configurations; if this tag is not configured, all table configurations will belong to a default logical library.

    <shceme> attribute :

       name attribute : defines the name of the logic library, which needs to correspond to the reference in server.xml

    checkSQLschema attribute : When the value is set to true, Mycat will remove the schema characters when executing the SQL statement (eg: "select * from TTESTDB.travelrecord" will be modified by Mycat to "select * from travelrecord" ), when the SQL statement does not have The name specified by the schema will not change the SQL statement. If the SQL statement with schema characters is sent to the database, it will report " (ERROR 1146 (42S02): Table ' testdb.travelrecord' doesn't exist). "Error

        sqlMaxLimit attribute: set the sql statement to automatically add the limit attribute every day

        dataNode attribute : Bind the logic library to a specific dataNode. If this attribute is defined, the logic library cannot work in the sub-database and sub-table mode, and all operations of this logic library will directly affect the bound dataNode. , this schema can be used for read-write separation and master-slave switching, which can be used directly. Note that this attribute can only be configured with one database

     Subtag <table> tag : defines the logical table, all tables that need to be split must be defined under this tag.

    <table> attribute:

            name attribute : logical table table name, the table tag name attribute in the same schema tag is unique;

            dataNode attribute : define the dataNode to which this logical table belongs, corresponding to the following dataNode label name attribute (the following methods can be used for multiple correspondences);

  1. <table name="testdb" dataNode="dn$0-2,multipleDn2$100-199" rule="auto-sharding- long" ></table>
 
  
  1. <dataNode name="dn" dataHost="localhost1" database="db$0-99" ></dataNode>
  2. <dataNode name="dn2" dataHost="localhost1" database=" db$0-99" ></dataNode>

            rule attribute : Specify the rule name used by the logical table, and there needs to be a corresponding <tableRule> tag in rule.xml.

            ruleRequired attribute : Specifies whether to bind the sharding rule. When true, a specific rule must be configured.

            primaryKey attributemarks the primary key of the logical table corresponding to the real table ( for example: the sharding rule is to use non-primary keys for sharding, then when using the primary key query, the query statement will be sent to all configured DNs, if you use This attribute configures the primary key of the real table, then mycat will cache the information of the primary key and the specific DN, then when the non-primary key is used for the query again, the broadcast query will not be performed, and the statement will be sent directly to the specific DN, but despite the configuration This property, if the cache does not hit, will still send a statement to the specific DN to get the data . )

            type attribute : define the logical table type, global (global table) & none (normal table)

            autoIncrement attributeMySQL will not return the result using last_insert_id() for non-self-increasing primary keys, but will only return 0. Therefore, only tables that define self-increasing primary keys can use last_insert_id() to return the primary key value. mycat provides the function of self-increasing primary key, but the data table on the corresponding mysql node does not have auto_increment, so calling last_insert_id() at the mycat layer will not return results. If you use this feature, it is best to use the global sequence of the database schema.

            needAddLimit attribute : specify whether the table needs to automatically add limit to the sql statement, this attribute defaults to true, the default is 100, you can set false to turn off this function

           

 
 
        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
		<!-- auto sharding by id (long) -->
		<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />


		<!-- global table is auto cloned to all defined data nodes ,so can join
			with any table whose sharding node is in the same data node -->
		<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
		<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
		<!-- random sharding using mod sharind rule -->
		<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
			   rule="mod-long" />
		<!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
			needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
			rule="mod-long" /> -->
		<table name="employee" primaryKey="ID" dataNode="dn1,dn2"
			   rule="sharding-by-intfile" />
		<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
			   rule="sharding-by-intfile">
			<childTable name="orders" primaryKey="ID" joinKey="customer_id"
						parentKey="id">
				<childTable name="order_items" joinKey="order_id"
							parentKey="id" />
			</childTable>
			<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
						parentKey="id" />
		</table>
		<!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
			/> -->
</schema>

<ChildTable> tag:   The childTable tag is used to define the child table of the ER shard. It is associated with the parent table through attributes on the label.

    joinKey attribute : When inserting into the child table, the value of this column will be used to find the data node stored in the parent table.

    parentKey attribute : The value specified by the attribute is generally the column name associated with the parent table. The program first obtains the value of joinkey, and then generates a query statement through the column name specified by the **parentKey** attribute, and executes the statement to obtain which shard the parent table is stored on. Thereby, the location where the sub-table is stored is determined.

        <table name="customer" primaryKey="ID" dataNode="dn1,dn2"  
            rule="sharding-by-intfile">  
            <childTable name="orders" primaryKey="ID" joinKey="customer_id"  
                parentKey="id">  
                <childTable name="order_items" joinKey="order_id"  
                    parentKey="id" />  
            </childTable>  
            <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"  
                parentKey="id" />  
        </table>  

3.2.2 <dataNode> tag

Configure sharding. After table slicing, you need to configure which databases are mapped to. The sharding of mycat is actually the alias of the library.

    name attribute : Define the data node name, unique.

    dataHost attribute:  defines which physical library the shard belongs to, corresponding to the name attribute on the <datahost> tag

    dataBase attribute: defines that the shard belongs to the physical library on the database instance

        <dataNode name="dn1" dataHost="localhost1" database="db1" />
	<dataNode name="dn2" dataHost="localhost1" database="db2" />
	<dataNode name="dn3" dataHost="localhost1" database="db3" />

3.2.3 <dataHost> tag

Defined physical library sharding mapping, configurable read-write separation, single library and heartbeat statement

    name attribute : identifies the dataHost tag, unique.

    maxCon attribute : Specify the maximum connection of each read-write instance connection pool.

    minCon attribute : Specify the minimum connection of each read-write instance connection pool, and initialize the connection pool size.

    balance attribute: specify the type of load balancing

        {"0" : Do not enable read-write separation, all operations are sent to writeHost--"1" : All readHost and stand by writeHost participate in the load balancing of the select statement, that is, all write operations are performed by the first writeHost, and read operations are performed by All other available hosts participate-- "2" : All select operations are randomly distributed to all hosts--"3" : Newly added after version 1.4, all select operations are performed by readHost corresponding to writeHost,}

    writeType attribute:  configure master-slave related attributes

        {"-1" : No automatic switch--"1" default value, automatic switch--"2": Based on MySql master-slave synchronization status to decide whether to switch, the heartbeat statement is show slace status -- "3" : 1.4.1 , Based on the switching mechanism of mysql galary cluster (in cluster state), the heartbeat statement is show status like 'wsrep%'}.

    dbType attribute: The database that specifies the backend connection currently supports the binary mysql protocol, and there are other databases linked using jdbc.

    DBDriver property:  Specifies the driver used by the database after the connection. Currently, the optional values ​​are native and JDBC. If you use native, because this value executes the binary mysql protocol, you can use mysql and maridb, and other types need to be supported by the JDBC driver. If you use JDBC, you need to put the driver jar that conforms to the JDBC4 standard in the mycat\lib directory, and check that the driver jar package includes the following directory structure file  META-INF\services\java.sql.Driver. Write the specific driver class name in this file, such as com.mysql.jdbc.Driver

    connectionInitSql attribute : mainly when using the Oracla database, the initialization SQL statement that needs to be executed is put here

<heartbeat>: Specifies the statement used for heartbeat detection by the physical library

<writeHost> <readHost>

Specify the relevant configuration of the backend database to mycat for instantiating the backend connection pool.

host attribute : used to identify different instances, generally we use *M1 for writeHost, and *S1 for readHost.
url attribute: the connection address of the backend instance. If the native dbDriver is used, it is generally in the form of address:port. With JDBC or other dbDriver, you need to specify it. When using JDBC, you can write: jdbc:mysql://localhost:3306/.
user attribute: the user name required by the backend storage instance
password attribute: the password required by the backend storage instance


        <dataHost name="localhost1" 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="localhost:3306" user="root"
				   password="123456">
			<!-- can have multi read hosts -->
			<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />
		</writeHost>
		<writeHost host="hostS1" url="localhost:3316" user="root"
				   password="123456" />
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>
3.3 rule.xml

Configure specific table sharding rules


Guess you like

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