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.与历史数据合并时可能存在主键冲突