Mycat实战之主键数据库自增方式

创建一个 person表,主键为Id,hash方式分片,主键自增(采用数据库方式)

#person表结构如下
Id,主键,Mycat自增主键
name,字符串,16字节最长
school,毕业学校,数字,1-1000范围,是学校编号
age,年龄,18-60
addr,地址,32字节,建议为 gz-tianhe(城市-地区两级 枚举的仿真数据)
zcode,邮编,
birth,生日,为日期类型,  1980到2010年之间随机的日期
score,得分,0-100分

1. 机器环境

192.168.2.136   mycat1
192.168.2.135   mysql
192.168.2.134   test

2. mycat参数文件配置

编辑server.xml

[root@localhost conf]# more /usr/local/mycat/conf/server.xml |grep sequnceHandlerType
                


#0 代表文件方式,
#1 代表数据库方式
#2 代表本地时间戳方式

重启mycat生效 登陆管理端口验证

[root@localhost conf]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...

3.数据库配置,创建 sequence 相关的表以及 function

#所有操作都在 db1 上
#创建 MYCAT_SEQUENCE 表
DROP TABLE IF EXISTS MYCAT_SEQUENCE;
– name sequence 名称
– current_value 当前 value
– increment 增长步长!   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;

#插入一条 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 ;

4.修改mycat 相关配置

sequence_db_conf.properties

[root@mysqldb conf]# vi sequence_db_conf.properties
#sequence stored in datanode
GLOBAL=dn1
COMPANY=dn1
CUSTOMER=dn1
ORDERS=dn1
PERSON=dn1

配置schema.xml



    
            
    
    


    
    
            select user()
            
            
    
  
 

配置rule.xml


id
murmur

     
            0
            2
            160
            
            
    

重启mycat

[root@mycat conf]# mycat restart
Stopping Mycat-server...
Mycat-server was not running.

[mysql@mycat ~]$  mysql -utest -ptest  -h192.168.2.136 -P8066
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 4
Server version: 5.5.8-mycat-1.5.1-RELEASE-20161130213509 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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.

mysql> 
mysql> 
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| person           |
+------------------+
1 row in set (0.00 sec)

5 mycat中创建person表并插入数据验证

#创建 person 表:
CREATE TABLE person(id INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(16),school INT, 
             age INT, addr VARCHAR(32),zcode VARCHAR(6),birth DATETIME,score INT) ;

#插入数据 
insert into person(id,name, school, age, addr, zcode, birth, score) 
       values( 1213706761350025216,'xiaoxu', 100, 30,
       'shanghai-jiuting','072450','1987-02-01',100);

插入成功!!!

你可能感兴趣的:(Mycat实战之主键数据库自增方式)