建议学习mycat之前先下载mycat权威指南一书,先整体看一遍。
先去mycat官网:http://dl.mycat.io/下载对应版本的mycat-server,我这里使用的是Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
上传到linux服务器。
解压:tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz 会生成一个mycat文件夹。
这里简单演示一下mycat使用id 取模 的单库 水平分表实例,我们线上大部分是这种分表策列。
修改conf目录下的server.xml, 内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="defaultSqlParser">druidparser</property>
</system>
<user name="root">
<property name="password">root</property>
<property name="schemas">tanlei</property>
</user>
</mycat:server>
其中name="root" 和password="root"对应mysql实例用户名和密码,schemas对应schema.xml下<schema>的name标签。
修改schema.xml,内容如下:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="tanlei" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_user" subTables="t_user$1-3" dataNode="dn1" rule="mod-long" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="tanleidb" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql"
dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="localhost:3306" user="root" password="root" />
</dataHost>
</mycat:schema>
修改rule.xml,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">3</property>
</function>
</mycat:rule>
进入bin目录下执行: ./mycat start 启动mycat server。
观察logs 目录下 mycat.log和wrapper.log, 没有报错则mycat server启动成功。
我这里在实例下建了一个tanleidb库,分别创建了三张表:t_user1,t_user2,t_user3,主键为id。
(用navicat链接root,root,hostip,8066端口,然后插入数据:)
执行命令行: mysql -uroot -proot -h127.0.0.1 -P8066 -Dtanlei; 进入mycat客户端。
insert into t_user(id,userName,age,createTime,updateTime) values(5,'tanlei',23,now(),now())
explain
select * from t_user where id = 9
发现id=9(mod=0,进入表1)的数据落入到了t_user1表中。