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

Posted chinesern

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

[[email protected] conf]# more /usr/local/mycat/conf/server.xml |grep sequnceHandlerType
                <!--<property name="sequnceHandlerType">0</property>-->


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

技术分享图片

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

[[email protected] 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

[[email protected] conf]# vi sequence_db_conf.properties
#sequence stored in datanode
GLOBAL=dn1
COMPANY=dn1
CUSTOMER=dn1
ORDERS=dn1
PERSON=dn1

配置schema.xml

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
            <table name="person" primaryKey="id" autoIncrement="true"
                   dataNode="dn1,dn2,dn3" rule="sharding-by-murmurperson" />
        
    </schema>


    <dataNode name="dn1" dataHost="mysqlserver" database="db1" />
    <dataNode name="dn2" dataHost="mysqlserver" database="db2" />
    <dataNode name="dn3" dataHost="mysqlserver" database="db3" />


    <!--######### TESTDB  ########-->
    <dataHost name="mysqlserver" maxCon="1000" minCon="10" balance="0"
            writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
            <heartbeat>select user()</heartbeat>
            <writeHost host="hostM1" url="192.168.2.135:3306" user="root" password="root123">
            </writeHost>
    </dataHost>
</mycat:schema> 

配置rule.xml


id
murmur

     <function name="murmur"
            class="org.opencloudb.route.function.PartitionByMurmurHash">
            <property name="seed">0</property><!-- 默认是0 -->
            <property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
            <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
            <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 -->
            <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 
                    用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
    </function>

重启mycat

[[email protected] conf]# mycat restart
Stopping Mycat-server...
Mycat-server was not running.

[[email protected] ~]$  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(NEXT VALUE FOR MYCATSEQ_PERSON,'xiaoxu', 100, 30,
       'shanghai-jiuting','072450','1987-02-01',100);

技术分享图片

插入成功!!!

以上是关于Mycat实战之主键数据库自增方式的主要内容,如果未能解决你的问题,请参考以下文章

Mycat学习实战-Mycat全局主键

mycat分片表全局自增主键测试

mycat全局系列号的学习和配置

mycat 主键自增问题

MyCAT全局序列号-数据库方式

Mycat 设置全局序列号