首先贴上自己测试用的schema文件,双引号之前的反斜杠不会消除,姑且当成不存在吧...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<? 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 > |
第一行参数<schema name="mycat" checkSQLschema="false" sqlMaxLimit="100"/>
在这一行参数里面,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设置
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<table
name="students" dataNode="dn1,dn2,dn3,dn4" rule="rule1" />
这一行代表在MyCAT前端会显示哪些表名,类似几行都代表一样的意思,这里强调的是表,而MyCAT并不会在配置文件里面定义表结构
如果在前端使用show create table ,MyCAT会显示正常的表结构信息,观察Debug日志,
可以看到,MyCAT把命令分发给了dn1代表的数据库,然后把dn1的查询结果返回给了前端
可以判断,类似的数据库级别的一些查询指令,有可能是单独分发给某个节点,然后再把某个节点的信息返回给前端;
dataNode的意义很简单,这个逻辑表的数据存储在后端的哪几个数据库里面
rule代表的是这个逻辑表students的具体切分策略,目前MyCAT只支持按照某一个特殊列,遵循一些特殊的规则来切分,如取模,枚举等,具体的留给之后细说
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<table
name="item_test" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3,dn4" />
这一行代表的是全局表,这意味着,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本身的逻辑结构里面,就包含了分库分表的这种特性(可以指定不同的表存在于不同的数据库中,而不必分到全部数据库)