Earlier we introduced the sub-library sub-table operation MyCat, then the data in the same table will be stored in a different database, which involves the 主键
maintenance problem, this time certainly can not use a single database in a manner id increment to handle , and then the way we can provide through MyCat to achieve growth in several
Global primary key increment
First, the local file increment mode
First we look at Dir a way, that is 本地文件自增
the way
1. Modify Partitioning Strategies
Partitioning strategy of our original configuration crc32slot
is not supported by the primary key increment, so we need to modify theauto-sharding-long
2. Modify the server.xml file
server.xml file sequnceHandlerType
is used to configure the generated key type
sequnceHandlerType value | Explanation |
---|---|
0 | Local file increment mode |
1 | Database increment mode |
2 | Local timestamp increment mode |
So we need to first modify the value of sequnceHandlerType0
3.sequence_conf.properties Introduction
Conf directory under sequence_conf.properties
the relevant configuration information sequence
#Wed Oct 16 07:40:44 CST 2019
COMPANY.MAXID=2000
GLOBAL.MAXID=20000
COMPANY.HISIDS=
CUSTOMER.MAXID=2000
HOTNEWS.CURID=1000
ORDER.MINID=1001
CUSTOMER.HISIDS=
HOTNEWS.MINID=1001
GLOBAL.CURID=10002
ORDER.MAXID=2000
COMPANY.CURID=1000
CUSTOMER.CURID=1000
COMPANY.MINID=1001
GLOBAL.MINID=10001
HOTNEWS.MAXID=2000
CUSTOMER.MINID=1001
GLOBAL.HISIDS=
HOTNEWS.HISIDS=
ORDER.HISIDS=
ORDER.CURID=1000
The main thing is GLOBAL.MAXID=20000
GLOBAL.CURID=10002
GLOBAL.MINID=10001
you can set your own
4. Test realization
Insert statement primary key field with next value for MYCATSEQ_GLOBAL
alternative
insert into t_user(id,name,age)values(next value for MYCATSEQ_GLOBAL,'HG-93',23)
Second, the local timestamp increment mode
Use the time stamp of the way, we do not need, or choose a different allocation strategy of partitioning strategy.
1. Modify the server.xml file
The server.xml file is sequnceHandlerType
modified to2
2. Restart mycat
Modify the configuration file, you need to restart the service to make its entry into force.
3. Insert test data
insert into t_user(id,name,age)values(next value for MYCATSEQ_GLOBAL,'HG-93',23)
Too long a time stamp will be id
revised to varchar
type.
Generate success ~
Third, the database auto-increment mode
1. Create a sequence table and related functions
The third way is to create in Mycat managed in a database table structure to increment a maintenance-related data, relevant official script, as follows:
DROP TABLE IF EXISTS MYCAT_SEQUENCE;
CREATE TABLE MYCAT_SEQUENCE (
NAME VARCHAR (50) NOT NULL,
current_value INT NOT NULL,
increment INT NOT NULL DEFAULT 100,
PRIMARY KEY (NAME)
) ENGINE = INNODB ;
INSERT INTO MYCAT_SEQUENCE(NAME,current_value,increment) VALUES ('GLOBAL', 100000, 100);
DROP FUNCTION IF EXISTS `mycat_seq_currval`;
DELIMITER ;;
CREATE 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 ;
DROP FUNCTION IF EXISTS `mycat_seq_nextval`;
DELIMITER ;;
CREATE 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 ;
DROP FUNCTION IF EXISTS `mycat_seq_setval`;
DELIMITER ;;
CREATE 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 ;
We call these scripts demo2
to perform on
2. Modify the server.xml
3. Modify sequence_db_conf.properties file
Because the demo2
corresponding logical library is dn2
so we need to change here
4. Test
Restart the service and insert test data
insert into t_user(id,name,age)values(next value for MYCATSEQ_GLOBAL,'hg-93',23)
Successful primary key generation, in addition to these three methods may also be maintained by the auto-increment primary key `zookeeper`, this can be realized on their own