mysql诡异的AUTO_INCREMENT收缩

问题

某日,二宝宝一脸迷茫的甩给我一张图,这些太扯了,AUTO_INCREMENT竟然还能往回收缩。
在这里插入图片描述
如图:2-14 10:39:47的时候,insert返回的主键是35,可是,为什么在16:54:57分的时候,insert返回的自增主键变成了30

怀疑

怀疑1

因为30-35创建的数据,不久之后因为其他原因删除了,所以怀疑删除数据 AUTO_INCREMENT的值会变小

打脸第一次

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eKiHk03m-1582528571521)(en-resource://database/1208:1)]

步骤

  • 创建数据表主键设置为自增长
  • 插入3条数据
  • 删除1条数据
  • 再插入
  • 结果主键是4

打脸第二次

刚才测试用的是mariadb 10.1docker环境
于是换到了目标机器的mariadb 5.5进行测试,结论一样,脸好疼

怀疑2

有人手动动了数据库,但是没有找到有利证据

分析

怀疑没有用,还是得动手分析日志,由于我还是个小学生,找了关于没有找到日志。在朋友的帮助下,找到了/var/log/mariadb/mariadb.log

有些发现

Version: '5.5.64-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
191202 18:35:21 [Warning] IP address '1.80.37.244' could not be resolved: Name or service not known
191203 22:03:33 [Warning] IP address '202.168.151.68' could not be resolved: Name or service not known
191204 10:26:01 [Warning] IP address '1.80.38.159' could not be resolved: Name or service not known
191204 13:04:29 [Warning] IP address '61.183.35.82' could not be resolved: Name or service not known
200214 12:15:27 [Note] /usr/libexec/mysqld: Normal shutdown
200214 12:15:27 [Note] Event Scheduler: Purging the queue. 0 events
200214 12:15:28  InnoDB: Starting shutdown...
200214 12:15:29  InnoDB: Shutdown completed; log sequence number 342490931
200214 12:15:29 [Note] /usr/libexec/mysqld: Shutdown complete

200214 12:15:29 mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended
200214 12:16:07 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
200214 12:16:07 [Note] /usr/libexec/mysqld (mysqld 5.5.64-MariaDB) starting as process 1451 ...
200214 12:16:07 InnoDB: The InnoDB memory heap is disabled
200214 12:16:07 InnoDB: Mutexes and rw_locks use GCC atomic builtins
200214 12:16:07 InnoDB: Compressed tables use zlib 1.2.7
200214 12:16:07 InnoDB: Using Linux native AIO
200214 12:16:07 InnoDB: Initializing buffer pool, size = 128.0M
200214 12:16:07 InnoDB: Completed initialization of buffer pool

仔细看,0214 12:15:27数据库被重启了,因为测试服务器是账号密码登录的,虽然修改了默认的SSH端口号,但是有小贱人,还是不停的用肉机来测试,所以重启了机器。

最终结论

[luojie@localhost proto]$ sh ~/mysql.sh 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.44-MariaDB-1~bionic mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use test;
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
MariaDB [test]> select * from ai_test;
+----+-------------+
| id | name        |
+----+-------------+
|  8 | 二宝宝11    |
|  9 | 二宝宝12    |
| 10 | 二宝宝13    |
| 11 | 二宝宝14    |
| 12 | 二宝宝15    |
| 13 | 二宝宝16    |
+----+-------------+
6 rows in set (0.00 sec)

MariaDB [test]> delete from ai_test where id > 10;
Query OK, 3 rows affected (0.01 sec)

MariaDB [test]> select * from ai_test;
+----+-------------+
| id | name        |
+----+-------------+
|  8 | 二宝宝11    |
|  9 | 二宝宝12    |
| 10 | 二宝宝13    |
+----+-------------+
3 rows in set (0.00 sec)
MariaDB [test]> show create table ai_test;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ai_test | CREATE TABLE `ai_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

删除了Id>10的数据,查看AUTO_INCREMENT还是14,下面我们重启一下这个docker环境的数据库

[luojie@localhost proto]$ sudo docker restart Master
[sudo] password for luojie: 
Master
[luojie@localhost proto]$ sh ~/mysql.sh 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.44-MariaDB-1~bionic mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show create table ai_test;
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> use test;
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
MariaDB [test]> show create table ai_test;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ai_test | CREATE TABLE `ai_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]>

原来重启数据库之后AUTO_INCREMENT会被新计算,所以导致了AUTO_INCREMENT看起来往回收缩了

思考

  • 业务方面强依赖单一主键,且没有校验机制(无法校验这个数据是不是属于某个人),最好不要删除数据,否则会有漏洞,而且查找非常困难。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章