MySQL/MariaDB數據庫的存儲引擎
html
做者:尹正傑node
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。mysql
一.MySQL體系結構sql
鏈接管理模塊:
負責接收遠程用戶的鏈接。
線程管理模塊:
維護一個線程池,經過鏈接管理模塊接入的鏈接會在線程池中分配一個空閒線程去響應,當用戶端口鏈接後線程資源會被回收(並不會被銷燬),即從新放回線程池。
用戶管理:
負責驗證用戶是否合法。
命名分發器:
負責分發命令到相應模塊並記錄日誌信息。
查詢緩存模塊:
負責緩存曾經執行SQL的返回結果。當下次查詢到與之匹配的結果就直接返回緩存結果。
日誌記錄模塊:
用戶執行命令是由相應日誌的,負責記錄用戶操做記錄以便於往後審計或者數據恢復等。
命令解析器:
負責解析用戶執行的SQL語句
查詢優化器:
選擇最優的查詢路徑,好比使用索引查詢等。
表變動模塊:
表的變動管理,好比對錶進行重命名操做。
表維護模塊:
表的維護管理,好比對錶中的內容進行增刪改操做。
複製模塊:
用於複製的模塊,好比MySQL的主從複製就得用它的功能實現。
狀態模塊:
讓用戶瞭解MySQL數據庫當前的工做狀態。當登陸數據庫成功時能夠使用一個叫作"status"命令查看當前數據庫相關狀態。
訪問控制模塊:
負責權限管理,雖然用戶模塊已經驗證是否由該用戶,可是否有權限還得訪問控制模塊來管理。
表管理模塊:
管理表的模塊調用存儲引擎負責來實現數據存儲。
存儲引擎接口:
數據庫的數據最終仍是保存到磁盤上,所以須要專門的存儲引擎接口讓用戶調用,好比myisam,innodb,NDB,Memory等。
二. 存儲引擎概述數據庫
存儲引擎說白了就是如何管理數據庫中的數據在磁盤上的存儲文件。 筆記: 1.在服務器而不是在存儲引擎中實現。 2.僅當使用壓縮行格式時,才支持壓縮MyISAM表。在MyISAM中使用壓縮行格式的表是隻讀的。 3.經過加密功能在服務器中實現。 4.經過加密功能在服務器上實現;在MySQL 5.7和更高版本中,支持靜態數據表空間加密。 5. MySQL Cluster NDB 7.3和更高版本提供了對外鍵的支持。 6. MySQL 5.6和更高版本提供了對FULLTEXT索引的InnoDB支持。 7. MySQL 5.7和更高版本提供了InnoDB對地理空間索引的支持。 8. InnoDB在內部將哈希索引用於其自適應哈希索引功能。 博主推薦閱讀: https://docs.oracle.com/cd/E17952_01/mysql-5.5-en/storage-engines.html
1>.MyISAM引擎緩存
MyISAM引擎特色 1.不支持事務 2.表級鎖定 3.讀寫相互阻塞,寫入不能讀,讀時不能寫 4.只緩存索引 5.不支持外鍵約束 6.不支持聚簇索引 7.讀取數據較快,佔用資源較少 8.不支持MVCC(多版本併發控制機制)高併發 9.崩潰恢復性較差 10.MySQL5.5.5前默認的數據庫引擎 MyISAM存儲引擎適用場景 只讀(或者寫較少)、表較小(能夠接受長時間進行修復操做) MyISAM引擎文件 tbl_name.frm: 表格式定義 tbl_name.MYD: 數據文件 tbl_name.MYI: 索引文件
2>.InnoDB引擎 安全
InnoDB引擎特色 行級鎖 支持事務,適合處理大量短時間事務 讀寫阻塞與事務隔離級別相關 可緩存數據和索引 支持聚簇索引 崩潰恢復性更好 支持MVCC高併發 從MySQL5.5後支持全文索引 從MySQL5.5.5開始爲默認的數據庫引擎 InnoDB數據庫文件 全部InnoDB表的數據和索引放置於同一個表空間中 表空間文件:datadir定義的目錄下 數據文件:ibddata1, ibddata2, ... 每一個表單獨使用一個表空間存儲表的數據和索引 啓用:innodb_file_per_table=ON InnoDB系統變量連接:https://mariadb.com/kb/en/library/xtradbinnodb-server-system-variables/#innodb_file_per_tableON (>= MariaDB 5.5) 兩類文件放在數據庫獨立目錄中 數據文件(存儲數據和索引):tb_name.ibd 表格式定義:tb_name.frm
3>.Performance_Schema服務器
Performance_Schema數據庫使用。
4>.Memory併發
將全部數據存儲在RAM中,以便在須要快速查找參考和其餘相似數據的環境中進行快速訪問。適用存放臨時數據。該引擎之前被稱爲HEAP引擎
5>.MRG_MyISAMoracle
使MySQL DBA或開發人員可以對一系列相同的MyISAM表進行邏輯分組,並將它們做爲一個對象引用。適用於VLDB(Very Large Data Base)環境,如數據倉庫
6>.Archive
爲存儲和檢索大量不多參考的存檔或安全審覈信息,只支持SELECT和INSERT操做;支持行級鎖和專用緩存區
7>.Federated聯合
用於訪問其它遠程MySQL服務器一個代理,它經過建立一個到遠程MySQL服務器的客戶端鏈接,並將查詢傳輸到遠程服務器執行,然後完成數據存取,提供連接單獨MySQL服務器的能力,以便從多個物理服務器建立一個邏輯數據庫。很是適合分佈式或數據集市環境
8>.BDB
可替代InnoDB的事務引擎,支持COMMIT、ROLLBACK和其餘事務特性
9>.Cluster/NDB
MySQL的簇式數據庫引擎,尤爲適合於具備高性能查找要求的應用程序,這類查找需求還要求具備最高的正常工做時間和可用性
10>.CSV
CSV存儲引擎使用逗號分隔值格式將數據存儲在文本文件中。能夠使用CSV引擎以CSV格式導入和導出其餘軟件和應用程序之間的數據交換
11>.BLACKHOLE
黑洞存儲引擎接受但不存儲數據,檢索老是返回一個空集。該功能可用於分佈式數據庫設計,數據自動複製,但不是本地存儲
12>.example
「stub」引擎,它什麼都不作。能夠使用此引擎建立表,但不能將數據存儲在其中或從中檢索。目的是做爲例子來講明如何開始編寫新的存儲引擎
13>.MariaDB支持的其它存儲引擎
OQGraph
SphinxSE
TokuDB
Cassandra
CONNECT
SQUENCE
三.管理存儲引擎
1>.查看mysql支持的存儲引擎
MariaDB [yinzhengjie]> SHOW ENGINES; +--------------------+---------+----------------------------------------------------------------------------------+--------------+------+-------- ----+| Engine | Support | Comment | Transactions | XA | Savepoi nts |+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+-------- ----+| CSV | YES | Stores tables as CSV files | NO | NO | NO || MRG_MyISAM | YES | Collection of identical MyISAM tables | NO | NO | NO || MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO || SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES || PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO || MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO || Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO || InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+-------- ----+8 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW ENGINES\G *************************** 1. row *************************** Engine: CSV Support: YES Comment: Stores tables as CSV files Transactions: NO XA: NO Savepoints: NO *************************** 2. row *************************** Engine: MRG_MyISAM Support: YES Comment: Collection of identical MyISAM tables Transactions: NO XA: NO Savepoints: NO *************************** 3. row *************************** Engine: MyISAM Support: YES Comment: Non-transactional engine with good performance and small data footprint Transactions: NO XA: NO Savepoints: NO *************************** 4. row *************************** Engine: SEQUENCE Support: YES Comment: Generated tables filled with sequential values Transactions: YES XA: NO Savepoints: YES *************************** 5. row *************************** Engine: PERFORMANCE_SCHEMA Support: YES Comment: Performance Schema Transactions: NO XA: NO Savepoints: NO *************************** 6. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables Transactions: NO XA: NO Savepoints: NO *************************** 7. row *************************** Engine: Aria Support: YES Comment: Crash-safe tables with MyISAM heritage Transactions: NO XA: NO Savepoints: NO *************************** 8. row *************************** Engine: InnoDB Support: DEFAULT Comment: Supports transactions, row-level locking, foreign keys and encryption for tables Transactions: YES XA: YES Savepoints: YES 8 rows in set (0.00 sec) MariaDB [yinzhengjie]>
2>.查看當前默認的存儲引擎
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE '%STORAGE_ENGINE%'; +----------------------------+--------+ | Variable_name | Value | +----------------------------+--------+ | default_storage_engine | InnoDB | | default_tmp_storage_engine | | | enforce_storage_engine | | | storage_engine | InnoDB | +----------------------------+--------+ 4 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
3>.設置默認的存儲引擎
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf [mysqld] character-set-server = utf8mb4 default_storage_engine = InnoDB #指定默認的存儲引擎爲InnoDB port = 3306 datadir = /mysql/3306/data socket = /mysql/3306/socket/mysql.sock [mysqld_safe] log-error = /mysql/3306/log/mariadb.log pid-file = /mysql/3306/pid/mariadb.pid [root@node105.yinzhengjie.org.cn ~]# [root@node105.yinzhengjie.org.cn ~]#
4>.查看庫中全部表使用的存儲引擎
MariaDB [yinzhengjie]> SHOW TABLE STATUS FROM yinzhengjie; +----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+| classes | InnoDB | 10 | Dynamic | 8 | 2048 | 16384 | 0 | 0 | 0 | 9 | 2019-10-28 22:10:33 | NULL | NULL | utf8_general_ci | NULL | | || coc | InnoDB | 10 | Dynamic | 14 | 1170 | 16384 | 0 | 0 | 0 | 15 | 2019-10-28 22:10:33 | NULL | NULL | utf8_general_ci | NULL | | || courses | InnoDB | 10 | Dynamic | 7 | 2340 | 16384 | 0 | 0 | 0 | 8 | 2019-10-28 22:10:33 | NULL | NULL | utf8_general_ci | NULL | | || scores | InnoDB | 10 | Dynamic | 15 | 1092 | 16384 | 0 | 0 | 0 | 16 | 2019-10-28 22:10:33 | NULL | NULL | utf8_general_ci | NULL | | || students | InnoDB | 10 | Dynamic | 25 | 655 | 16384 | 0 | 0 | 0 | 26 | 2019-10-28 22:10:33 | 2019-10-29 07:58:57 | NULL | utf8_general_ci | NULL | | || teachers | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 0 | 5 | 2019-10-28 22:10:33 | NULL | NULL | utf8_general_ci | NULL | | |+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+6 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TABLE STATUS FROM yinzhengjie\G *************************** 1. row *************************** Name: classes Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 8 Avg_row_length: 2048 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 9 Create_time: 2019-10-28 22:10:33 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: *************************** 2. row *************************** Name: coc Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 14 Avg_row_length: 1170 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 15 Create_time: 2019-10-28 22:10:33 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: *************************** 3. row *************************** Name: courses Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 7 Avg_row_length: 2340 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 8 Create_time: 2019-10-28 22:10:33 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: *************************** 4. row *************************** Name: scores Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 15 Avg_row_length: 1092 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 16 Create_time: 2019-10-28 22:10:33 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: *************************** 5. row *************************** Name: students Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 25 Avg_row_length: 655 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 26 Create_time: 2019-10-28 22:10:33 Update_time: 2019-10-29 07:58:57 Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: *************************** 6. row *************************** Name: teachers Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 4 Avg_row_length: 4096 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 5 Create_time: 2019-10-28 22:10:33 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: 6 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>
5>.查看庫中指定表的存儲引擎
MariaDB [yinzhengjie]> SHOW TABLE STATUS LIKE 'students'; +----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+| students | InnoDB | 10 | Dynamic | 25 | 655 | 16384 | 0 | 0 | 0 | 26 | 2019-10-28 22:10:33 | 2019-10-29 07:58:57 | NULL | utf8_general_ci | NULL | | |+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+- --------------------+---------------------+------------+-----------------+----------+----------------+---------+1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TABLE STATUS LIKE 'students'\G *************************** 1. row *************************** Name: students Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 25 Avg_row_length: 655 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 26 Create_time: 2019-10-28 22:10:33 Update_time: 2019-10-29 07:58:57 Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW CREATE TABLE students; +----------+------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+----------+------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| students | CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Age` tinyint(3) unsigned NOT NULL, `Gender` enum('F','M') NOT NULL, `ClassID` tinyint(3) unsigned DEFAULT NULL, `TeacherID` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`StuID`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 | +----------+------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TABLES; +-----------------------+ | Tables_in_yinzhengjie | +-----------------------+ | classes | | coc | | courses | | scores | | students | | teachers | +-----------------------+ 6 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]> SHOW CREATE TABLE students\G *************************** 1. row *************************** Table: students Create Table: CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Age` tinyint(3) unsigned NOT NULL, `Gender` enum('F','M') NOT NULL, `ClassID` tinyint(3) unsigned DEFAULT NULL, `TeacherID` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`StuID`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) MariaDB [yinzhengjie]>
6>.設置表的存儲引擎
建立表時指定存儲引擎,若不指定則使用默認的存儲引擎: CREATE TABLE tb_name(... ) ENGINE=InnoDB; 直接修改表的存儲引擎: ALTER TABLE tb_name ENGINE=InnoDB;