配置MySql主从数据库,MyCat分库分表,读写分离中间件(纯干货,无美图)

569 阅读23分钟

先点赞,后观看,伸手才有好习惯

  • 基于Java环境开发
  • 主要配置文件 schema.xml(逻辑数据库)、rule.xml(分片规则)、server.xml(一些系统和用户)

理解:Mycat作为一个逻辑数据库,是需要依赖下面的真实数据库

配置主从

  • 坑点及建议:
    1、因为关注点在主从,别花太多时间在一台单机安装mysql两个端口,可尝试虚拟机两台
    2、mysql5.7跟之前的版本不一样,windows上面测试的朋友my.ini可以自己新建
    3、对于my.ini的各个参数的解释请随机去百度,这里有我的转载

  • 实操
    假设现在两台数据库的mysql均已正常安装,配置如下

服务器 IP 账号 密码
A AUSER APWD
B BUSER BPWD

一般进my.ini只需要更新以下所给信息(有则改之无则加):

主库配置及步骤

[mysqld]
#binlog格式,分三种:statement level,rowlevel,mixed
#三种模式的差别介绍可以看:https://juejin.cn/post/6844904019815596046
binlog_format=mixed
#为服务器标识,主从一定不要一样  
server-id   = 1
#清理二进制日志的时间间隔
expire_logs_days = 10
#是需要同步的数据库  
binlog-do-db
#不需要同步的数据库
binlog-ignore-db = mysql                
binlog-ignore-db = test  

#设置gtid同步方式
gtid_executed_compression_period = 1000 #1000默认
gtid_mode = on #默认off
enforce_gtid_consistency = on #默认off

1、连接主库,并进入mysql

>mysql -u数据库用户名 -p数据库密码

2、为从库创建授权用户slave,密码slave ,B为对应的IP

grant replication slave on *.* to 'slave'@'B' identified by 'slave' ;

3、刷新权限信息

flush privileges;

4、自行创建数据库和数据表并插入相应数据,mysql默认InnoDB,主库可以不用修改引擎

从库配置及步骤

my.ini文件

[mysqld]
#binlog格式,分三种:statement level,rowlevel,mixed
#三种模式的差别介绍可以看:
binlog_format=mixed
#为服务器标识,主从一定不要一样  
server-id   = 13
#清理二进制日志的时间间隔
expire_logs_days = 10
#是需要同步的数据库  
binlog-do-db
#设置gtid同步方式
gtid_executed_compression_period = 1000 #1000默认
gtid_mode = on #默认off
enforce_gtid_consistency = on #默认off

1、连接从库,并进入mysql

>mysql -u数据库用户名 -p数据库密码

2、复制一份主库的数据库到从库,并赋予slave权限

grant all privileges on *.* to 'slave'@'%' identified by 'slave' with grant option;

3、刷新权限或者退出mysql命令行重启mysql服务

4、master建立数据同步

change master to master_host='A',master_user='slave',master_password='slave',master_auto_position=1;

重要说明:部分教程用的master_log_file & master_log_pos 参数来指定,但是slave一旦出现问题,无法确认断节点,数据容易造成不一致,所以才引入gtid(即global transaction ID全局事务ID),想看相应介绍的可以去mysql官网里面搜寻相应的版本然后看看.

5、mysql命令查看从库数据库状态

mysql>show slave status \G;

当显示的数据内有:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
就说明可以了

6、自行验证主库更新数据对从库的影响

Mycat

mycat架构图

avatar

MyCat简介

简介个毛线,[MyCat官网](http://mycat.io/)那么详细的介绍不看,非要听我在这摘录?

环境安装

由于Mycat是基于Java开发的,所以JDK环境先安装好,再去安装Mycat,安装教程一大把,我就不赘述了

实现过程

1、为简单明了的看清配置信息,会删除不影响结果的注释,且本记录只针对一个主库和一个从库,分库分表只需要加对应的配置即可,配置文件中有相应的详细说明
2、可以进入mysql把从库的引擎改成MyISAM,如果不想从库有写的功能,也可以将mysql设置成只读数据库

schema.xml 配置信息(完整的配置信息及解释在文末):
主要是配置读写数据库信息和对应表信息

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

	<!-- 每一个schema代表一个逻辑数据库 
	    这里的name是程序端使用的数据库名称,对应的数据库账号密码在server.xml中
	-->
	<schema name="MycatDemo" checkSQLschema="true" sqlMaxLimit="100">
		<!--  auto sharding by id (long)
			每个逻辑数据库下面对应的是所有的表,下方RDB代表真实数据库
			name:对应RDB中的表名
			primaryKey:RDB中该表的主键
			dataNode:这些表对应的数据库
			rule:对应的分片规则
		-->
		<table name="article" primaryKey="article_id" autoIncrement="true" dataNode="dn1"
			   rule="mod-long" />
	</schema>
	
	<!-- 各个数据结点的信息,便于上方schema使用 

	如果是多个数据结点和多个host,那就同步复制一份dataNode和dataHost数据,然后写上对应的配置信息
		name:结点的名称
		dataHost:结点的主机地址
		database:RDB数据库名称
	-->
	<dataNode name="dn1" dataHost="localhost1" database="demo" />
	
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<!-- 心跳语句检测,检测对应的mysql是否正常运行 -->
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="11.11.11.11:3306" user="root"
				   password="root">
			<!-- can have multi read hosts -->
			<readHost host="hostS2" url="22.22.22.22:3306" user="root" password="root" />
		</writeHost>
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>
	
</mycat:schema>

rule.xml 配置信息(完整的配置信息及解释在文末): 主要定义一些分片规则和生成规则之类的

这里尤其要注意一点,有个叫mod-long的function在schema.xml中引用到了,但是我只做了一分数据库,所以这里我把默认的3改成了1,如果你有对应的多个数据库分表,则改成相应的数量。

<?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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<tableRule name="rule1">
		<rule>
			<columns>id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="rule2">
		<rule>
			<columns>user_id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="sharding-by-intfile">
		<rule>
			<columns>sharding_id</columns>
			<algorithm>hash-int</algorithm>
		</rule>
	</tableRule>
	<tableRule name="auto-sharding-long">
		<rule>
			<columns>id</columns>
			<algorithm>rang-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="mod-long">
		<rule>
			<columns>article_id</columns>
			<algorithm>mod-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-murmur">
		<rule>
			<columns>id</columns>
			<algorithm>murmur</algorithm>
		</rule>
	</tableRule>
	<tableRule name="crc32slot">
		<rule>
			<columns>id</columns>
			<algorithm>crc32slot</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-month">
		<rule>
			<columns>create_time</columns>
			<algorithm>partbymonth</algorithm>
		</rule>
	</tableRule>
	<tableRule name="latest-month-calldate">
		<rule>
			<columns>calldate</columns>
			<algorithm>latestMonth</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="auto-sharding-rang-mod">
		<rule>
			<columns>id</columns>
			<algorithm>rang-mod</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="jch">
		<rule>
			<columns>id</columns>
			<algorithm>jump-consistent-hash</algorithm>
		</rule>
	</tableRule>

	<function name="murmur"
		class="io.mycat.route.function.PartitionByMurmurHash">
		<property name="seed">0</property><!-- 默认是0 -->
		<property name="count">1</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
		<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
		<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 -->
		<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 
			用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
	</function>

	<function name="crc32slot"
			  class="io.mycat.route.function.PartitionByCRC32PreSlot">
		<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
	</function>
	<function name="hash-int"
		class="io.mycat.route.function.PartitionByFileMap">
		<property name="mapFile">partition-hash-int.txt</property>
	</function>
	<function name="rang-long"
		class="io.mycat.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
	</function>
	<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">1</property><!-- 只有一个数据库我改成了1,默认3 -->
	</function>

	<function name="func1" class="io.mycat.route.function.PartitionByLong">
		<property name="partitionCount">8</property>
		<property name="partitionLength">128</property>
	</function>
	<function name="latestMonth"
		class="io.mycat.route.function.LatestMonthPartion">
		<property name="splitOneDay">24</property>
	</function>
	<function name="partbymonth"
		class="io.mycat.route.function.PartitionByMonth">
		<property name="dateFormat">yyyy-MM-dd</property>
		<property name="sBeginDate">2015-01-01</property>
	</function>
	
	<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
        	<property name="mapFile">partition-range-mod.txt</property>
	</function>
	
	<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
		<property name="totalBuckets">3</property>
	</function>
</mycat:rule>

server.xml 配置信息(完整的配置信息及解释在文末):
主要定义sql服务相关的参数,例如账号密码,sql事务及最大文本

<?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/">
	<system>
	<property name="useSqlStat">0</property>  <!-- 1为开启实时统计、0为关闭 -->
	<property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->

		<property name="sequnceHandlerType">2</property>
      <!--  <property name="useCompression">1</property> --> <!--1为开启mysql压缩协议-->
        <!--  <property name="fakeMySQLVersion">5.6.20</property> --> <!--设置模拟的MySQL版本号-->
	<!-- <property name="processorBufferChunk">40960</property> -->
	<!-- 
	<property name="processors">1</property> 
	<property name="processorExecutor">32</property> 
	 -->
		<!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
		<property name="processorBufferPoolType">0</property>
		<!-- 默认是65535 64K 用于sql解析时最大文本长度  -->
		<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> <property name="bindIp">0.0.0.0</property> 
			<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>
		<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
		<property name="handleDistributedTransactions">0</property>
		
			<!--
			off heap for merge/order/group/limit      1开启   0关闭
		-->
		<property name="useOffHeapForMerge">1</property>

		<!--
			单位为m
		-->
		<property name="memoryPageSize">1m</property>

		<!--
			单位为k
		-->
		<property name="spillsFileBufferSize">1k</property>

		<property name="useStreamOutput">0</property>

		<!--
			单位为m
		-->
		<property name="systemReserveMemorySize">384m</property>


		<!--是否采用zookeeper协调切换  -->
		<property name="useZKSwitch">true</property>


	</system>
	
	<!-- 全局SQL防火墙设置 -->
	<!-- 
	<firewall> 
	   <whitehost>
	      <host host="127.0.0.1" user="mycat"/>
	      <host host="127.0.0.2" user="mycat"/>
	   </whitehost>
       <blacklist check="false">
       </blacklist>
	</firewall>
	-->
	<!-- 相当于Mycat逻辑数据库的管理员账号 数据库名跟schema.xml中定义的保持一致-->
	<user name="mycatuser">
		<property name="password">mycatpwd</property>
		<property name="schemas">MycatDemo</property>
		<property name="defaultAccount">true</property>
		<!-- 表级 DML 权限设置 -->
		<!-- 		
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>		
		 -->
	</user>
	<!-- 相当于Mycat逻辑数据库的只读账号 数据库名跟schema.xml中定义的保持一致-->
	<user name="user">
		<property name="password">user</property>
		<property name="schemas">MycatDemo</property>
		<property name="readOnly">true</property>
	</user>

</mycat:server>

程序中连接数据库的时候就不再是单纯的mysql的连接和账号密码了,而是上面定义的:
数据库名:MycatDemo(schema.xml中)
账号:mycatuser(server.xml中)
密码:mycatpwd(server.xml中)

验证读写分离

进入mysql命令行,开启主从两个数据库的sql日志,通过sql日志执行的情况就能判断读写使用的哪个库

#查看日期情况

mysql>show variables like '%general%';

#开启日志

mysql>SET GLOBAL general_log = 'On';

#指定日志文件

mysql>SET GLOBAL general_log_file = '/var/lib/mysql/mysql.log';

Mycat性能监控

mycat有性能监控的WEB管理端软件,有需要的自行下载安装

配置文件说明

server.xml文件说明

<?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/">
	<system>
	<property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
	<property name="useHandshakeV10">1</property>
	<property name="useSqlStat">0</property>  <!-- 1为开启实时统计、0为关闭 -->
	<property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->

	<property name="sequnceHandlerType">2</property> <!--0 本地文件方式  1 数据库方式  2 时间戳方式-->
	<property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
        <!--  <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
	<!-- <property name="processorBufferChunk">40960</property> -->
	<!-- 
	<property name="processors">1</property> 
	<property name="processorExecutor">32</property> 
	 -->
        <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
		<property name="processorBufferPoolType">0</property>
		<!--默认是65535 64K 用于sql解析时最大文本长度 -->
		<!--<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> <property name="bindIp">0.0.0.0</property> 
			<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
		<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
		<property name="handleDistributedTransactions">0</property>
		
			<!--
			off heap for merge/order/group/limit      1开启   0关闭
		-->
		<property name="useOffHeapForMerge">1</property>

		<!--
			单位为m
		-->
        <property name="memoryPageSize">64k</property>

		<!--
			单位为k
		-->
		<property name="spillsFileBufferSize">1k</property>

		<property name="useStreamOutput">0</property>

		<!--
			单位为m
		-->
		<property name="systemReserveMemorySize">384m</property>


		<!--是否采用zookeeper协调切换  -->
		<property name="useZKSwitch">false</property>

		<!-- XA Recovery Log日志路径 -->
		<!--<property name="XARecoveryLogBaseDir">./</property>-->

		<!-- XA Recovery Log日志名称 -->
		<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
		<!--如果为 true的话 严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接-->
		<property name="strictTxIsolation">false</property>
		
		<property name="useZKSwitch">true</property>
		
	</system>
	
	<!-- 全局SQL防火墙设置 -->
	<!--白名单可以使用通配符%或着*-->
	<!--例如<host host="127.0.0.*" user="root"/>-->
	<!--例如<host host="127.0.*" user="root"/>-->
	<!--例如<host host="127.*" user="root"/>-->
	<!--例如<host host="1*7.*" user="root"/>-->
	<!--这些配置情况下对于127.0.0.1都能以root账户登录-->
	<!--
	<firewall>
	   <whitehost>
	      <host host="1*7.0.0.*" user="root"/>
	   </whitehost>
       <blacklist check="false">
       </blacklist>
	</firewall>
	-->

	<!--用户名是mycat-->
	<user name="mycat" defaultAccount="true">
		<!--密码是123456-->
		<property name="password">123456</property>
		<!---可访问的逻辑库有mycatdb-->
		<property name="schemas">mycatdb</property>
		
		<!-- 表级 DML 权限设置 -->
		<!-- 		
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>		
		 -->
	</user>

	<user name="user">
		<property name="password">user</property>
		<property name="schemas">mycatdb</property>
		<!--是否只读-->
		<property name="readOnly">true</property>
	</user>

</mycat:server>

schema.xml文件说明

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

	<!-- checkSQLschema这个属性默认就是false,官方文档的意思就是是否去掉表前面的数据库的名称,
	”select * from db1.testtable” ,设置为true就会去掉db1。但是如果db1的名称不是schema的名称,那么也不会被去掉,
	因此官方建议不要使用这种语法。同时默认设置为false。-->
	
    <!-- sqlMaxLimit当该值设置为某个数值时。每条执行的 SQL 语句,如果没有加上 limit 语句,MyCat 也会自动的加上所对应的值。
	例如设置值为 100,执行”select * from test_table”,则效果为“selelct * from test_table limit 100”.
	注意:如果运行的 schema 为非拆分库的,那么该属性不会生效。-->
	<schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100">
	
	    <!-- name	该属性定义逻辑表的表名 -->
		<!-- dataNode	该属性定义这个逻辑表所属的 dataNode, 该属性的值需要和 dataNode 标签中 name 属性的值相互对应。 -->
		<!-- rule	该属性用于指定逻辑表要使用的规则名字,规则名字在 rule.xml 中定义,必须与 tableRule 标签中 name 属性属性值一一对应 -->
		<!-- ruleRequired	该属性用于指定表是否绑定分片规则,如果配置为 true,但没有配置具体 rule 的话 ,程序会报错。 -->
		<!-- primaryKey	该逻辑表对应真实表的主键, -->
		<!-- type	该属性定义了逻辑表的类型,目前逻辑表只有“全局表”和”普通表”两种类型。全局表定义type=”global”,不定义的就是普通表。 -->
		<!-- autoIncrement	主键是否自增长。 -->
		<!-- subTables	分表,分表目前不支持Join。 -->
		<!-- needAddLimit是否自动添加limit,默认是开启状态。关闭请谨慎。 -->
		
		<!--rule="mod-long"  指定规则在rule中配置-->
		<table name="users" primaryKey="id" dataNode="dn1"/>
		<table name="orders" primaryKey="id" dataNode="dn1"/>
		
		
		<table name="product" primaryKey="id" dataNode="dn2"/>
		<table name="news" primaryKey="id" dataNode="dn2"/>
		

	</schema>

	<!--  Name	定义数据节点的名字,这个名字需要是唯一的-->
    <!-- dataHost	该属性用于定义该分片属于哪个数据库实例 -->
	<!-- Database	该属性用于定义该分片属性哪个具体数据库实例上的具体库 -->
	<dataNode name="dn1" dataHost="localhost1" database="sharding1" />
	<dataNode name="dn2" dataHost="localhost2" database="sharding2" />

	<!--  name	唯一标识 dataHost 标签,供上层的标签使用-->
    <!-- maxCon	指定每个读写实例连接池的最大连接。 -->
	<!-- minCon	指定每个读写实例连接池的最小连接,初始化连接池的大小。 -->
	<!-- balance	负载均衡类型,目前的取值有4 种: 
	“0”, 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。 
	“1”,全部的 readHost 与 stand by writeHost(非主非从) 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。
	”2”,所有读操作都随机的在 writeHost、readhost 上分发。
	”3”,所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压writeType 1. writeType=”0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .2. writeType=”1”,所有写操作都随机的发送到配置的 writeHost,1.5 以后废弃不推荐。默认0就好了!-->
    <!-- dbType	指定后端连接的数据库类型,目前支持二进制的 mysql 协议,还有其他使用 JDBC 连接的数据库。例如:mongodb、oracle、spark 等. -->
	<!-- dbDriver	指定连接后端数据库使用的 Driver,目前可选的值有 native 和 JDBC。使用 native 的话,因为这个值执行的是二进制的 mysql 协议,所以可以使用 mysql 和 maridb。其他类型的数据库则需要使用 JDBC 驱动来支持。 -->
	<!-- switchType	“-1” 表示不自动切换; “1” 默认值,自动切换; 
					“2” 基于 MySQL 主从同步的状态决定是否切换心跳语句为 show slave status; 
					“3” 基于 MySQL galary cluster 的切换机制(适合集群)(1.4.1)心跳语句为 show status like ‘wsrep%’.					
	-->
	<!--slaveThreshold:指定从节点的最大个数-->
   

	<dataHost name="localhost1" 
			  maxCon="1000" 
			  minCon="10" 
			  balance="0"
			  writeType="0" 
			  dbType="mysql" 
			  dbDriver="native" 
			  switchType="1"  
			  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		
		<!--host	用于标识不同实例,一般 writeHost 我们使用*M1,readHost 我们用*S1。-->
		<!--url	后端实例连接地址。Native:地址:端口 JDBC:jdbc的url-->
		<!--password	后端存储实例需要的密码-->
		<!--user	后端存储实例需要的用户名字-->
		<!--weight	权重 配置在 readhost 中作为读节点的权重-->
		<!--usingDecrypt	是否对密码加密,默认0。具体加密方法看官方文档。-->
		<writeHost host="hostM1" url="47.94.158.155:3306" user="root"
				   password="TJXtjx_19991007">
		</writeHost>
	</dataHost>
	
	
	<dataHost name="localhost2" 
			  maxCon="1000" 
			  minCon="10" 
			  balance="0"
			  writeType="0" 
			  dbType="mysql" 
			  dbDriver="native" 
			  switchType="1"  
			  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<writeHost host="hostM2" url="47.94.158.155:3306" user="root"
				   password="TJXtjx_19991007">
		</writeHost>
	</dataHost>

</mycat:schema>

rule.xml文件说明

<?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:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
	<tableRule name="rule1">
		<rule>
			<columns>id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="rule2">
		<rule>
			<columns>user_id</columns>
			<algorithm>func1</algorithm>
		</rule>
	</tableRule>

	<tableRule name="sharding-by-intfile">
		<rule>
			<columns>sharding_id</columns>
			<algorithm>hash-int</algorithm>
		</rule>
	</tableRule>
	<tableRule name="auto-sharding-long">
		<rule>
			<columns>id</columns>
			<algorithm>rang-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="mod-long">
		<rule>
			<columns>id</columns>
			<algorithm>mod-long</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-murmur">
		<rule>
			<columns>id</columns>
			<algorithm>murmur</algorithm>
		</rule>
	</tableRule>
	<tableRule name="crc32slot">
		<rule>
			<columns>id</columns>
			<algorithm>crc32slot</algorithm>
		</rule>
	</tableRule>
	<tableRule name="sharding-by-month">
		<rule>
			<columns>create_time</columns>
			<algorithm>partbymonth</algorithm>
		</rule>
	</tableRule>
	<tableRule name="latest-month-calldate">
		<rule>
			<columns>calldate</columns>
			<algorithm>latestMonth</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="auto-sharding-rang-mod">
		<rule>
			<columns>id</columns>
			<algorithm>rang-mod</algorithm>
		</rule>
	</tableRule>
	
	<tableRule name="jch">
		<rule>
			<columns>id</columns>
			<algorithm>jump-consistent-hash</algorithm>
		</rule>
	</tableRule>

	<function name="murmur"
		class="io.mycat.route.function.PartitionByMurmurHash">
		<property name="seed">0</property><!-- 默认是0 -->
		<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
		<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
		<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 -->
		<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 
			用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
	</function>

	<function name="crc32slot"
			  class="io.mycat.route.function.PartitionByCRC32PreSlot">
	</function>
	<function name="hash-int"
		class="io.mycat.route.function.PartitionByFileMap">
		<property name="mapFile">partition-hash-int.txt</property>
	</function>
	<function name="rang-long"
		class="io.mycat.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
	</function>
	<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
		<!-- how many data nodes -->
		<property name="count">3</property>
	</function>

	<function name="func1" class="io.mycat.route.function.PartitionByLong">
		<property name="partitionCount">8</property>
		<property name="partitionLength">128</property>
	</function>
	<function name="latestMonth"
		class="io.mycat.route.function.LatestMonthPartion">
		<property name="splitOneDay">24</property>
	</function>
	<function name="partbymonth"
		class="io.mycat.route.function.PartitionByMonth">
		<property name="dateFormat">yyyy-MM-dd</property>
		<property name="sBeginDate">2015-01-01</property>
	</function>
	
	<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
        	<property name="mapFile">partition-range-mod.txt</property>
	</function>
	
	<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
		<property name="totalBuckets">3</property>
	</function>
</mycat:rule>