mycat

mycat

(附注:vi命令查看行数 :set nu)

难点:mysql用mycat做集群

1.业务理解:

垂直切分:业务维度

水平切分:数据维度

混合切分:业务+数据

mycat_第1张图片

2.定义(mycat)

基于MySQL的数据库中间件,用来协调切分后的数据库,使其可以进行统一管理

逻辑库(虚拟库):数据库集群对外的统一访问入口

mycat_第2张图片

3.思路:

1.schema.xml

2.rule.xml

3.server.xml

4.基础概念:
1.schema.xml 逻辑库,逻辑表,数据节点,节点主机

附注:balance=“0”---->不进行读写分离
writeHost------>代表是写服务器
writeType=“0”---->所有的写操作都发到第一个写服务器(从上到下,如果有服务器宕机,则往下追溯)
writeType=“1”---->随机分发,不按照顺序
dbDriver=“native”—>mysql和mycat高度兼容,如果是连接oracle就不能配置成native

案例:



<mycat:schema xmlns:mycat="http://io.mycat/">
    <-- ####逻辑库,逻辑表,数据节点,节点主机#### -->
	<schema name="dm_order"  checkSQLschema="true" sqlMaxLimit="100">
	   <table name="dm_order" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2,dn3"
		rule="auto-sharding-rang-mod-order"/>
 <-- ####数据节点#### -->       
<dataNode name="dn1"   dataHost="a"  database="dm_order1"/>
<dataNode name="dn2"   dataHost="a"  database="dm_order2"/>
<dataNode name="dn3"   dataHost="a"  database="dm_order3"/>
<-- ####节点主机#### -->      
<dataHost  name="a"   maxCon="1000"  minCon="10"  balance="0"
	writeType="0"   dbType="mysql"
		dbDriver="native" switchType="1"  slaveThreshold="100">
	<heartbeat>select 1heartbeat>
	<writeHost host="hostM1"
		url="192.168.133.129:3306" user="root" password="123456">
	writeHost>
dataHost>
mycat:schema>

2.rule.xml 分片规则(求模分片算法PartitionByMod)

 
<mycat:rule xmlns:mycat="http://io.mycat/">
	 <tableRule name="auto-sharding-rang-mod-order">
	<rule>
		<columns>idcolumns>
		<algorithm>rang-mod-dmalgorithm>
	rule>
tableRule>

<function name="rang-mod-dm"  class="io.mycat.route.function.PartitionByMod">
	<property name="count">3property>
function>

mycat:rule>
3.server.xml 连接mycat的用户信息(用户名,密码)
<user name="root">
		<property name="password">123456property>
		<property name="schemas">dm_orderproperty>
user>
5.步骤:
一.准备三个xml文件

1.schema.xml:



<mycat:schema xmlns:mycat="http://io.mycat/">
	<schema name="dm_order"  checkSQLschema="true" sqlMaxLimit="100">
	   <table name="dm_order" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2,dn3"
		rule="auto-sharding-rang-mod-order"/>
	schema>
	<dataNode name="dn1"   dataHost="a"  database="dm_order1"/>
<dataNode name="dn2"   dataHost="a"  database="dm_order2"/>
<dataNode name="dn3"   dataHost="a"  database="dm_order3"/>
<dataHost  name="a"   maxCon="1000"  minCon="10"  balance="0"
	writeType="0"   dbType="mysql"
		dbDriver="native" switchType="1"  slaveThreshold="100">
	<heartbeat>select 1heartbeat>
	<writeHost host="hostM1"
		url="192.168.133.129:3306" user="root" password="123456">
	writeHost>
dataHost>
mycat:schema>
2.rule.xml:



<mycat:rule xmlns:mycat="http://io.mycat/">
	 <tableRule name="auto-sharding-rang-mod-order">
	<rule>
		<columns>idcolumns>
		<algorithm>rang-mod-dmalgorithm>
	rule>
tableRule>
<function name="rang-mod-dm"  class="io.mycat.route.function.PartitionByMod">
	<property name="count">3property>
function>
mycat:rule>
3.server.xml:



<mycat:server xmlns:mycat="http://io.mycat/">
	<system>
	<property name="useSqlStat">0property>  
	<property name="useGlobleTableCheck">0property>  
		<property name="sequnceHandlerType">2property>
       
         
	
	
		
		<property name="processorBufferPoolType">0property>
		
		
		
		
		
		
		
		
		<property name="handleDistributedTransactions">0property>	
			
		<property name="useOffHeapForMerge">1property>
		
		<property name="memoryPageSize">1mproperty>
		
		<property name="spillsFileBufferSize">1kproperty>
		<property name="useStreamOutput">0property>
		
		<property name="systemReserveMemorySize">384mproperty>
		
		<property name="useZKSwitch">trueproperty>
	system>
	
	
	<user name="root">
		<property name="password">123456property>
		<property name="schemas">dm_orderproperty>
		
		
		
	user>
mycat:server>
二、将三个配置文件用ftp的方式上传到linux的root目录下,(root设置为可读写,修改端口号IP)
三、打开shell,进入存放三个xml文件的目录 cd /root/xxx

(目的:将宿主机中的三个xml文件复制到mycat容器中的/usr/local/mycat/conf目录)

docker cp server.xml springcloudv20_mycat_1:/usr/local/mycat/conf 
docker cp rule.xml springcloudv20_mycat_1:/usr/local/mycat/conf 
docker cp schema.xml springcloudv20_mycat_1:/usr/local/mycat/conf 
四、测试

1.重新启动mycat
2.连接mycat的逻辑库,端口号8066
3.连接mysql的实体库,端口号3306

mysql初始配置:
(1)mysql -uroot -p
(2)直接回车
(3)use mysql
(4)update user set password=password(‘123456’) where user = ‘root’;
(5)grant all privileges on . to root@’%’ identified by ‘123456’ with grant option;

4.使用sqlyog测试分库

实体库,端口号3306

mysql初始配置:
(1)mysql -uroot -p
(2)直接回车
(3)use mysql
(4)update user set password=password(‘123456’) where user = ‘root’;
(5)grant all privileges on . to root@’%’ identified by ‘123456’ with grant option;

4.使用sqlyog测试分库

mycat_第3张图片

你可能感兴趣的:(mycat)