潇湘夜雨移动版

主页 > 数据库 >

mycat中间件读写分离配置(2)

</schema>
<dataNode name="dn1" dataHost="localhost1" database="test" /> #将逻辑库节点映射到后端物理数据库test
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>show slave status</heartbeat> #后端数据库心跳检测
#配置读库和写库,writehost为主库,readhost为从库。
<writeHost host="master" url="172.18.109.235:3306" user="admin" password="admin">
<readHost host="slave1" url="172.18.109.236:3306" user="admin" password="admin" />
<readHost host="slave2" url="172.18.109.237:3306" user="admin" password="admin" />
</writeHost>
</dataHost>
</mycat:schema>
 
3.2 新增一个phpcmsv9数据库
 
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://org.opencloudb/">
<system>
<property name="defaultSqlParser">druidparser</property>
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!-- 
<property name="processors">1</property> 
<property name="processorExecutor">32</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="mutiNodeLimitType">1</property> 
    <property name="mutiNodePatchSize">100</property>
<property name="processors">32</property> <property name="processorExecutor">32</property> 
<property name="serverPort">3306</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>
</system>
<user name="admin">
<property name="password">admin</property>
<property name="schemas">testdb,phpcmsv9</property>
</user>
 
<user name="user">
<property name="password">user</property>
<property name="schemas">testdb</property>
<property name="readOnly">true</property>
</user>
<!-- 
<quarantine> 
  <whitehost>
     <host host="127.0.0.1" user="mycat"/>
     <host host="127.0.0.2" user="mycat"/>
  </whitehost>
       <blacklist check="false"></blacklist>
</quarantine>
-->
 
</mycat:server>
 
schema.xml配置文件:
</mycat:server>
[root@node3 conf]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
 
<schema name="testdb" checkSQLschema="ture" sqlMaxLimit="100" dataNode="dn1">  
</schema>  
<schema name="phpcmsv9" checkSQLschema="ture" sqlMaxLimit="100" dataNode="dn2">  
</schema>  
<dataNode name="dn1" dataHost="localhost1" database="test" />  
<dataNode name="dn2" dataHost="localhost1" database="phpcmsv9" />  
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">  
<heartbeat>show slave status</heartbeat>  
<writeHost host="master" url="172.18.109.235:3306" user="admin" password="admin">
<readHost host="slave1" url="172.18.109.236:3306" user="admin" password="admin" />
<readHost host="slave2" url="172.18.109.237:3306" user="admin" password="admin" />
</writeHost>
</dataHost>  
</mycat:schema>  
 
读写分离的参数:
balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。
balance="2",所有读操作都随机的在writeHost、readhost上分发。
balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力
writeType表示写模式
writeType="0",所有的操作发送到配置的第一个writehost
writeType="1",随机发送到配置的所有writehost
writeType="2",不执行写操作
switchType指的是切换的模式,目前的取值也有4种:
switchType=‘-1‘ 表示不自动切换
switchType=‘1‘ 默认值,表示自动切换
switchType=‘2‘ 基于MySQL主从同步的状态决定是否切换,心跳语句为show slave status
switchType=‘3‘基于MySQL galary cluster的切换机制(适合集群)(1.4.1),心跳语句为show status like ‘wsrep%‘。
 
 
 4. 启动mycat
 
[root@node3 local]# mycat status
Mycat-server is running (25020).
[root@node3 local]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:32000             0.0.0.0:*                   LISTEN      25022/java          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      1939/php-fpm        
tcp        0      0 0.0.0.0:45963               0.0.0.0:*                   LISTEN      1724/rpc.statd       (责任编辑:liangzh)