mysql 通过mycat 读写分离

一、下载mycat

http://dl.mycat.org.cn/

二、配置server.xml ,主要是mycat的ip 端口及账号密码配置,一般保持默认

三、配置schema.xml ,配置主从节点及数据库表的配置

<mycat:schema xmlns:mycat="http://io.mycat/">

        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
                <!-- auto sharding by id (long) -->
                <table name="user1" dataNode="dn1" primaryKey="id" autoIncrement="true" />

        </schema>
        <dataNode name="dn1" dataHost="localhost1" database="test" />
        <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="hostM1" url="localhost:3306" user="root"
                                   password="123456">
                        <!-- can have multi read hosts -->
                        <readHost host="hostS2" url="192.168.1.5:3306" user="root" password="123456" />
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>

</mycat:schema>

参数解释

sqlMaxLimit配置默认查询数量
database为真实数据库名
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 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有, 1.3 没有。
writeType="0", 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
writeType="1",所有写操作都随机的发送到配置的 writeHost。
writeType="2",没实现。
-1 表示不自动切换
1 默认值,自动切换
2 基于MySQL 主从同步的状态决定是否切换

四、启动及连接mycat

./mycat start
mysql -uroot -p123456 -P8066 -h127.0.0.1
#stop
./mycat stop

五、测试读写连接目标,设置mycat 的log4j 日志级别为debug

2021-01-02 22:01:26.643 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.mysql.nio.MySQLConnection.synAndDoExecute(MySQLConnection.java:463)) - con need syn ,total syn cmd 1 commands SET names utf8;schema change:false con:MySQLConnection [id=3, lastTime=1609596086643, user=root, schema=test, old shema=test, borrowed=true, fromSlaveDB=false, threadId=27, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{insert into user1 (id) values (5)}, respHandler=SingleNodeHandler [node=dn1{insert into user1 (id) values (5)}, packetId=0], host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
2021-01-02 22:01:26.785 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.server.NonBlockingSession.releaseConnection(NonBlockingSession.java:386)) - release connection MySQLConnection [id=3, lastTime=1609596086620, user=root, schema=test, old shema=test, borrowed=true, fromSlaveDB=false, threadId=27, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{insert into user1 (id) values (5)}, respHandler=SingleNodeHandler [node=dn1{insert into user1 (id) values (5)}, packetId=1], host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
2021-01-02 22:01:26.785 DEBUG [$_NIOREACTOR-0-RW] (io.mycat.backend.datasource.PhysicalDatasource.releaseChannel(PhysicalDatasource.java:633)) - release channel MySQLConnection [id=3, lastTime=1609596086620, user=root, schema=test, old shema=test, borrowed=true, fromSlaveDB=false, threadId=27, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

总结:

1.需要注意主从数据库所在服务器的防火墙关闭,远程连接权限开放

2.读写分离必须建立在主从复制的基础上进行,参考https://www.cnblogs.com/lufei33180/p/14223155.html

原文地址:https://www.cnblogs.com/lufei33180/p/14224218.html