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

mycat 主键自增问题

时间:2019-05-07 01:34:08      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:使用   dom   127.0.0.1   ast   schema   create   设置   type   class   

MyCAT自增字段和返回生成的主键ID的经验分享
说明:
1、mysql本身对非自增长主键,使用last_insert_id()是不会返回结果的,只会返回0.
2、mysql只会对定义自增长主键,可以用last_insert_id()返回主键值。

mycat目前提供了自增长主键功能,但是如果对应的mysql节点上数据表,没有定义auto_increment,
那么在mycat层调用last_insert_id()也是不会返回结果的。
正确使用方式如下:
1、mysql定义自增主键
CREATE TABLE `tt2` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `nm` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
2、mycat定义自增
[root@test conf]# vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">

        <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
            <!-- random sharding using mod sharind rule -->
        <!-- autoIncrement="true" 属性-->
            <table name="tt2" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2,dn3,dn4,dn5" rule="mod-long" />
            <table name="mycat_sequence" primaryKey="name" dataNode="dn1"/>
        </schema>

        <dataNode name="dn1" dataHost="localhost1" database="db1" />
        <dataNode name="dn2" dataHost="localhost1" database="db2" />
        <dataNode name="dn3" dataHost="localhost1" database="db3" />
        <dataNode name="dn4" dataHost="localhost1" database="db4" />
        <dataNode name="dn5" dataHost="localhost1" database="db5" />

        <dataHost name="localhost1" maxCon="1000" minCon="20" balance="0" writeType="0" dbType="mysql" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="127.0.0.1:3366" user="root" password="123456">
                </writeHost>
        </dataHost>
</mycat:schema>

3、mycat对应sequence_db_conf.properties增加相应设置;
4、mycat的对应mycat_sequence增加对应记录。
5、链接mycat,测试结果如下:
 
127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.14 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              101 |
+------------------+
1 row in set (0.01 sec)

127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.00 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              102 |
+------------------+
1 row in set (0.00 sec)

127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.00 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              103 |
+------------------+
1 row in set (0.00 sec)

127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.01 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              104 |
+------------------+
1 row in set (0.00 sec)

127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.00 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              105 |
+------------------+
1 row in set (0.00 sec)

127.0.0.1/root:[TESTDB> insert into tt2(nm) values (99);
Query OK, 1 row affected (0.00 sec)

127.0.0.1/root:[TESTDB> select last_insert_id();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|              106 |
+------------------+
1 row in set (0.00 sec)

 

mycat 主键自增问题

标签:使用   dom   127.0.0.1   ast   schema   create   设置   type   class   

原文地址:https://www.cnblogs.com/lshan/p/10823108.html

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