MyCat读写分离
一个主库负责写 一个从库负责读取
方案1:配置好 mysql 主从后,在应用层面,对写的操作应用到主库,对读的操作应用到从库,这种在代码层面进行修改
方案2:使用数据库中间件,如 mycat:应用程序只需连接到中间件,所有读写请求的路由由中间件负责
接下来进行方案2的演示 环境:mycat 1.6,jdk 1.8(mycat 是 java 语言编写的,需要 jdk 环境)
解压 mycat 的安装包
进入 conf 目录,配置 mycat 本机信息 server.xml ,和 mysql 数据库映射信息 schema.xml
schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="zzl" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
<schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"></schema>
<dataNode name="dn1" dataHost="localhost1" database="zzl" />
<dataNode name="dn2" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="500" minCon="20" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="master" url="10.0.0.24:3306" user="root" password="123456">
<readHost host="slave" url="10.0.0.25:3306" user="root" password="123456" />
</writeHost>
</dataHost>
</mycat:schema>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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">5012</property>
<property name="processors">16</property>
<property name="processorExecutor">16</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">123456</property>
<property name="schemas">zzl,test</property>
</user>
</mycat:server>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
在 server.xml 中配置访问 mycat 的用户,配置 逻辑数据库
在 schema.xml 中配置 逻辑主机,和 逻辑数据库 之间的相应关系
接下来启动 mycat:./bin/mycat start
然后测试数据库读写分离,使用 navicat 连接 mycat,做插入和读取的操作
观察 mycat 日志:tail -n 100 ./logs/wrapper.log | grep host
INFO | jvm 1 | 2024/08/17 08:52:15 | 2024-08-17 08:52:15,221 [INFO ][$_NIOREACTOR-0-RW] connectionAcquired MySQLConnection [id=62, lastTime=1723855935220, user=root, schema=zzl, old shema=zzl, borrowed=false, fromSlaveDB=true, threadId=228, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=10.0.0.25, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.NewConnectionRespHandler:NewConnectionRespHandler.java:45)
INFO | jvm 1 | 2024/08/17 08:52:15 | 2024-08-17 08:52:15,221 [INFO ][$_NIOREACTOR-1-RW] connectionAcquired MySQLConnection [id=63, lastTime=1723855935220, user=root, schema=test, old shema=test, borrowed=false, fromSlaveDB=true, threadId=229, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=10.0.0.25, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.NewConnectionRespHandler:NewConnectionRespHandler.java:45)
INFO | jvm 1 | 2024/08/17 08:52:15 | 2024-08-17 08:52:15,224 [INFO ][$_NIOREACTOR-15-RW] connectionAcquired MySQLConnection [id=61, lastTime=1723855935220, user=root, schema=test, old shema=test, borrowed=false, fromSlaveDB=false, threadId=281, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=10.0.0.24, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false] (io.mycat.backend.mysql.nio.handler.NewConnectionRespHandler:NewConnectionRespHandler.java:45)
1
2
3
2
3
10.0.0.25(从库):连接被标记为 fromSlaveDB=true,这意味着 Mycat 正在使用从库来处理读操作
10.0.0.24(主库):连接被标记为 fromSlaveDB=false,表示这是主库的连接,主要用于写操作