上次介绍Mycat概念时, 说到了分片表, 下面就从配置一个分片表入手简述Mycat的配置.
schema.xml是首先遇到的配置文件, 其管理着Mycat的逻辑库, 表, 分片规则, 分片节点和节点主机, 如此内容分别呈现在相应的标签中, 配置过程即对这些标签的设置.
- 逻辑库的设置, 即schema标签, 指定库名称testdb.
<schema name="testdb" checkSQLschema="true" sqlMaxLimit="100">
...
</schema>
- 表的设置, 即table标签, 指定表名称tb1, 所在分片节点, 和分片规则. table标签要包含在schema标签中.
<table name="tb1" dataNode="dnTest1,dnTest2" rule="mod-long"/>
- 分片节点的设置, 即dataNode标签, 指定分片节点名称, 所在节点主机, 如分片节点dnTest1位于节点主机Rep1_3306上, 和与之对应的物理库的名称, 如分片节点dnTest1对应物理库test1(其真实的存在于后端数据库实例上).
<dataNode name="dnTest1" dataHost="Rep1_3306" database="test1"/>
<dataNode name="dnTest2" dataHost="Rep2_3306" database="test2"/>
- 节点主机的设置, 即dataHost标签,指定节点主机名称, 与之对应的后端数据库实例信息, 和读写分离配置, 心跳语句等, 之后在Mycat高可用部分会进一步说明该处.
<dataHost name="Rep1_3306" maxCon="20" minCon="5" balance="2" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.4.235:3306" user="zzzz" password="zzzz">
<readHost host="hostS1" url="192.168.4.234:3306" user="zzzz" password="zzzz"/>
</writeHost>
</dataHost>
<dataHost name="Rep2_3306" maxCon="20" minCon="5" balance="2" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM2" url="192.168.4.151:3306" user="zzzz" password="zzzz">
<readHost host="hostS2" url="192.168.4.150:3306" user="zzzz" password="zzzz"/>
</writeHost>
</dataHost>
在对dataHost标签设置时,后端MySQL实例要创建好zzzz用户.
mysql> create user 'zzzz'@'192.168.4.%' identified by 'zzzz';
mysql> grant select, insert, update, delete, execute, alter, create on test1.* to 'zzzz'@'192.168.4.%';
由于tb1是分片表, 还需说明分片字段和分片算法, 配置rule.xml文件.
- 分片字段, 为tb1表的主键id字段.
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
- 分片算法, 简单取摸.
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<property name="count">2</property>
</function>
至此tb1表配置完了, 要通过Mycat访问该表,还要一个简单权限, 配置server.xml文件, 该文件还包括了Mycat的系统配置, 和调优参数.
<user name="test_user">
<property name="password">test_user</property>
<property name="schemas">testdb</property>
</user>
现在就可以登陆Mycat, 创建tb1的表结构了, 该步骤也可直接到后端实例上做, 要有多个节点的话, 会比较麻烦, 所以才给zzzz用户赋予了create权限, 直接通过Mycat创建.
- 登陆mycat
$ mysql -h292.168.4.184 -P8066 -utest_user -ptest_user
mysql> create table tb1(id int auto_increment primary key, user_name varchar(30) not null default '');
此时就可以使用tb1表了, 插入条数据, 通过日志看下路由过程, 为了看到比较详细的日志, 先将日志级别调整为debug, 配置log4j.xml文件.
<root>
<level value="debug" />
<appender-ref ref="FILE" />
</root>
- 插入数据
mysql> insert into tb1 select null, 'abc';
ERROR 1064 (HY000): partition table, insert must provide ColumnList
mysql> insert into tb1(id, user_name) values(null, 'abc');
ERROR 1064 (HY000): For input string: "NULL"
mysql> insert into tb1(id, user_name) values(7, 'abc');
Query OK, 1 row affected (0.00 sec)
- 观察日志, 可见route到dnTest2分片节点上insert.
03/18 08:33:04.221 DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=2, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=true, schema=testdb]insert into tb1(id, user_name) values(7, 'abc')
03/18 08:33:04.222 DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:113) -ServerConnection [id=2, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=true, schema=testdb]insert into tb1(id, user_name) values(7, 'abc'), route={
1 -> dnTest2{insert into tb1(id, user_name) values(7, 'abc')}
} rrs
03/18 08:33:04.222 DEBUG [$_NIOREACTOR-2-RW] (MySQLConnection.java:459) -con need syn ,total syn cmd 2 commands SET names utf8;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=5, lastTime=1521333184222, user=zzzz, schema=test2, old shema=test2, borrowed=true, fromSlaveDB=false, threadId=68, charset=utf8, txIsolation=0, autocommit=true, attachment=dnTest2{insert into tb1(id, user_name) values(7, 'abc')}, respHandler=SingleNodeHandler [node=dnTest2{insert into tb1(id, user_name) values(7, 'abc')}, packetId=0], host=192.168.4.151, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]