mycat 黑匣子

1,执行 select * from travelrecord ,分析Debug日志,说明整个执行逻辑,包括连接获取,连接同步信息,数据合并,数据返回,连接释放

1.1清空debug信息

[[email protected]_idc_squid_1_11 logs]# cat/dev/null > mycat.log

[[email protected]_idc_squid_1_11 logs]# ll

修改日志为debug:

vim log4j.xml

<root>

<level value="debug" />

<appender-ref ref="FILE" />

<!--<appender-ref ref="FILE" />-->

</root>

获取日志:

[[email protected]_idc_squid_1_11 logs]# cpmycat.log 1.log

[[email protected]_idc_squid_1_11 logs]# vim 1.log

在命令行窗口执行select * from travelrecord;

1.1连接获取

01/2421:59:12.850  DEBUG [$_NIOREACTOR-3-RW](EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDBselect * from travelrecord

01/2421:59:12.851  DEBUG [$_NIOREACTOR-3-RW](EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDBselect * fromtravelrecord value:select * from travelrecord, route={

1 -> dn1{SELECT *

FROMtravelrecord

LIMIT100}

2 -> dn2{SELECT *

FROMtravelrecord

LIMIT100}

3 -> dn3{SELECT *

FROMtravelrecord

LIMIT100}

}

01/2421:59:12.852  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:113)-ServerConnection [id=4, schema=TESTDB, host=127.0.0.1,user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * fromtravelrecord, route={

1 -> dn1{SELECT *

FROMtravelrecord

LIMIT100}

2 -> dn2{SELECT *

FROM travelrecord

LIMIT100}

3 -> dn3{SELECT *

FROMtravelrecord

LIMIT100}

} rrs

1.2连接同步信息:

01/2421:59:12.852  DEBUG [$_NIOREACTOR-3-RW](MySQLConnection.java:445) -con need syn ,total syn cmd 2 commands SET nameslatin1;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schemachange:false con:MySQLConnection [id=11, lastTime=1453643952852, user=root,schema=db1, old shema=db1, borrowed=true, fromSlaveDB=true, threadId=3425,charset=latin1, txIsolation=0, autocommit=true, attachment=dn1{SELECT *

FROMtravelrecord

LIMIT 100},res[email protected]30a6aae0,host=127.0.0.1, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

1.3数据合并

01/24 21:59:12.856  DEBUG [BusinessExecutor6](DataMergeService.java:296) -prepare mpp merge result for select * fromtravelrecord

01/24 21:59:12.856  DEBUG [BusinessExecutor6](MultiNodeQueryHandler.java:324) -last packet id:11

01/24 21:59:21.910  DEBUG [Timer1] (SQLJob.java:85) -con querysql:show slave status to con:MySQLConnection [id=14, lastTime=1453643961910,user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false,threadId=22, charset=latin1, txIsolation=3, autocommit=true, attachment=null,respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

01/24 21:59:21.910  DEBUG [Timer1] (SQLJob.java:85) -con querysql:show slave status to con:MySQLConnection [id=27, lastTime=1453643961910, user=root,schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3442,charset=latin1, txIsolation=3, autocommit=true, attachment=null,respHandler=null, host=127.0.0.1, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

……………..

1.4数据返回

01/24 21:59:12.854  DEBUG [$_NIOREACTOR-3-RW](MultiNodeQueryHandler.java:171) -received ok response ,executeResponse:falsefrom MySQLConnection [id=11, lastTime=1453643952835, user=root, schema=db1, oldshema=db1, borrowed=true, fromSlaveDB=true, threadId=3425, charset=latin1,txIsolation=0, autocommit=true, attachment=dn1{SELECT *

FROM travelrecord

LIMIT 100},res[email protected]30a6aae0,host=127.0.0.1, port=3327,[email protected]d43df48,writeQueue=0, modifiedSQLExecuted=false]

01/24 21:59:12.855  DEBUG [$_NIOREACTOR-3-RW](MultiNodeQueryHandler.java:171) -received ok response ,executeResponse:falsefrom MySQLConnection [id=11, lastTime=1453643952835, user=root, schema=db1, oldshema=db1, borrowed=true, fromSlaveDB=true, threadId=3425, charset=latin1, txIsolation=3,autocommit=true, attachment=dn1{SELECT *

FROM travelrecord

LIMIT 100},res[email protected]30a6aae0,host=127.0.0.1, port=3327,[email protected]d43df48,writeQueue=0, modifiedSQLExecuted=false]

文章源地址:http://blog.csdn.net/mchdba/article/details/51160495,谢绝转载。

1.5连接释放:

01/24 21:59:21.911  DEBUG [$_NIOREACTOR-2-RW](PhysicalDatasource.java:403) -release channel MySQLConnection [id=14,lastTime=1453643961901, user=root, schema=db1, old shema=db1, borrowed=true,fromSlaveDB=false, threadId=22, charset=latin1, txIsolation=3, autocommit=true,attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null,writeQueue=0, modifiedSQLExecuted=false]

01/24 21:59:21.911  DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403)-release channel MySQLConnection [id=27, lastTime=1453643961901, user=root,schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3442,charset=latin1, txIsolation=3, autocommit=true, attachment=null,respHandler=null, host=127.0.0.1, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

01/24 21:59:21.911  DEBUG [$_NIOREACTOR-2-RW](PhysicalDatasource.java:403) -release channel MySQLConnection [id=16,lastTime=1453643961901, user=root, schema=db1, old shema=db1, borrowed=true,fromSlaveDB=true, threadId=3431, charset=utf8, txIsolation=0, autocommit=true,attachment=null, respHandler=null, host=127.0.0.1, port=3327, statusSync=null,writeQueue=0, modifiedSQLExecuted=false]

2,新增一个分片表 T_VOTE (ID,PROVINCE),PROVINCE用hash分片,并用reload命令方式重载生效,截图和文字说明整个过程。

2.1配置hash分片

Schema.xml文件,dataNode有dn21、dn22; dataHost有m1、m2,如下2.1.jpg所示:

Rule.xml :

reload更新配置文件:

mysql> reload @@config;

ERROR 1064 (HY000):com.alibaba.druid.sql.parser.ParserException: syntax error, error in :‘[email protected]@config‘,expect IDENTIFIER, actual IDENTIFIER reload

mysql>

问题好奇怪,虽然有报错信息,但是后台生效了,建表explain可以看到新的dn21和dn22 

建表:

2.2 录入初始化数据验证数据分片:

总共录入了4条记录,去查看数据,2条记录在dn21分片上也就是mysql的3317实例上面,2条记录在dn22分片也就是mysql的3327实例上,如下所示:

Dn21分片3317mysql端口:

Dn22分片3327mysql端口:

3,该字符集为latin后的显示

3.1 Mysql服务器字符集为latin:

后台debug信息显示为charset=latin1了:

01/25 02:47:19.128  DEBUG [$_NIOREACTOR-1-RW](MySQLConnection.java:445) -con need syn ,total syn cmd 1 commands SET SESSIONTRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:falsecon:MySQLConnection [id=32, lastTime=1453661239128, user=root, schema=db3, oldshema=db3, borrowed=true, fromSlaveDB=false, threadId=23, charset=latin1,txIsolation=0, autocommit=true, attachment=dn22{SELECT *

FROM T_VOTE

LIMIT 100},res[email protected]5485687f,host=10.254.1.11, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

3.2 登录mycat的客户端设置成gbk:

1/25 02:50:26.348  DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76)-SQLRouteCache  miss cache,key:TESTDBselect * from T_VOTE where PROVINCE="beijing"

01/25 02:50:26.392  DEBUG [$_NIOREACTOR-1-RW](EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDBselect * from T_VOTEwhere PROVINCE="beijing" value:select * from T_VOTE wherePROVINCE="beijing", route={

1-> dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘beijing‘

LIMIT 100}

}

01/25 02:50:26.393  DEBUG [$_NIOREACTOR-1-RW](NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB,host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select* from T_VOTE where PROVINCE="beijing", route={

1-> dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘beijing‘

LIMIT 100}

} rrs

01/25 02:50:26.393  DEBUG [$_NIOREACTOR-1-RW](PhysicalDBPool.java:452) -select read source hostM2 for dataHost:m2

01/25 02:50:26.396  DEBUG [$_NIOREACTOR-3-RW](NonBlockingSession.java:229) -release connection MySQLConnection [id=11,lastTime=1453661426394, user=root, schema=db3, old shema=db3, borrowed=true,fromSlaveDB=false, threadId=16, charset=gbk, txIsolation=3, autocommit=true,attachment=dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘beijing‘

LIMIT 100}, respHandler=SingleNodeHandler[node=dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘beijing‘

LIMIT 100}, packetId=5], host=10.254.1.11,port=3327,[email protected]4a7f2c7,writeQueue=0, modifiedSQLExecuted=false]

01/25 02:50:26.396  DEBUG [$_NIOREACTOR-3-RW](PhysicalDatasource.java:403) -release channel MySQLConnection [id=11,lastTime=1453661426394, user=root, schema=db3, old shema=db3, borrowed=true,fromSlaveDB=false, threadId=16, charset=gbk, txIsolation=3, autocommit=true, attachment=null,respHandler=null, host=10.254.1.11, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

01/25 02:50:32.191  DEBUG [Timer0] (SQLJob.java:85) -con querysql:select user() to con:MySQLConnection [id=2, lastTime=1453661432191,user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false,threadId=23, charset=latin1, txIsolation=0, autocommit=true, attachment=null,respHandler=null, host=10.254.1.11, port=3317, statusSync=null, writeQueue=0,modifiedSQLExecuted=false].

看到了charset=gbk,返回的数据和mysql数据库设置的字符集关系不大,和登录mycat客户端设置的字符集有关。

3.3设置成为utf8后

01/25 02:54:20.399  DEBUG [$_NIOREACTOR-1-RW](EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDBselect * from T_VOTE wherePROVINCE="shanghai"

01/25 02:54:20.400  DEBUG [$_NIOREACTOR-1-RW](EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDBselect * from T_VOTEwhere PROVINCE="shanghai" value:select * from T_VOTE wherePROVINCE="shanghai", route={

1-> dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘shanghai‘

LIMIT 100}

}

01/25 02:54:20.401  DEBUG [$_NIOREACTOR-1-RW](NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB,host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select* from T_VOTE where PROVINCE="shanghai", route={

1-> dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘shanghai‘

LIMIT 100}

} rrs

01/25 02:54:20.401  DEBUG [$_NIOREACTOR-1-RW](PhysicalDBPool.java:452) -select read source hostM2 for dataHost:m2

01/25 02:54:20.403  DEBUG [$_NIOREACTOR-3-RW](NonBlockingSession.java:229) -release connection MySQLConnection [id=11,lastTime=1453661660386, user=root, schema=db3, old shema=db3, borrowed=true,fromSlaveDB=false, threadId=16, charset=utf8, txIsolation=3, autocommit=true,attachment=dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘shanghai‘

LIMIT 100}, respHandler=SingleNodeHandler[node=dn22{SELECT *

FROM T_VOTE

WHERE PROVINCE = ‘shanghai‘

LIMIT 100}, packetId=5], host=10.254.1.11,port=3327,[email protected]017d906,writeQueue=0, modifiedSQLExecuted=false]

01/25 02:54:20.403  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403)-release channel MySQLConnection [id=11, lastTime=1453661660386, user=root,schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=16, charset=utf8,txIsolation=3, autocommit=true, attachment=null, respHandler=null,host=10.254.1.11, port=3327, statusSync=null, writeQueue=0,modifiedSQLExecuted=false]

看到debug信息中,返回的信息是utf8,所以客户端登录的字符集是啥,返回的数据就是啥字符集的。

3.3 改成latin?

Mysql数据库都是latin,登录mycat客户端也是latin,不用测试,肯定是latin

总结:mycat登录的所有字符集,数据处理返回结果都跟着登录设置的字符集走。

时间: 2024-09-30 19:34:56

mycat 黑匣子的相关文章

mycat 不得不说的缘分

                                  1,愕然回首,它在灯火阑珊处 关于mysql集群中间件,以前写在应用程序里面,由开发人员实现,在配置文件里面写多个数据源,写库一个数据源,读库一个数据源,笨拙不高效,由于程序员的差异化,效果并不是特别理想. 后来,组织了开发人员写了一个自动识别读写的功能模块接口,让开发人员调用,这样能满足特定场景的业务需求,但是适应性比较窄. 后来出了cobar,但是在高并发这里出壳了,后来在一次无意中的演讲中,见到leader在介绍了myca

mycat

schema.xml管理逻辑库,分片表,分片借点和分片主机等信息 server.xml是系统参数的配置文件. sequence是全局序列的配置文件 log4j.xml是mycat的日志输出配置文件 mycat从1.5版本开始支持两种配置方式:zookeeper和本地xml方式.默认以本地加载xml方式启动.如果需要配置成zookeeper方式启动,把zk.conf文件中的loadfromzk参数设置成true. server.xml配置文件 1.user标签 p.p1 { margin: 0.0

MyCAT+MySQL搭建

Mycat安装部署 1下载jdk: http://www.oracle.com/technetwork/java/javase/archive-139210.html,建议用7就好 jdk-7u55-linux-i586.tar.gz 解压包:tar –zxvf jdk-7u55-linux-i586.tar.gz 存放在/usr/local:mv jdk1.7.0_55 /usr/local/jdk 添加环境变量: Vim /etc/profile exportJAVA_HOME=/usr/l

mycat实现简单的mysql集群负载均衡

什么是mycat呢? 简单理解为一个MySQL中间件,它支持分流.基于心跳的自动故障切换,支持读写分离,支持mysql主从,基于Nio管理线程的高并发- 详见官网:http://www.mycat.io/ 为什么需要mysql集群? 一个庞大的分布式系统的性能瓶颈中,最脆弱的就是连接,一个是客户端与后端的连接,另一个是后端与数据库的连接,说白了就是发送端请求太多,接收端能够的接收和处理的请求并不多,在客户端与后端中可以利用类似nginx的负载均衡解决,而在后端与数据库中可以利用类似mycat的负

Mycat读写分离和分库分表配置

Mycat是一个开源的分布式数据库系统,不同于oracle和mysql,Mycat并没有存储引擎,但是Mycat实现了mysql协议,前段用户可以把它当做一个Proxy.其核心功能是分表分库,即将一个大表水平分割为N个小表,存储在后端mysql存储引擎里面.最新版本的Mycat不仅支持mysql,还可以支持MS SqlServer,Oracle,DB2等关系型数据库,而且还支持MongoDB这种NoSQL.Mycat对调用者屏蔽了后端存储具体实现. Mycat的原理是先拦截用户的SQL语句并做分

数据库中间件mycat简单入门

当在项目中mysql数据库成为瓶颈的时候,我们一般会使用主从复制,分库分表的方式来提高数据库的响应速度,比如mysql主从复制,在没有数据库中间件的情况下,我们只能由开发工程师在程序中控制,这对于一个在正在线上运行的项目来说,需要改动的代码量蛮大的,也不方便扩展,比如,我想再添加一台从机,下面进行mycat入门 一.部署步骤详解 (1) 用命令行工具或图形化客户端,连接MYSQL,创建DEMO所用三个分片数据库: CREATE database db1; CREATE database db2;

[项目构建 十四]babasport Mycat配置及使用详解.

首先我们来看下什么是Mycat:MyCat:开源分布式数据库中间件, 这里定义的很简单, 就是分布式数据库的中间件. 其实Mycat 是可以时mysql进行集群的中间件, 我们可以对mysql来分库分表 来应对日益增长的数据量. 每台机器只存少量数据, 数据总和是分布式的机器上数据量总和. 例如我们一个表中有512条数据(当然实际情况可能有成千上万条数据), 那么现在我们有三台机器装有mysql数据库, 我们想将这些数据按照一定规则的存储在三台机器上, 那么我们设定规则: 表的id%/512 取

关于MyCAT字符集的验证

MyCAT默认字符集是UTF8 下面通过查看日志来验证不同的MySQL客户端字符集和服务器字符集对于MyCAT的影响. 日志中与字符集有关的主要有三部分: 1. 初始化MyCAT连接池 2. 心跳检测 3. 在执行SQL语句时的连接同步. 因为MyCAT实现的是三节点的读写分离和自动切换,以下修改的均是localhost节点上MySQL实例的字符集,其它两个点(192.168.244.146和192.168.244.144)均没有修改.默认为 mysql> show variables like

MyCAT安装指南

MyCAT安装指南 MyCAT 1.2版本 快速上手-安装指南(安装单机) Mycat的server和mysql位于同一台服务器,centos6.2.4环境 Mycat:10.191.116.175 Mysql:10.191.116.175 是用Java开发,需要有JAVA运行环境,mycat依赖jdk1.7的环境,若本机没有,则需要下载安装: http://www.java.com/zh_CN/ 获取MyCAT的最新开源版本,项目主页http://code.google.com/p/MyCAT