mysql的auto_increment注意点

本文详细介绍了MySQL 5.6.27版本中修复的关于InnoDB中Auto_increment的bug,包括场景重现、分析、影响以及参考链接等内容。重点讨论了重启数据库后空表的auto_increment被重置的现象,并分析了其原因和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql的5.6.27版本修复了一些bugs,其中有一个bug是关于auto_increment的,链接地址在 http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-27.html,
第一条bug fixed就是

InnoDB: Reloading a table that was evicted while empty caused an AUTO_INCREMENT value to be reset. (Bug #21454472, Bug #77743),但是并没有解决数据库重启后空表的auto_increment被重置的场景

一、场景重现:
[root@Kenyon ~]# mysql test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.27-log Source distribution

Copyright (c) 2000, 2015, 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> create table tbl_kenyon(id int auto_increment primary key,vname varchar(32)) auto_increment 1000 engine innodb;
Query OK, 0 rows affected (0.16 sec)

mysql> create table tbl_kenyon2(id int auto_increment primary key,vname varchar(32)) auto_increment 1000 engine innodb;
Query OK, 0 rows affected (0.04 sec)

mysql> insert into tbl_kenyon (vname) values('Just test');
Query OK, 1 row affected (0.03 sec)

mysql> show create table tbl_kenyon \G
*************************** 1. row ***************************
       Table: tbl_kenyon
Create Table: CREATE TABLE `tbl_kenyon` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table tbl_kenyon2 \G
*************************** 1. row ***************************
       Table: tbl_kenyon2
Create Table: CREATE TABLE `tbl_kenyon2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
--重启数据库后,可以看到auto_increment变化了
mysql> show create table tbl_kenyon\G
*************************** 1. row ***************************
       Table: tbl_kenyon
Create Table: CREATE TABLE `tbl_kenyon` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> show create table tbl_kenyon2\G
*************************** 1. row ***************************
       Table: tbl_kenyon2
Create Table: CREATE TABLE `tbl_kenyon2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vname` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

二、分析

innodb的auto_increment值并没有固化下来,而是在内存中,假如重启了db,那么会被重置为当前表中最大的自增值,myisam的不受此影响,因为值是存在.MYI文件里的。早期的版本中可能还有另一种奇葩的情况是建一个空表,alter表重置auto_increment,不去操作过一段时间后自己会被重置成1,不过这个bug官方给出的答复是在5.6.27被修复了。

三、影响

1.对自增值有要求的场景存在风险
2.与历史数据合并时可能存在主键冲突

四、参考:
1.http://bugs.mysql.com/bug.php?id=78491
2.https://bugs.mysql.com/bug.php?id=77743


转载于:https://my.oschina.net/Kenyon/blog/537843

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值