参考: https://blog.51cto.com/9025736/2500516
MySQL [db02]> grant all on *.* to ‘ops‘@‘%‘ identified by ‘hanye131‘;
MySQL [db02]> flush privileges;
MySQL [db02]> CREATE DATABASE `db01` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
MySQL [db02]> CREATE DATABASE `db02` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
分别创建erp_agents表
CREATE TABLE `erp_agents` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`org_id` int(10) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘组织架构id‘,
`org_names` varchar(191) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT ‘所属组织结构‘,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=362 DEFAULT CHARSET=utf8
[root@k8s-master01 data]# wget https://github.com/MyCATApache/Mycat-download/blob/master/1.5-RELEASE/Mycat-server-1.5.1-RELEASE-20161130213509-linux.tar.gz
[root@k8s-master01 data]# yum install -y java-11-openjdk java-11-openjdk-devel
在rule.xml中定义了各种myCat支持的分片规则。
取模mod-long
自然月分片 sharding-by-month
按日期(天)分片sharding-by-date
按单月小时拆分sharding-by-hour
范围约定,提前规划好分片字段某个范围属于哪个分片,auto-sharding-long
范围求模分片
取模范围约束sharding-by-pattern
分片枚举sharding-by-intfile
固定分片hash算法
截取数字hash解析sharding-by-stringhash
一致性hash
日期范围hash分片rangeDateHash
截取数字做hash求模范围约束sharding-by-prefixpattern
应用指定,在运行阶段有应用自主决定路由到那个分片。sharding-by-substring
冷热数据分片 sharding-by-date
有状态分片算法
crc32slot分片算法
注意:
id中推荐配置主键列
所有的 tableRule 只能使用一次。如果需要为多个表配置相同的分片规则,那么需要在此重新定义该规则。
在 crc32Slot 算法中的分片数量一旦给定,MyCat 会将该分片数量和 slor 的取值范围保存到文件中。在次修改分片数量时是不会生效的,需要将该文件删除。文件位置位于 conf目录中的 ruledata 目录中。
server.xml
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
<system>
<property name="defaultSqlParser">druidparser</property>
</system>
<user name="ops">
<property name="password">hanye131</property>
<property name="schemas">hanye,information_schema,sys,user</property>
</user>
</mycat:server>
rule.conf #注意此处 由于我是分库分了两个库 需要吧count值修改为2
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
<schema name="user" checkSQLschema="true" sqlMaxLimit="100" >
<table name="erp_agents" primaryKey="ID" dataNode="dn1,dn2" rule="mod-long" />
</schema>
<schema name="hanye" checkSQLschema="false" sqlMaxLimit="100" dataNode="hanye" >
</schema>
<schema name="information_schema" checkSQLschema="false" sqlMaxLimit="10000" dataNode="hanye">
</schema>
<schema name="sys" checkSQLschema="false" sqlMaxLimit="10000" dataNode="hanye">
</schema>
<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
/> -->
<dataNode name="dn1" dataHost="localhost1" database="db01" />
<dataNode name="dn2" dataHost="localhost1" database="db02" />
<dataNode name="hanye" dataHost="23_29server" database="hanye" />
<dataHost name="23_29server" maxCon="5000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="60">
<heartbeat>show slave status</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hanyeM1" url="192.168.31.5:3306" user="ops" password="hanye131">
<readHost host="hanyeS2" url="192.168.31.6:3306" user="ops" password="hanye131" weight="1"/>
<readHost host="hanyeS3" url="192.168.31.7:3306" user="ops" password="hanye131" weight="2" />
</writeHost>
</dataHost>
<dataHost name="localhost1" maxCon="5000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="60">
<heartbeat>show slave status</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hanyem1" url="192.168.31.5:3306" user="ops" password="hanye131">
<readHost host="hanyes2" url="192.168.31.7:3306" user="ops" password="hanye131" weight="2" />
</writeHost>
</dataHost>
</mycat:schema>
[root@k8s-master01 mycat]# ./bin/mycat restart
[root@k8s-master01 mycat]# mysql -uops -phanye131 -h192.168.31.10 -P8066
MySQL [(none)]> use user;
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (1, 1, ‘广州公司‘, ‘系统管理员1‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (2, 1, ‘广州富司‘, ‘系统管理员2‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (3, 1, ‘广州富司‘, ‘系统管理员3‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (4, 1, ‘广州富司‘, ‘系统管理员4‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (5, 1, ‘广州富司‘, ‘系统管理员5‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (6, 1, ‘广州富司‘, ‘系统管理员6‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (7, 1, ‘广州富司‘, ‘系统管理员7‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (8, 1, ‘广州富司‘, ‘系统管理员8‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (9, 1, ‘广州富司‘, ‘系统管理员10‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (10, 1, ‘广州公司‘, ‘系统管理员11‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (11, 1, ‘广州公司‘, ‘系统管理员12‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (12, 1, ‘广州公司‘, ‘系统管理员13‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (13, 1, ‘广州公司‘, ‘系统管理员14‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (14, 1, ‘广州公司‘, ‘系统管理员15‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (15, 1, ‘广州公司‘, ‘系统管理员16‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (16, 1, ‘广州公司‘, ‘系统管理员17‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (17, 1, ‘广州公司‘, ‘系统管理员18‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (18, 1, ‘广州公司‘, ‘系统管理员19‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (18, 1, ‘广州公司‘, ‘系统管理员20‘);
INSERT INTO `erp_agents`(id,org_id,org_names,name) VALUES (20, 1, ‘广州公司‘, ‘系统管理员21‘);
[root@k8s-master01 mycat]# mysql -uops -phanye131 -h192.168.31.10 -P8066
MySQL [user]> use user;
MySQL [user]> select * from erp_agents;
[root@k8s-master01 mycat]# mysql -uops -phanye131 -h192.168.31.5
1: Mycat查表报错find no Route:select * from `db_user`.`users` limit 0, 1000;
处理方式:
<schema name="user" checkSQLschema="true" sqlMaxLimit="100" >
修改: checkSQLschema="true" 即可
2:Startup failed: Timed out waiting for a signal from the JVM.
JVM did not exit on request, terminated
解决办法
在wrapper.conf中添加
wrapper.startup.timeout=300 //超时时间300秒
wrapper.ping.timeout=120
3:Can‘t find a valid data node for specified node index :ERP_AGENTS -> ID -> 3
处理方式:
修改对应的Rule.conf
<function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property> 修改为对应的分库个数 即可
mycal 1.5 给予主键ID mod-long进行分库分表
原文:https://blog.51cto.com/9025736/2535107