mycat 垂直切分和水平切分配置示例node
首先是數據庫表的分佈:
192.168.0.83:db1數據庫 test1表;db3數據庫 user表 user_detail表
192.168.0.165:db1數據庫 test2表;db3數據庫 user表 user_detail表
sql:mysql
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50713 Source Host : localhost:3306 Source Database : db1 Target Server Type : MYSQL Target Server Version : 50713 File Encoding : 65001 Date: 2016-08-26 17:23:56 */
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `test1`
-- ----------------------------
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test1
-- ----------------------------
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50713 Source Host : localhost:3306 Source Database : db3 Target Server Type : MYSQL Target Server Version : 50713 File Encoding : 65001 Date: 2016-08-26 17:24:04 */
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` ( `id` int(11) NOT NULL, `user_id` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50713 Source Host : localhost:3306 Source Database : db3 Target Server Type : MYSQL Target Server Version : 50713 File Encoding : 65001 Date: 2016-08-26 17:24:18 */
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user_detail`
-- ----------------------------
DROP TABLE IF EXISTS `user_detail`;
CREATE TABLE `user_detail` ( `id` int(11) NOT NULL, `user_id` varchar(255) DEFAULT NULL, `detail` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_detail
-- ----------------------------
/* Navicat MySQL Data Transfer Source Server : song Source Server Version : 50713 Source Host : 192.168.0.165:3306 Source Database : db1 Target Server Type : MYSQL Target Server Version : 50713 File Encoding : 65001 Date: 2016-08-26 17:24:27 */
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `test2`
-- ----------------------------
DROP TABLE IF EXISTS `test2`;
CREATE TABLE `test2` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test2
-- ----------------------------
schema.xml文件:web
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
<schema name="qs" checkSQLschema="false" sqlMaxLimit="100">
<!-- 垂直切分 -->
<table name="test1" primaryKey="id" type="global" dataNode="nodeTest1" />
<table name="test2" primaryKey="id" type="global" dataNode="nodeTest2" />
<!-- 全局表的配置以下(好比配置文件的數據,數據不大不多變更的,可是常常用到查詢的) -->
<!-- <table name="t_area" primaryKey="id" type="global" dataNode="dn1,dn2" /> -->
<!-- 水平切分 -->
<!-- ER分片表:如user表和user_detail表的關聯字段user_id;保證相同user_id的數據在同一塊片區上 -->
<table name="user" primaryKey="user_id" dataNode="nodeUser01,nodeUser02" rule="mod-long">
<childTable name="user_detail" primaryKey="id" joinKey="user_id" parentKey="user_id" />
</table>
</schema>
<dataNode name="nodeUser01" dataHost="dataHost01" database="db3" />
<dataNode name="nodeUser02" dataHost="dataHost02" database="db3" />
<dataNode name="nodeTest1" dataHost="dataHost01" database="db1" />
<dataNode name="nodeTest2" dataHost="dataHost02" database="db1" />
<dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.0.83:3306" user="root" password="root"/>
</dataHost>
<dataHost name="dataHost02" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.0.165:3306" user="root" password="root"/>
</dataHost>
</mycat:schema>
注: rule=」mod-long」 分片規則是根據分片字段求模運算;
分片規則有:固定分片hash,範圍約定,一致性hash,按日期範圍等。
詳見官網文檔 http://www.mycat.org.cn/document/Mycat_V1.6.0.pdfsql
測試:
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)數據庫
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.svg
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.測試
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.url
mysql> show databases;
+———-+
| DATABASE |
+———-+
| qs |
+———-+
1 row in set (0.00 sec)spa
mysql> show tables;
+————–+
| Tables in qs |
+————–+
| test1 |
| test2 |
| user |
| user_detail |
+————–+
4 rows in set (0.00 sec)code