首先贴上自己测试用的schema文件,双引号之前的反斜杠不会消除,姑且当成不存在吧...
<?xml version=\"1.0\"?> <!doctype mycat:schema system \"schema.dtd\"> <mycat:schema xmlns:mycat=\"http://org.opencloudb/\"> <schema name=\"mycat\" checksqlschema=\"false\" sqlmaxlimit=\"100\"> <!-- auto sharding by id (long) --> <table name=\"students\" datanode=\"dn1,dn2,dn3,dn4\" rule=\"rule1\" /> <table name=\"log_test\" datanode=\"dn1,dn2,dn3,dn4\" rule=\"rule2\" /> <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node --> <!--<table name=\"company\" primarykey=\"id\" type=\"global\" datanode=\"dn1,dn2,dn3\" /> <table name=\"goods\" primarykey=\"id\" type=\"global\" datanode=\"dn1,dn2\" /> --> <table name=\"item_test\" primarykey=\"id\" type=\"global\" datanode=\"dn1,dn2,dn3,dn4\" /> <!-- random sharding using mod sharind rule --> <!-- <table name=\"hotnews\" primarykey=\"id\" datanode=\"dn1,dn2,dn3\" rule=\"mod-long\" /> --> <!-- <table name=\"worker\" primarykey=\"id\" datanode=\"jdbc_dn1,jdbc_dn2,jdbc_dn3\" rule=\"mod-long\" /> --> <!-- <table name=\"employee\" primarykey=\"id\" datanode=\"dn1,dn2\" rule=\"sharding-by-intfile\" /> <table name=\"customer\" primarykey=\"id\" datanode=\"dn1,dn2\" rule=\"sharding-by-intfile\"> <childtable name=\"orders\" primarykey=\"id\" joinkey=\"customer_id\" parentkey=\"id\"> <childtable name=\"order_items\" joinkey=\"order_id\" parentkey=\"id\" /> <ildtable> <childtable name=\"customer_addr\" primarykey=\"id\" joinkey=\"customer_id\" parentkey=\"id\" /> --> </schema> <!-- <datanode name=\"dn\" datahost=\"localhost\" database=\"test\" /> --> <datanode name=\"dn1\" datahost=\"localhost\" database=\"test1\" /> <datanode name=\"dn2\" datahost=\"localhost\" database=\"test2\" /> <datanode name=\"dn3\" datahost=\"localhost\" database=\"test3\" /> <datanode name=\"dn4\" datahost=\"localhost\" database=\"test4\" /> <!-- <datanode name=\"jdbc_dn1\" datahost=\"jdbchost\" database=\"db1\" /> <datanode name=\"jdbc_dn2\" datahost=\"jdbchost\" database=\"db2\" /> <datanode name=\"jdbc_dn3\" datahost=\"jdbchost\" database=\"db3\" /> --> <datahost name=\"localhost\" maxcon=\"100\" mincon=\"10\" balance=\"1\" writetype=\"1\" dbtype=\"mysql\" dbdriver=\"native\"> <heartbeat>select user()<beat> <!-- can have multi write hosts --> <writehost host=\"localhost\" url=\"localhost:3306\" user=\"root\" password=\"wangwenan\"> <!-- can have multi read hosts --> <readhost host=\"hosts1\" url=\"localhost:3307\" user=\"root\" password=\"wangwenan\"/> </writehost> <writehost host=\"localhost1\" url=\"localhost:3308\" user=\"root\" password=\"wangwenan\"> <!-- can have multi read hosts --> <readhost host=\"hosts11\" url=\"localhost:3309\" user=\"root\" password=\"wangwenan\"/> </writehost> </datahost> <!-- <writehost host=\"hostm2\" url=\"localhost:3316\" user=\"root\" password=\"123456\"/> --> <!-- <datahost name=\"jdbchost\" maxcon=\"1000\" mincon=\"1\" balance=\"0\" writetype=\"0\" dbtype=\"mongodb\" dbdriver=\"jdbc\"> <heartbeat>select user()<beat> <writehost host=\"hostm\" url=\"mongodb://192.168.0.99/test\" user=\"admin\" password=\"123456\" ></writehost> </datahost> --> <!-- <datahost name=\"jdbchost\" maxcon=\"1000\" mincon=\"10\" balance=\"0\" dbtype=\"mysql\" dbdriver=\"jdbc\"> <heartbeat>select user()<beat> <writehost host=\"hostm1\" url=\"jdbc:mysql://localhost:3306\" user=\"root\" password=\"123456\"> </writehost> </datahost> --> </mycat:schema>
第一行参数262e537606e2a5215fd1ebf0c92793dd
在这一行参数里面,schema name定义了可以在mycat前端显示的逻辑数据库的名字,
checksqlschema这个参数为false的时候,表明mycat会自动忽略掉表名前的数据库名,比如说mydatabase1.test1,会被当做test1;
sqlmaxlimit指定了sql语句返回的行数限制;
如截图,这个limit会让mycat在分发sql语句的时候,自动加上一个limit,限制从分库获得的结果的行数,另外,截图右上角可以看到,mycat本身也是有缓存的;
那么,如果我们执行的语句要返回较多的数据行,在不修改这个limit的情况下,mycat会怎么做?
可以从截图看到,mycat完全就没搭理前端的实际需求,老老实实返回100条数据,所以如果实际应用里面需要返回大量数据,可能就得手动改逻辑了
mycat的1.4版本里面,用户的limit参数会覆盖掉默认的mycat设置
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
77b1ae820cb2419fc4581f8fe17392c0
这一行代表在mycat前端会显示哪些表名,类似几行都代表一样的意思,这里强调的是表,而mycat并不会在配置文件里面定义表结构
如果在前端使用show create table ,mycat会显示正常的表结构信息,观察debug日志,
可以看到,mycat把命令分发给了dn1代表的数据库,然后把dn1的查询结果返回给了前端
可以判断,类似的数据库级别的一些查询指令,有可能是单独分发给某个节点,然后再把某个节点的信息返回给前端;
datanode的意义很简单,这个逻辑表的数据存储在后端的哪几个数据库里面
rule代表的是这个逻辑表students的具体切分策略,目前mycat只支持按照某一个特殊列,遵循一些特殊的规则来切分,如取模,枚举等,具体的留给之后细说
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7d4277d2e49b845bfe131d19a8dcefd2
这一行代表的是全局表,这意味着,item_test这张表会在四个datanode里面都保存有完整的数据副本,那么查询的时候还会分发到所有的数据库么?
结果如截图,mycat依然是规规矩矩的返回了100条数据(╮(╯_╰)╭),而针对全局表的查询,只会分发到某一个节点上
配置的primarykey没发现作用在哪里,姑且忽略吧,以后发现了再补上
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
childtable我在测试中并没有实际用起来不过在mycat的设计文档里面有提到,childtable是一种依赖于父表的结构,
这意味着,childtable的joinkey会按照父表的parentkey的策略一起切分,当父表与子表进行连接,且连接条件是childtable.joinkey=parenttable.parentkey时,不会进行跨库的连接.
ps:具体测试以后再补
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
datanode的参数在之前的篇章介绍过,这里直接跳过~
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
datahost配置的是实际的后端数据库集群,大部分参数简单易懂,这里就不一个个介绍了,只介绍比较重要的两个参数,writetype和balance.
writetype和balance是用来控制后端集群的读写分离的关键参数,这里我用了双主双从的集群配置
这里的测试过程比较麻烦,所以直接贴结论:
1.balance=0时,读操作都在localhost上(localhost失败时,后端直接失败)
2.balance=1时,读操作会随机分散在localhost1和两个readhost上面(localhost失败时,写操作会在localhost1,如果localhost1再失败,则无法进行写操作)
3.balance=2时,写操作会在localhost上,读操作会随机分散在localhost1,localhost1和两个readhost上面(同上)
4.writetype=0时,写操作会在localhost上,如果localhost失败,会自动切换到localhost1,localhost恢复以后并不会切换回localhost进行写操作
5.writetype=1时,写操作会随机分布在localhost和localhost1上,单点失败并不会影响集群的写操作,但是后端的从库会无法从挂掉的主库获取更新,会在读数据的时候出现数据不一致
举例:localhost失败了,写操作会在localhost1上面进行,localhost1的主从正常运行,但是localhost的从库无法从localhost获取更新,localhost的从库于其他库出现数据不一致
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实际上,mycat本身的读写分离是基于后端集群的同步来实现的,而mycat本身则提供语句的分发功能,当然,那个sqllimit的限制也使得mycat会对前端应用层的逻辑造成一些影响
由schema到table的配置,则显示出mycat本身的逻辑结构里面,就包含了分库分表的这种特性(可以指定不同的表存在于不同的数据库中,而不必分到全部数据库)
以上就是mysql分布式集群之mycat(二)schema代码详解的详细内容。