我们在之前的schma.cnf中定义了t1在是一张全局表,且位于名称为mysql的分片节点(datanode)上,因此当mycat拦截到客户端SQL语句之后,分析t1位于mysql分片节点上,直接发放该分片节点的readhost上
mysql > select * from t1 where id=1;
2018-05-25 10:16:16.906 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.backend.mysql.nio.MySQLConnection.synAndDoExecute(MySQLConnection.java:451)) - con need syn ,total syn cmd 1 commands SET names utf8;schema change:false con:MySQLConnection [id=29, lastTime=1527214576906, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=20, charset=utf8, txIsolation=3, autocommit=true, attachment=MySQL{SELECT *
FROM t1
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=MySQL{SELECT *
FROM t1
WHERE id = 1
LIMIT 100}, packetId=0], host=192.168.31.117, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
2018-05-25 10:16:16.907 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.server.NonBlockingSession.releaseConnection(NonBlockingSession.java:378)) - release connection MySQLConnection [id=29, lastTime=1527214576902, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=20, charset=utf8, txIsolation=3, autocommit=true, attachment=MySQL{SELECT *
FROM t1
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=MySQL{SELECT *
FROM t1
WHERE id = 1
LIMIT 100}, packetId=8], host=192.168.31.117, port=3306, statusSync=io.mycat.backend.mysql.nio.MySQLConnection$StatusSync@40563af9, writeQueue=0, modifiedSQLExecuted=false]
2018-05-25 10:16:16.907 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.backend.datasource.PhysicalDatasource.releaseChannel(PhysicalDatasource.java:595)) - release channel MySQLConnection [id=29, lastTime=1527214576902, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=20, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.31.117, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
t2同理,只不过发往名为mysql_galera的分片节点上
mysql > select * from t2 where id=3;
2018-05-25 10:44:00.504 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.backend.mysql.nio.MySQLConnection.synAndDoExecute(MySQLConnection.java:451)) - con need syn ,total syn cmd 1 commands SET names utf8;schema change:false con:MySQLConnection [id=44, lastTime=1527216240504, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=42, charset=utf8, txIsolation=3, autocommit=true, attachment=MySQL_galera{SELECT *
FROM t2
WHERE id = 3
LIMIT 100}, respHandler=SingleNodeHandler [node=MySQL_galera{SELECT *
FROM t2
WHERE id = 3
LIMIT 100}, packetId=0], host=192.168.31.123, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
2018-05-25 10:44:00.506 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.server.NonBlockingSession.releaseConnection(NonBlockingSession.java:378)) - release connection MySQLConnection [id=44, lastTime=1527216240502, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=42, charset=utf8, txIsolation=3, autocommit=true, attachment=MySQL_galera{SELECT *
FROM t2
WHERE id = 3
LIMIT 100}, respHandler=SingleNodeHandler [node=MySQL_galera{SELECT *
FROM t2
WHERE id = 3
LIMIT 100}, packetId=8], host=192.168.31.123, port=3306, statusSync=io.mycat.backend.mysql.nio.MySQLConnection$StatusSync@786ee29, writeQueue=0, modifiedSQLExecuted=false]
2018-05-25 10:44:00.506 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.backend.datasource.PhysicalDatasource.releaseChannel(PhysicalDatasource.java:595)) - release channel MySQLConnection [id=44, lastTime=1527216240502, user=mycat, schema=yuyang, old shema=yuyang, borrowed=true, fromSlaveDB=true, threadId=42, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=192.168.31.123, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
接下来我们分析一下t3的转发逻辑,在schma.cnf中我们定义t3分别位于mysql与mysql_galera两个分片节点上,且分片规则为id%1024后值为0-512则在mysql_galera上,513-1024则在mysql上,搞清楚了定义的分片位置与规则,下面来分析一下转发过程
首先,当mtcat拦截到SQL语句之后,发现t3存在分片规则,提取分片字段,匹配分片函数(也就是规则),得到了分片列表,把SQL语句发送到对应的分片节点上去执行以select id from t3 where id=512 这里的查找id=512,按照分片函数,512放在名为mysql的分片节点上,然后把SQL发往该节点执行。
mysql > select id from t3 where id=512;
2018-05-25 10:46:01.027 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:126)) - ServerConnection [id=10, schema=mycat01, host=192.168.31.124, user=root,txIsolation=3, autocommit=true, schema=mycat01]select * from t3 where id=512, route={
1 -> MySQL{select * from t3 where id=512}
} rrs
mysql > select id from t3 where id=1088;
2018-05-25 10:50:20.308 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:126)) - ServerConnection [id=10, schema=mycat01, host=192.168.31.124, user=root,txIsolation=3, autocommit=true, schema=mycat01]select * from t3 where id=1088, route={
1 -> MySQL_galera{select * from t3 where id=1088}
} rrs
Mycat读写分离以及拆库拆表综合实验3:通过日志分析mycat路由过程
原文地址:http://blog.51cto.com/2143754/2120168