1. mycat tar包直接解压即可,不需安装。
2.配置
(1)mysql建库,且建立通过mycat访问的用户
create user mycat identified by 'Mycat_123';
grant all on *.* to mycat@'%';
(2)配置server.xml 配置访问用户名,密码,和端口,端口默认为8066
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<user name="mycat">
<property name="password">Mycat_123</property>
<property name="schemas">user</property>
</user>
(3)配置分库及路由,主配置表schema.xml (schema表示为database,user为用户)
<schema name="user" checkSQLschema="false" sqlMaxLimit="100">
<!-- auto sharding by id (long) -->
<table name="employee" primaryKey="id" dataNode="dn1,dn2" rule="sharding-by-pattern" />
</schema>
<dataNode name="dn1" dataHost="168.60.40.105" database="user" />
<dataNode name="dn2" dataHost="168.60.40.104" database="user" />
<dataHost name="168.60.40.105" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="168.60.40.105" url="168.60.40.105:3306" user="mycat" password="Mycat_123">
<readHost host="168.60.40.105" url="168.60.40.105:3306" user="mycat" password="Mycat_123" />
</writeHost>
</dataHost>
<dataHost name="168.60.40.104" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="168.60.40.104" url="168.60.40.104:3306" user="mycat" password="Mycat_123">
<readHost host="168.60.40.104" url="168.60.40.104:3306" user="mycat" password="Mycat_123" />
</writeHost>
</dataHost>
(4)配置分片库规则rule.xml,配置schema中应用的rule,以及rule应用的function名
<tableRule name="sharding-by-pattern">
<rule>
<columns>id</columns>
<algorithm>sharding-by-pattern</algorithm>
</rule>
</tableRule>
<function name="sharding-by-pattern"
class="io.mycat.route.function.PartitionByPattern">
<property name="patternValue">20</property>
<property name="defaultNode">0</property>
<property name="mapFile">partition-pattern.txt</property>
</function>
(5)创建partition-pattern.txt文件,并写规则
0-10=0
11-19=1
启动mycat即可。
本文中分片规则是,对20取余,0-10 在1库中,11-19在2库中。