Mycat实现postgresql的分库分表

mycat的介绍和安装这里就不扯了,百度介绍的都挺详细的。

mycat支持常用的几种数据源,原理就是通过代码把不同的数据源代理成一个虚拟的mysql数据库,然后项目中直接访问虚拟的这个mysql数据源就可以了。

对mycat的配置超级简单,只是单独修改conf中的几个xml文件即可实现不同的配置,其中最重要的几个xml包括:server.xml(数据库用户等基本设置配置)、schema.xml(虚拟数据源配置)、rule.xml(数据分片设置)。

直接上操作,第一步配置一个虚拟的数据源:
打开conf的schema.xml文件,schema节点代表一个数据库,schema下的table节点代表一张数据表,然后在table节点中指定数据的分区、数据写入策略、主键字段等等一些信息,下面给个示例:

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

    <!--
	   创建一个名字叫TESTDB的数据库
	   name:数据库的名字
	   checkSQLschema:是否检查sql 当该值为true时,例如我们执行语句select * from TESTDB.company 。mycat会把语句修改为 select * from company 去掉TESTDB
	   sqlMaxLimit:当该值设置为某个数值时,每条执行的sql语句,如果没有加上limit语句,Mycat会自动加上对应的值。不写的话,默认返回所有的值
	-->
	<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
	    <!--
		添加表名为test的表到TESTDB中
		name:表名
		primaryKey:主键字段
		dataNode:分区,对应dataNode的name字段,多个之间用逗号分隔
		rule:数据分区策略,对应rule.xml中配置的策略
		-->
		<table name="test" primaryKey="id" dataNode="dn1,dn2" rule="sharding-by-pattern" />
	</schema>
	
	<!-- 
	创建一个名字为dn1的分区
	name:分区名称
	dataHost:分区的读写数据源配置
	database:数据库名称,自定义,目测没什么卵用
	-->
	<dataNode name="dn1" dataHost="localhost1" database="db1" />
	<dataNode name="dn2" dataHost="localhost2" database="db2" />
	
	
	<!-- 
	创建一个名字叫localhost1的分区数据源
	name:数据源名称
	maxCon:最大连接数
	minCon:最小连接数
	balance:负载均称类型 
	              0:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上
				  1:全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1-S1,M2-S2 并且M1 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。
				  2:所有读操作都随机的在writeHost、readHost上分发
				  3:所有读请求随机的分发到writeHst对应的readHost执行,writeHost不负担读写压力。(1.4之后版本有)
    writeType:写数据负载类型
				  0:所有的写操作发送到配置的第一个writeHost,第一个挂了切换到第二个。切换记录在文件dnindex.properties
                  1:所有的鞋操作都随机的发送到配置的writeHost,1.5以后版本废弃不推荐。
    dbType:数据源类型
	dbDriver:指定连接后段数据库使用的driver,目前可选的值有native和JDBC。使用native的话,因为这个值执行的是二进制的mysql协议,所以可以使用mysql和maridb,其他类型的则需要使用JDBC驱动来支持。
	-->
	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="postgresql" dbDriver="jdbc">
	    <!--心跳检测sql-->
		<heartbeat>select user</heartbeat>
		<!-- 读写数据源配置 -->
		<writeHost host="hostM1" url="jdbc:postgresql://39.100.0.174:5432/prime_server_production_shanghai" user="postgres"
				   password="postgres">
			<!-- 读数据源配置 -->
			<readHost host="hostS1" url="jdbc:postgresql://39.100.0.174:5432/prime_server_production_shanghai" user="postgres" password="postgres" />
		</writeHost>
	</dataHost>
	<dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
			  writeType="0" dbType="postgresql" dbDriver="jdbc">
		<heartbeat>select user</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM2" url="jdbc:postgresql://47.105.165.101:5432/prime_server_production_shanghai" user="postgres"
				   password="postgres">
			<!-- can have multi read hosts -->
			<readHost host="hostS2" url="jdbc:postgresql://47.105.165.101:5432/prime_server_production_shanghai" user="postgres" password="postgres" />
		</writeHost>
		<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>
</mycat:schema>

上面代码中table节点rule的值是在rule.xml中自定义的一个数据分区策略,
在rule.xml中配置生成策略贼鸡儿简单,就两个节点,function和tableRule,这里我们生成一个名字叫sharding-by-pattern的分区策略:

<!--
	分区策略
	columns:id字段名称
	algorithm:分区方法名称,对应function节点的namae
	-->
<tableRule name="sharding-by-pattern">
          <rule>
                <columns>id</columns>
                <algorithm>sharding-by-pattern</algorithm>
          </rule>
 </tableRule>
 
 <!-- 
	分区方法
	class="io.mycat.route.function.PartitionByPattern"代表根据匹配算法分区   算法为:根据id%patternValue的余值判断写入到那个分区中
	patternValue:除数
	defaultNode:默认分区   第几个分区,从0开始计数
	mapFile:配置文件
	-->
 <function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern">
		<property name="patternValue">2</property>
		<property name="defaultNode">0</property>
		<property name="mapFile">partition-pattern.txt</property>
 </function>

partition-pattern.txt

# 余数为0到0之间的时候把数据存入下标为1的分区
# 余数为1到1之间的时候把数据存入下标为0的分区
0-0=1
1-1=0

最后一步,配置一下虚拟数据库的用户名密码,打开server.xml文件:

	<!--
	创建一个用户名为postgres的用户
	password:用户密码
	schemas:数据库名称,对应schema.xml中的schema名称
	-->
	<user name="postgres">
		<property name="password">postgres</property>
		<property name="schemas">TESTDB</property>
	</user>

ojbk,打开mycat解压目录下的bin文件,用cmd打开startup_nowrap.bat,这里是win版本的,其他版本启动方式另行百度。
在这里插入图片描述
需要注意的是我们访问mycat的数据源的时候端口号为8066,下面我们用Navicat连接一下数据源试试:
在这里插入图片描述
在这里插入图片描述
连接正常,需要注意的是分库分表后数据插入时自增主键需要程序生成,例如把表名跟主键以key,value的形式存入redis中,插入一条则value加1(redis的increment方法)等等。

刚开始学习,仅做笔记。
发布了43 篇原创文章 · 获赞 126 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/W_DongQiang/article/details/101209895