mycat实现mysql的读写分离
Posted 从零开始的linux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mycat实现mysql的读写分离相关的知识,希望对你有一定的参考价值。
环境
ip | 角色 | 端口 |
---|---|---|
128.0.0.101 | master | 3306 |
128.0.0.102 | slave | 3306 |
128.0.0.103 | mycat | 8066 |
前提两台机器已经做好主从
配置schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="alex_schema1" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
<schema name="alex_schema2" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn2"></schema>
<dataNode name="dn1" dataHost="localhost1" database="alex" />
<dataNode name="dn2" dataHost="localhost1" database="alex11" />
<dataHost name="localhost1" maxCon="500" minCon="20" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<heartbeat>show slave status</heartbeat>
<writeHost host="hostM1" url="128.0.0.101:3306" user="root"
password="123456" />
<writeHost host="hostS1" url="128.0.0.102:3306" user="root"
password="123456" />
</dataHost>
</mycat:schema>
配置server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
<system>
<property name="defaultSqlParser">druidparser</property>
<property name="charset">utf8mb4</property>
</system>
<user name="user2">
<property name="password">password</property>
<property name="schemas">alex_schema1,alex_schema2</property>
</user>
<!-- 用户3,只读权限-->
<user name="user3">
<property name="password">password</property>
<property name="schemas">alex_schema1,alex_schema2</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
启动
mycat start
mysql -uuser2 -password -h128.0.0.103 -P8066
读取数据库的log
02/11 13:50:15.573 DEBUG [$_NIOREACTOR-0-RW] (PhysicalDBPool.java:431) -select read source hostS1 for dataHost:localhost1
#看到是conghosts1上面读写的说明是slave节点
写入数据库的log
02/11 13:57:37.387 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:230) -release connection MySQLConnection [id=1, lastTime=1518328657366, schema=alex11, old shema=alex11, borrowed=true, fromSlaveDB=false, threadId=63, charset=utf8, txIsolation=3, autocommit=true, attachment=dn2{INSERT INTO `alex11`.`ee` (`ss`, `aa`) VALUES ('哈哈哈ha', '111111')}, respHandler=SingleNodeHandler [node=dn2{INSERT INTO `alex11`.`ee` (`ss`, `aa`) VALUES ('哈哈哈ha', '111111')}, packetId=0], host=128.0.0.101, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
#看到是从host=128.0.0.101上面写入的说明是master节点
完成
以上是关于mycat实现mysql的读写分离的主要内容,如果未能解决你的问题,请参考以下文章