mycat读写分离(mysql数据库)

mycat读写分离(mysql数据库)

grant select on *.* to 'read'@'%' IDENTIFIED BY 'dear'; 

安装依赖包yum install java-1.8.0-openjdk

vim /usr/local/mycat/conf/schema.xml

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

<schema name="mydb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="localhost1" database="mydb" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="m01" url="192.168.1.100:3306" user="root" password="toor">
<!-- can have multi read hosts -->
<readHost host="node2" url="192.168.1.12:3306" user="read_user" password="user_read" />
<readHost host="node3" url="192.168.1.13:3306" user="read_user" password="user_read" />

</writeHost>
</dataHost>

</mycat:schema>

vim  /usr/local/mycat/conf/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="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>
-->

<user name="root">
<property name="password">toor</property>
<property name="schemas">mydb</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="read_user">
<property name="password">user_read</property>
<property name="schemas">mydb</property>
<property name="readOnly">true</property>
</user>

</mycat:server>

-------------------------------------------------------------------------------------------------------------------------------------------------------

配置文件注意事项:
conf/server.xml 可以不修改,但要注意
<property name="schemas">mydb</property>
虚拟库名称,用户名和密码,要和后面对应
schemas是这个用户下的逻辑数据库可以有多个逻辑数据库可以用“,”逗号隔开
用户名和密码是连接 mycat 的用户名和密码,与 mysql 实例的用户名密码无关
mycat默认的普通连接端口是8066,管理连接端口是9066
mysql -uread -pdear -h127.0.0.1 -P8066 -e 'select @@hostname;


schema:逻辑数据库
dataNode:节点
dataHost:节点对应的读库写库的地址和连接
balance指的负载均衡类型,目前的取值有4种:
balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance="1",全部的readHost与stand by writeHost参与select语句的负载均衡
balance="2",所有读操作都随机的在writeHost、readhost上分发。
balance="3",所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力

switchType指的是切换的模式,目前的取值也有4种:
switchType='-1' 表示不自动切换
switchType='1' 默认值,表示自动切换
switchType='2' 基于MySQL主从同步的状态决定是否切换,心跳语句为 show slave status
switchType='3' 基于MySQL galary cluster的切换机制(适合集群)(1.4.1),心跳语句为 show status like 'wsrep%'

WriteType参数设置:
writeType=“0”, 所有写操作都发送到可用的writeHost上。
writeType=“1”,所有写操作都随机的发送到readHost。
writeType=“2”,所有写操作都随机的在writeHost、readhost分上发。

配置完成以后连接 mycat 查询
mysql -uroot -p123456 -h192.168.4.20 -P 8066 -e 'select @@hostname;'
多查询几次,可以看到轮询效果

读写分离了,mycat 单点故障, mysql master 单点故障怎么解决 ?
官方推荐高可用方案

猜你喜欢

转载自www.cnblogs.com/hmj589119/p/10922760.html