下载安装
请参照官网的文档,官网文档非常详细。建议测试阶段使用源码运行,有问题可以debug。
基本配置
schema.xml
配置了三个节点dn0~1
z_user使用test-rule规则进行分片
z_order是z_user的子表,使用er分片
z_dual使用z_dual-rule规则进行分片
z_dict配置的为全局表
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="MYSQLDB" checkSQLschema="false">
<table name="z_user" primaryKey="ID" rule="test-rule" dataNode="dn0,dn1,dn2">
<childTable parentKey="ID" name="z_order" joinKey="p_id"/>
</table>
<table name="z_dual" primaryKey="ID" rule="z_dual-rule" dataNode="dn0,dn1,dn2"/>
<table name="z_dict" primaryKey="ID" type="global" dataNode="dn0,dn1,dn2"/>
</schema>
<dataNode name="dn0" dataHost="mysql_host0" database="mycat_db0" />
<dataNode name="dn1" dataHost="mysql_host1" database="mycat_db1" />
<dataNode name="dn2" dataHost="mysql_host2" database="mycat_db2" />
<dataHost name="mysql_host0" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
</writeHost>
</dataHost>
<dataHost name="mysql_host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
</writeHost>
</dataHost>
<dataHost name="mysql_host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
</writeHost>
</dataHost>
</mycat:schema>
rule.xml
添加了schema中用到的两个分片规则
<tableRule name="test-rule">
<rule>
<columns>user_code</columns>
<algorithm>test-rule-func</algorithm>
</rule>
</tableRule>
<tableRule name="z_dual-rule">
<rule>
<columns>id</columns>
<algorithm>z_dual-rule-func</algorithm>
</rule>
</tableRule>
<!--...-->
<function name="test-rule-func" class="org.opencloudb.route.function.PartitionByHashMod">
<property name="count">3</property>
</function>
<function name="z_dual-rule-func" class="org.opencloudb.route.function.PartitionByMod">
<property name="count">3</property>
</function>
server.xml
配置访问用的用户名密码等
<mycat:server xmlns:mycat="http://org.opencloudb/">
<user name="yourUsername">
<property name="password">yourPassword</property>
<property name="schemas">MYSQLDB</property>
</user>
</mycat:server>
启动
- 如果是源码的方式,运行这个类
org.opencloudb.MycatStartup
- 如果是下载的编译好的启动
bin\startup_nowrap.bat
如果启动信息不报错,则表明启动成功
测试
测试时jdbc的连接方式与mysql一样,只要修改相应的数据库名称和用户名密码等。
package com.am.mycatclient;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.am.mycatclient.datasource.DataSourceFactory;
import com.am.mycatclient.datasource.TableInfo;
public class App {
public static void main(String[] args) throws Exception {
DataSourceFactory ds = DataSourceFactory.instance();
ds.initDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:8066/MYSQLDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8", "yourUsername", "yourPassword");
testQuery(ds);
}
public static void testQuery(DataSourceFactory ds) {
PreparedStatement pst = null;
Connection con = null;
try {
con = ds.getConnection();
String querySql = "select * from z_dict";
pst = con.prepareStatement(querySql);
ResultSet result = pst.executeQuery();
while (result.next()) {
String id = result.getString("id");
System.out.println(id);
}
} catch (Exception e) {
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}