問題
某日,二寶寶一臉迷茫的甩給我一張圖,這些太扯了,AUTO_INCREMENT竟然還能往回收縮。
如圖:2-14 10:39:47的時候,insert返回的主鍵是35,可是,爲什麼在16:54:57分的時候,insert返回的自增主鍵變成了30
懷疑
懷疑1
因爲30-35創建的數據,不久之後因爲其他原因刪除了,所以懷疑刪除數據 AUTO_INCREMENT的值會變小
打臉第一次
步驟
- 創建數據表主鍵設置爲自增長
- 插入3條數據
- 刪除1條數據
- 再插入
- 結果主鍵是4
打臉第二次
剛纔測試用的是mariadb 10.1的docker環境
於是換到了目標機器的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看起來往回收縮了
思考
- 業務方面強依賴單一主鍵,且沒有校驗機制(無法校驗這個數據是不是屬於某個人),最好不要刪除數據,否則會有漏洞,而且查找非常困難。