Home  >  Article  >  Database  >  How to configure MyCat

How to configure MyCat

一个新手
一个新手Original
2017-09-08 11:08:272989browse

schema.xml 涵盖了MyCat的逻辑库、表、分片规则、分片节点及数据源。

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<!-- schema 定义mycat中的逻辑库,可以有多个逻辑库,
	1)dataNode属性:绑定逻辑库到具体的Database上面,
	2)checkSQLschema:如果为true,则会替换掉schema,如果为false则不会;
	3)sqlMaxLimit:如果带了该属性,则每次执行sql的时候如果sql没有limit则会带上这个limit,如果schema为非拆分库,则该属性不会生效。-->
	<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
		<!-- table标签定义了MyCat中的逻辑表,所有拆分的表都需要在table标签中定义。 -->
		<!-- 1)name属性:定义逻辑表的名称-->
		<!-- 2)dataNode属性:定义逻辑表所属的dataNode,如果需要引用多个dataNode,则可以用dataNode="dn$0-99" 来代表dn0到dn99的数据库-->
		<!-- 3)rule属性:用来指定逻辑表使用的规则名字,规则名字在rule.xml中定义。-->
		<!-- 4)ruleRequired属性:该属性用于指定表是否绑定分片规则,如果配置为true,但是没有具体的分片规则,则会报错。-->
		<!-- 5)type属性:定义逻辑表的类型,分为"全局表(global)"和"普通表"两种类型,不设置该值的时候未global的所有表。-->
		<!-- 6)autoIncrement属性:使用该值的时候需要定义auto_increment,使用的时候最好配合数据库模式的全局序列。-->
		<!-- 7)subTables属性:dataNode在分表的条件下只能配置一个,不支持各种条件的Join关联查询。-->
		<!-- 8)primaryKey属性:逻辑表对应真实表的主键。-->
		<!-- 9)needAddLimit属性:指定表是否需要字段再每个语句的后面加上limit限制。-->
		<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标签用于定义E-R分片的子表,通过标签上的属性与浮表进行关联-->
			<!-- 1)name属性:定义子表的名称-->
			<!-- 2)joinKey属性:插入子表时,回使用这个值查找浮表存贮的数据节点-->
			<!-- 3)parentKey属性:与父表建立关联关系的列名,程序首先获取joinKey的值,然后通过parentKey属性指定的列名产生查询语句,通过执行语句得知父表存储在哪个分片上,从而确定子表存贮的位置。-->
			<!-- 4)primaryKey:和table标签一样-->
			<!-- 5)needAddLimit:和table标签一样-->
			<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>
	<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
		/> -->
	<!-- dataNode标签定义了mycat中的数据节点,这也就是我们通常所说的数据分片,一个单独的dataNode就是一个独立的数据分片-->
    <!--1)name属性:定义数据节点的唯一名字-->
    <!--2)dataHost属性:定义该分片所属的数据库实例,属性引用自dataHost标签上定义的name属性-->
    <!--3)database属性:定义该分片所属的数据库实例上的具体数据库。-->
	<dataNode name="dn1" dataHost="localhost1" database="db1" />
	<dataNode name="dn2" dataHost="localhost1" database="db2" />
	<dataNode name="dn3" dataHost="localhost1" database="db3" />
	<!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
	 <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
	<dataNode	name="jdbc_dn2" dataHost="jdbchost" database="db2" />
	<dataNode name="jdbc_dn3" 	dataHost="jdbchost" database="db3" /> -->

	<!-- 定义数据库实例,读写分离和心跳语句-->
    <!--1)那么属性:标识唯一的dataHost,-->
    <!-- 2)maxCon属性:指定每个读写实例连接池的最大连接数。内嵌writeHost、readHost标签会使用这个属性的值来实例化连接池的最大连接数-->
    <!--3)minCon属性:指定每个读写实例连接池的最小连接数。初始化连接池的大小的属性。-->
    <!--4)balance属性:负债均衡类型,有四种-->
    <!--   balance="0" : 不开启读写分离机制,所有的读操作都发送到当前可以用的writeHost上-->
    <!--   balance="1" : 全部的readHost与stand by writeHost(双主从模式下的master) 都参与select语句的负债均衡-->
   	<!--   balance="2" : 所有的读操作都随机的往writeHost和readHost上分发-->
    <!--   balance="3" : 所有的读分发到readHost上,writeHost负责写-->
    <!--5)writeType属性:负载均衡目前的取值有两种:-->
    <!--   writeType="0":所有的写操作都发送到第一个writeHost,writeHost1挂了,则切换到writeHost2上,重新恢复writeHost1后,还是以writeHost2为准-->
    <!--   writeType="1":所有的写操作都随机的发送到配置的writeHost上,1.5版本以后不推荐使用该值。-->
    <!--6)dbType属性:制定后端后端数据的类型:mysql,oracle、mongoDB-->
    <!--7)dbDriver属性:制定后端数据库使用的Driver.目前可选的值为native和JDBC。-->
    <!--8)switchType属性:默认值为1,自动切换。-->
    <!--                  -1表示不自动切换-->
    <!--                   2表示基于mysql主从同步的状态决定是否切换。-->
    <!--                  3表示基于mysql galaxy cluster 的切换机制-->
    <!--9)tempReadHostAvailable属性:如果配置了writeHost属性,下面的readHost依旧可以使用,默认为0-->
   	
	<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>
	<!--
		<dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc">
		<heartbeat> 		</heartbeat>
		 <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" 	password="jifeng"></writeHost>
		 </dataHost>

	  <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" 	dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat>
		<connectionInitSql>alter session set nls_date_format=&#39;yyyy-mm-dd hh24:mi:ss&#39;</connectionInitSql>
		<writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" 	password="123456" > </writeHost> </dataHost>

		<dataHost name="jdbchost" maxCon="1000" 	minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc">
		<heartbeat>select 	user()</heartbeat>
		<writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>

		<dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc">
		<heartbeat> </heartbeat>
		 <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" 	password="jifeng"></writeHost> </dataHost> -->

	<!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"
		dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
		url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost>
		</dataHost> -->
</mycat:schema>

The above is the detailed content of How to configure MyCat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn