查看TaurusDB的存儲容量
TaurusDB是存儲計算分離架構,數據存儲在共享存儲系統中,共享存儲容量可以通過管理控制臺看到,詳情請參考如下步驟操作,數據每30分鐘更新一次。
操作步驟
步驟 1 登錄管理控制臺。
步驟 2 單擊管理控制臺右上角的
,選擇Region。
步驟 3 在頁面左上角單擊
,選擇“數據庫 > 云數據庫TaurusDB”。
步驟 4 在實例列表中,單擊目標實例名稱,進入實例的“基本信息”頁面。
步驟 5 在基本信息頁面的“存儲“存儲/備份空間”模塊可以看到當前實例占用的共享存儲容量。
說明TaurusDB存儲容量的計算與傳統MySQL有一定的區別,與傳統MySQL使用(數據大小+索引大小+空閑空間)計算的容量數據會有一定的差別。
如果要查詢精確的存儲使用量,可以使用管理控制臺查詢或者連接TaurusDB數據庫后,執行show spaceusage;命令查看當前數據使用的存儲容量,該值為精確值,非估算值。
圖 查看存儲容量


- 共享存儲
- 顯示的使用狀況就是該實例購買的包周期的共享存儲容量及目前數據已占用的容量。
- 如果已使用空間超過購買的共享存儲容量,TaurusDB會自動擴容,無需擔心磁盤滿帶來的業務問題。
- 自動擴容的空間會按照按需使用的收費標準收取,建議超出后使用磁盤容量變更功能擴展包周期的存儲容量。
- 備份空間:
系統會贈送一份與包周期或按需存儲容量相同大小的備份空間。
執行show spaceusage;命令查看存儲容量,其值等于表數據、表預分配空間、分區預分配空間、Binlog、Redolog和Undolog之和,詳情見下表:
| 條目 | 查看方式 | 說明 |
|---|---|---|
| 表數據 | select sum(data_length+index_length+data_free) from information_schema.tables; | 傳統MySQL的容量計算方式,該語句依賴統計數據的精準度,在統計數據未更新時可能會有偏差。 |
| 表預分配空間 | select count(*) from information_schema.tables; | 每張表會預分配4MB空間,該語句查詢出表的數量乘以4MB就是總的表預分配空間。 |
| 分區預分配空間 | select count(*) from INFORMATION_SCHEMA.PARTITIONS where PARTITION_NAME is not null; | 每個分區會預分配4MB空間,該語句查詢出分區的數量乘以4MB就是總的分區預分配空間。 |
| Binlog | show binary logs; | 將所有binlog的文件大小相加。 |
| Redolog | show lsninfo; | flushed_to_disk_lsn- truncate_lsn |
| Undolog | 無法直接查看 | 需要時可咨詢客服人員。 |
修改庫名和修改表名
對于庫重命名和表重命名,TaurusDB與社區MySQL的用法是相同的。
- 支持修改表名:rename table a to b; 注意,該語句是可以跨庫執行的,比如:rename table da.ta to db.ta;是將ta表從da庫移動到db庫。
- 不支持修改庫名,如果有修改庫名的需求,可以先創建新的庫名,然后借助rename table的跨庫執行將所有表從原庫移動到新庫,然后刪除原庫。語句示例:
# 進入原庫
use ta;
# 列出原庫的所有表名
Show tables;
# 查看原庫的創建語句
Show create database ta;
# 使用原庫的創建語句創建新庫(只改庫名,其他參數照抄,這樣能盡量保證新庫與原庫的各類參數相同)
create database tb;
# 將原庫所有表移動至新庫
rename table da.ta to db.ta;
rename table da.tb to db.tb;
rename table da.tc to db.tc;
…
# 刪除原庫
Drop database ta;
字符集和字符序的默認選擇方式
相關變量設置
參數組中默認character_set_server=utf8、collation_server=utf8_general_ci,可以在界面修改參數值。
默認選擇方式
- 在創建數據庫時,如果未顯式指定庫的字符集和字符序,則庫的字符集和字符序采用character_set_server和collation_server參數的值;如果顯式指定,則使用指定的字符集和字符序。
- 在創建數據表時,如果未顯式指定表的字符集和字符序,則表默認字符集和字符序使用所在數據庫的字符集和字符序;如果顯式指定,則使用指定的字符集和字符序。
- 在創建數據表時,如果未顯式指定字段的字符集和字符序,則字段使用所在表的字符集和字符序;如果顯式指定,則使用指定的字符集和字符序。
示例1:不顯式指定字符集、字符序的情況下創建數據庫和數據表。




示例2:顯式指定庫的字符集、字符序的情況下創建數據庫。


示例3:顯式指定表的字符集、字符序的情況下創建數據表。


示例4:顯式指定字段的字符集、字符序的情況下創建數據表。


自增字段值跳變的原因
本節介紹了自增字段相關問題與處理方法。
數據表中的自增字段取值不是連續的,自增值跳變。
出現表中的自增字段取值不連續的情況,可能原因有以下幾種:
- 初值與步長問題,步長不為1會導致自增字段取值不連續。
# 進入原庫
use ta;
# 列出原庫的所有表名
Show tables;
# 查看原庫的創建語句
Show create database ta;
# 使用原庫的創建語句創建新庫(只改庫名,其他參數照抄,這樣能盡量保證新庫與原庫的各類參數相同)
create database tb;
# 將原庫所有表移動至新庫
rename table da.ta to db.ta;
rename table da.tb to db.tb;
rename table da.tc to db.tc;
…
# 刪除原庫
Drop database ta;
- 直接修改表的AUTO_INCREMENT,會導致自增字段取值跳變。
# 進入原庫
use ta;
# 列出原庫的所有表名
Show tables;
# 查看原庫的創建語句
Show create database ta;
# 使用原庫的創建語句創建新庫(只改庫名,其他參數照抄,這樣能盡量保證新庫與原庫的各類參數相同)
create database tb;
# 將原庫所有表移動至新庫
rename table da.ta to db.ta;
rename table da.tb to db.tb;
rename table da.tc to db.tc;
…
# 刪除原庫
Drop database ta;
- 插入數據時直接指定自增字段的取值,會導致自增字段取值跳變。
mysql> select * from animals;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
+----+-----------+
mysql> INSERT INTO animals (id,name) VALUES(100,'rabbit');
Query OK, 1 row affected (0.00 sec)
mysql> select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 100 | rabbit |
+-----+-----------+
9 rows in set (0.00 sec)
- 未提交的事務或回滾的事務,會導致AUTO_INCREMENT增長,但回滾后不會下降。后續如果再次插入數據就會導致數據中的自增字段發生跳變。
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
1 row in set (0.00 sec)
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
mysql> begin;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into auto_test1 values (0),(0),(0);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 |
CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
1 row in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
mysql> insert into auto_test1 values (0),(0),(0);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 7 |
| 8 |
| 9 |
+----+
6 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+-----------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------+
- 數據插入后,AUTO_INCREMENT變化,然后刪除對應的數據行,AUTO_INCREMENT不會下降,后續如果再次插入數據就會導致數據中的自增字段發生跳變。
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
1 row in set (0.00 sec)
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
mysql> insert into auto_test1 values (0),(0),(0);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
6 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
1 row in set (0.00 sec)
mysql> delete from auto_test1 where id>3;
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+----------------------------------------+
| Table | Create Table |
+------------+----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+------------+----------------------------------------+
mysql> insert into auto_test1 values (0),(0),(0);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from auto_test1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 7 |
| 8 |
| 9 |
+----+
6 rows in set (0.00 sec)
mysql> show create table auto_test1;
+------------+-----------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------+
| auto_test1 | CREATE TABLE `auto_test1` (
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------+
- 因為一些原因(比如唯一鍵沖突),使得插入數據最終未成功的,有可能導致AUTO_INCREMENT跳變。
mysql> create table auto_test7(`id` int NOT NULL AUTO_INCREMENT, cred_id int UNIQUE, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.64 sec)
mysql> insert into auto_test7 values(null, 1);
Query OK, 1 row affected (0.03 sec)
mysql> show create table auto_test7;
+------------+-------------------------------+
| Table | Create Table |
+------------+-------------------------------+
| auto_test7 | CREATE TABLE `auto_test7` ( `id` int NOT NULL AUTO_INCREMENT, `cred_id` int DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cred_id` (`cred_id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+------------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into auto_test7 values(null, 1);
ERROR 1062 (23000): Duplicate entry '1' for key 'auto_test7.cred_id'
mysql> show create table auto_test7;
+------------+--------------------------------------------------------------+
| Table | Create Table |
+------------+--------------------------------------------------------------+
| auto_test7 | CREATE TABLE `auto_test7` ( `id` int NOT NULL AUTO_INCREMENT, `cred_id` int DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cred_id` (`cred_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+------------+---------------------------------------------------------------+
- 批量插入數據時(如insert...select、load file等),自增鍵的申請是分批申請的,每批申請2的n次方個序號,用完繼續申請,沒用完也不會退回,所以可能會導致AUTO_INCREMENT跳變。
mysql> create table auto_test7(`id` int NOT NULL AUTO_INCREMENT, cred_id int UNIQUE, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.64 sec)
mysql> insert into auto_test7 values(null, 1);
Query OK, 1 row affected (0.03 sec)
mysql> show create table auto_test7;
+------------+-------------------------------+
| Table | Create Table |
+------------+-------------------------------+
| auto_test7 | CREATE TABLE `auto_test7` ( `id` int NOT NULL AUTO_INCREMENT, `cred_id` int DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cred_id` (`cred_id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+------------+--------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into auto_test7 values(null, 1);
ERROR 1062 (23000): Duplicate entry '1' for key 'auto_test7.cred_id'
mysql> show create table auto_test7;
+------------+--------------------------------------------------------------+
| Table | Create Table |
+------------+--------------------------------------------------------------+
| auto_test7 | CREATE TABLE `auto_test7` ( `id` int NOT NULL AUTO_INCREMENT, `cred_id` int DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cred_id` (`cred_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+------------+---------------------------------------------------------------+
表的自增AUTO_INCREMENT初值與步長
數據表中自增字段的AUTO_INCREMENT的初值與步長由auto_increment_increment和auto_increment_offset參數決定。
- auto_increment_offset:AUTO_INCREMENT值的初值。
- auto_increment_increment:AUTO_INCREMENT值每次增長的步長。
- 當auto_increment_offset > auto_increment_increment 時,實際使用時初值會變為auto_increment_increment。
- 當auto_increment_offset <= auto_increment_increment,自增值計算方式如下:
自增值= auto_increment_offset + N*auto_increment_increment (N為插入的數據條數)
在TaurusDB中這兩個參數默認值都為1,參考如下步驟修改。如需修改時需要在控制臺-實例詳情-參數修改中修改。
步驟 1 登錄管理控制臺。
步驟 2 單擊管理控制臺右上角的
,選擇Region。
步驟 3 在頁面左上角單擊
,選擇“數據庫 > 云數據庫TaurusDB”。
步驟 4 在“實例管理”頁面,選擇指定的實例,單擊實例名稱,進入實例的基本信息頁面。
步驟 5 在左側導航欄中選擇“參數修改”,在“參數”頁簽修改相應參數。
示例:
- auto_increment_offset=1,auto_increment_increment=1,表示初值為1,步長為1。
show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
- 修改auto_increment_increment=2,步長變為2。
set session auto_increment_offset=2;
Query OK, 0 rows affected (0.02 sec)
show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 2 |
| auto_increment_offset | 1 |
+--------------------------+-------+
- auto_increment_offset=10,auto_increment_increment=2,由于auto_increment_offset > auto_increment_increment,因此初值為2,步長為2。
set session auto_increment_offset=10;
set session auto_increment_increment=2;
show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 2 |
| auto_increment_offset | 10 |
+--------------------------+-------+
create table auto_test2(id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.08 sec)
show create table auto_test2;
CREATE TABLE `auto_test2` ( `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
insert into auto_test2 values(0), (0), (0);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
select * from auto_test2;
+----+
| id |
+----+
| 2 |
| 4 |
| 6 |
+----+
3 rows in set (0.01 sec)
- auto_increment_offset=5,auto_increment_increment=10,初值為5,步長為10。
set session auto_increment_offset=10;
set session auto_increment_increment=2;
show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 2 |
| auto_increment_offset | 10 |
+--------------------------+-------+
create table auto_test2(id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.08 sec)
show create table auto_test2;
CREATE TABLE `auto_test2` ( `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
insert into auto_test2 values(0), (0), (0);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
select * from auto_test2;
+----+
| id |
+----+
| 2 |
| 4 |
| 6 |
+----+
3 rows in set (0.01 sec)
修改表的自增AUTO_INCREMENT值
AUTO_INCREMENT修改時,遵循如下約束限制:
- 當AUTO_INCREMENT大于表中數據的最大值時,可以在取值范圍內任意修改為更大的值。
set session auto_increment_offset=10;
set session auto_increment_increment=2;
show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 2 |
| auto_increment_offset | 10 |
+--------------------------+-------+
create table auto_test2(id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.08 sec)
show create table auto_test2;
CREATE TABLE `auto_test2` ( `id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
insert into auto_test2 values(0), (0), (0);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
select * from auto_test2;
+----+
| id |
+----+
| 2 |
| 4 |
| 6 |
+----+
3 rows in set (0.01 sec)
- 當AUTO_INCREMENT大于表中數據的最大值時,如果修改后的指定值仍大于數據的最大值,則修改為指定值成功。否則,默認會修改為數據最大值+1。
mysql> select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| -50 | -middle |
| 1 | dog |
| 2 | cat |
| 50 | middle |
| 100 | rabbit |
+-----+-----------+
mysql> show create table animals;
+---------+-----------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------+
| animals | CREATE TABLE `animals` (
`id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------+
mysql> alter table animals AUTO_INCREMENT=150;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table animals;
+---------+-----------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------+
| animals | CREATE TABLE `animals` (
`id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=150 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------+
mysql> alter table animals AUTO_INCREMENT=50;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table animals;
+---------+-----------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------+
| animals | CREATE TABLE `animals` (
`id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------+
mysql> delete from animals where id=100;
Query OK, 1 row affected (0.00 sec)
mysql> select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| -50 | -middle |
| 1 | dog |
| 2 | cat |
| 50 | middle |
+-----+-----------+
10 rows in set (0.00 sec)
mysql> alter table animals AUTO_INCREMENT=50;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table animals;
+---------+-----------------------------------------------------+
| Table | Create Table |
+---------+-----------------------------------------------------+
| animals | CREATE TABLE `animals` (
`id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------+
1 row in set (0.00 sec)
- AUTO_INCREMENT無法修改為負數。
alter table animals AUTO_INCREMENT=-1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1
自增主鍵達到上限,無法插入數據
場景描述
插入數據時報錯ERROR 1062 (23000): Duplicate entry 'xxx' for key 'xxx'。
原因分析
自增主鍵的字段取值達到上限,無法繼續增長,導致新插入的數據生成的自增主鍵值與表中上一條數據相同,因為自增主鍵的值不可重復,插入失敗報錯。
解決方案
- 如果數據變化較多,表中實際數據量遠小于自增主鍵的容量,則可以考慮將該表的數據全量導入新表,刪除原表,然后rename將新表名改回原表名。(使用數據導入導出的方法有多種實現方法,此處僅舉其中一種例子)
a. 創建表auto_test5_tmp。
create table auto_test5_tmp(id tinyint not null AUTO_INCREMENT, name varchar(8), PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.07 sec)
b. 插入數據。
insert into auto_test5_tmp select 0,name from auto_test5;
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
c. 查詢表數據。
select * from auto_test5_tmp;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | X |
| 5 | Y |
| 6 | Z |
+----+------+
d. 刪除表。
drop table auto_test5;
e. 重命名。
rename table auto_test5_tmp to auto_test5;
Query OK, 0 rows affected (0.12 sec)
- 如果自增主鍵的取值范圍不夠,則修改自增主鍵的字段類型。
alter table auto_test6 modify column id int NOT NULL AUTO_INCREMENT;
Query OK, 6 rows affected (0.15 sec)
Records: 6 Duplicates: 0 Warnings: 0
自增字段取值
TaurusDB對自增字段的賦值有以下幾種方法:
# 表結構
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
- 不對自增字段賦值,數據庫會自動將自增值填入字段中,AUTO_INCREMENT自增。
a. 插入數據。
INSERT INTO animals (name) VALUES ('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');
b. 查詢表數據。
select * from animals;
+----+---------+
| id | name |
+----+---------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
+----+---------+
c. 查詢表結構。
show create table animals;
+---------+--------------------------------------------------+
| Table | Create Table |
+---------+--------------------------------------------------+
| animals | CREATE TABLE `animals` ( `id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |
+---------+--------------------------------------------------+
- 對自增字段賦0或null值,數據庫會自動將自增值填入字段中。AUTO_INCREMENT自增。
a. 插入數據。
INSERT INTO animals (id,name) VALUES(0,'groundhog');
INSERT INTO animals (id,name) VALUES(NULL,'squirrel');
b. 查詢數據。
select * from animals;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
| 7 | groundhog |
| 8 | squirrel |
+----+-----------+
8 rows in set (0.00 sec)
c. 查詢表結構。
show create table animals;
+---------+----------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------+
| animals | CREATE TABLE `animals` ( `id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------+
- 直接使用大于AUTO_INCREMENT的值A,數據庫會將A填入字段并修改AUTO_INCREMENT=A+1。
a. 插入數據。
INSERT INTO animals (id,name) VALUES(100,'rabbit');
b. 查詢數據。
select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
| 7 | groundhog |
| 8 | squirrel |
| 100 | rabbit |
+-----+-----------+
9 rows in set (0.00 sec)
c. 查詢表結構。
show create table animals;
+---------+----------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------+
| animals | CREATE TABLE `animals` ( `id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 |
+---------+----------------------------------------------------------------------+
- 使用小于AUTO_INCREMENT,但不沖突的值。數據可以插入,但AUTO_INCREMENT不變。
mysql> INSERT INTO animals (id,name) VALUES(50,'middle');
Query OK, 1 row affected (0.00 sec)
mysql> select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
| 7 | groundhog |
| 8 | squirrel |
| 50 | middle |
| 100 | rabbit |
+-----+-----------+
10 rows in set (0.00 sec)
mysql> show create table animals;
+---------+------------------------------------------------------------------+
| Table | Create Table |
+---------+------------------------------------------------------------------+
| animals | CREATE TABLE `animals` ( `id` mediumint NOT NULL AUTO_INCREMENT, `name` char(30) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 |
+---------+------------------------------------------------------------------+
- 使用負值數據可以插入,但AUTO_INCREMENT不變。
a. 插入數據。
INSERT INTO animals (id,name) VALUES(-50,'-middle');
b. 查詢數據。
select * from animals;
+-----+-----------+
| id | name |
+-----+-----------+
| -50 | -middle |
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
| 7 | groundhog |
| 8 | squirrel |
| 50 | middle |
| 100 | rabbit |
+-----+-----------+
11 rows in set (0.00 sec)
c. 查詢表結構。
show create table animals;
+---------+------------------------------------------------------------------------------------+
| Table | Create Table
+---------+------------------------------------------------------------------------------------+
| animals | CREATE TABLE `animals` ( `id` mediumint NOT NULL AUTO_INCREMENT,
`name` char(30) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------------------------------------+
自增屬性AUTO_INCREMENT為何未在表結構中顯示
場景描述
創建表時,添加了自增屬性AUTO_INCREMENT,執行 show create table ,自增屬性未在表結構中顯示。
創建表:


執行show create table xxx ,未顯示自增屬性AUTO_INCREMENT:


原因分析
經過排查,是因為參數“sql_mode”設置了NO_FIELD_OPTIONS屬性。
sql_mode相關屬性介紹:
- NO_FIELD_OPTIONS:不要在SHOW CREATE TABLE的輸出中打印MySQL專用列選項。
- NO_KEY_OPTIONS:不要在SHOW CREATE TABLE的輸出中打印MySQL專用索引選項。
- NO_TABLE_OPTIONS:不要在SHOW CREATE TABLE的輸出中打印MySQL專用表選項(例如ENGINE)。
解決方案
將sql_mode的NO_FIELD_OPTIONS屬性去掉即可。
空用戶的危害
MySQL中是允許用戶名為 **'' **的用戶存在,本章節介紹數據庫中存在這種空用戶時的危害。
MySQL中使用空用戶時,它將可以匹配任何用戶名。這一特性也會帶來多種安全性、功能性危害。所以,在實際使用過程中應避免使用空用戶。
- 安全性危害
- 當存在空用戶時,連接時可以使用任意用戶名進行登錄。
- 如果空用戶有密碼,則使用任意用戶名和空用戶的密碼即可登錄數據庫,并獲得空用戶所擁有的所有權限。示例:
#沒有空用戶時,使用非法用戶名‘abcd’,連接失敗
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
mysql -uabcd -h127.0.0.1 -P3306 -pTest_1234
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'abcd'@'localhost' (using password: YES)
# 創建空用戶后,使用非法用戶名‘abcd’,密碼用空用戶的密碼,連接成功
mysql> create user ''@'localhost' IDENTIFIED BY 'Test_1234';
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
mysql -uabcd -h127.0.0.1 -P3306 -pTest_1234
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37Server version: 8.0.22-debug Source distribution
Copyright (c) 2000, 2020, 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>
- 如果空用戶沒有密碼,則使用任意用戶名即可免密登錄數據庫,并獲得空用戶所擁有的所有權限。示例:
#存在無密碼的空用戶時,可以使用任意用戶免密登錄數據庫。
mysql> create user ''@'localhost';
Query OK, 0 rows affected (8.87 sec)
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
mysql -uabcd -h127.0.0.1 -P3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39Server version: 8.0.22-debug Source distribution
Copyright (c) 2000, 2020, 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>
#-----------------
mysql -usdhsjkdshk -h127.0.0.1 -P3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40Server version: 8.0.22-debug Source distribution
Copyright (c) 2000, 2020, 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>
- 功能性危害
當存在空用戶時,可能因為匹配出錯,導致正常的用戶名無法登錄。
示例:存在空用戶與root用戶的host有重疊時,導致root用戶無法使用密碼登錄,或者使用空用戶的密碼登錄后無法進入root的權限。
mysql> create user ''@'localhost';
Query OK, 0 rows affected (8.87 sec)
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
# 用root的密碼無法登錄
mysql -uroot -h127.0.0.1 -P3306 -pTest_root
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
# 用空用戶的密碼(免密)登錄后實際是空用戶登錄,沒有root權限。
mysql -uroot -h127.0.0.1 -P3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 45Server version: 8.0.22-debug Source distribution
Copyright (c) 2000, 2020, 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> select user,host from mysql.user;
ERROR 1142 (42000): SELECT command denied to user ''@'localhost' for table 'user'
慢日志顯示SQL語句掃描行數為0
場景描述
查詢慢日志中記錄SQL執行65秒,但是掃描行數為0。


原因分析
被中斷的查詢超過慢日志設置閾值也會記錄慢日志,但是所記錄的掃描行數為0。客戶JDBC連接設置了查詢超時:


解決方案
優化SQL或者將sockTimeOut設置合理值。