码迷,mamicode.com
首页 > 其他好文 > 详细

mycat设置主键自增—sequence的配置

时间:2020-11-30 15:53:10      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:any   cap   store   sql   ini   tail   数据库配置   set   str   

此文转载自:https://blog.csdn.net/ZHAI_KE/article/details/110064468

mycat参数文件配置

1. 配置server.xml文件

(#0 代表文件方式,#1 代表数据库方式,#2 代表本地时间戳方式)
<property name="sequnceHandlerType">1</property>

技术图片

2. 数据库配置

#所有操作都在一个物理数据库(dn1)上
#创建 MYCAT_SEQUENCE 表
#name sequence 名称
#current_value 当前 value
#increment 增长步长!   
#mycat在数据库中一次读取多少个sequence 

DROP TABLE IF EXISTS MYCAT_SEQUENCE;
CREATE TABLE MYCAT_SEQUENCE (
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ‘名称‘,
  `current_value` int(11) NOT NULL COMMENT ‘当前值‘,
  `increment` int(11) NOT NULL DEFAULT 1 COMMENT ‘步长‘,
  PRIMARY KEY (`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

#插入一条 sequence
INSERT INTO MYCAT_SEQUENCE(name,current_value,increment) VALUES (‘PERSON’,
100000, 100);

#创建相关 function
#获取当前 sequence 的值 (返回当前值,增量)
DROP FUNCTION IF EXISTS mycat_seq_currval;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `mycat_seq_currval`(seq_name VARCHAR(50))
RETURNS VARCHAR(64) charset utf8
DETERMINISTIC
BEGIN
DECLARE retval VARCHAR(64);
SET retval="-999999999,null";
SELECT CONCAT(CAST(current_value AS CHAR),",",CAST(increment AS
CHAR) ) INTO retval
FROM MYCAT_SEQUENCE WHERE NAME = seq_name;
RETURN retval ;
END
;;
DELIMITER ;


#设置 sequence 值
DROP FUNCTION IF EXISTS mycat_seq_setval;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION mycat_seq_setval(seq_name VARCHAR(50),value INTEGER) 
RETURNS varchar(64) CHARSET utf8
DETERMINISTIC
BEGIN
UPDATE MYCAT_SEQUENCE
SET current_value = value
WHERE name = seq_name;
RETURN mycat_seq_currval(seq_name);
END ;;
DELIMITER ;

#获取下一个 sequence 值
DROP FUNCTION IF EXISTS `mycat_seq_nextval`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `mycat_seq_nextval`(seq_name VARCHAR(50)) 
RETURNS VARCHAR(64)CHARSET utf8
DETERMINISTIC
BEGIN
UPDATE MYCAT_SEQUENCE SET current_value = current_value + increment
WHERE NAME = seq_name;
RETURN mycat_seq_currval(seq_name);
END;;
DELIMITER ;

3. 修改mycat 相关配置

sequence_db_conf.properties
#sequence stored in datanode
GLOBAL=dn1
COMPANY=dn1
CUSTOMER=dn1
ORDERS=dn1
SYS_USER=dn1
TBL_MEMBER=dn1

技术图片

配置schema.xml
<schema name="DB1" checkSQLschema="false" sqlMaxLimit="100">
	<table name="sys_user" primaryKey="user_id" autoIncrement="true" dataNode="dn$1-2" rule="rule1" splitTableNames ="true"/>            
</schema>

<dataNode name="dn$1-2" dataHost="localhost1" database="db$1-2" />

<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="xxxx:8066" user="mycat" password="xxxxxx">
	</writeHost>
</dataHost>

4. 重启mycat

./mycat restart

mycat设置主键自增—sequence的配置

标签:any   cap   store   sql   ini   tail   数据库配置   set   str   

原文地址:https://www.cnblogs.com/phyger/p/14035026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!