1. 程式人生 > 其它 >MySQL事務以及儲存引擎

MySQL事務以及儲存引擎

MySQL事務以及儲存引擎

目錄

一、事務

1. 事務的概念

● 事務是一種機制、一個操作序列,包含了一組資料庫操作命令,並且把所有的命令作為一個整體一起向系統提交或撤銷操作請求,即這一組資料庫命令要麼都執行,要麼都不執行。
● 事務是一個不可分割的工作邏輯單元,在資料庫系統上執行併發操作時,事務是最小的控制單元。
● 事務適用於多使用者同時操作的資料庫系統的場景,如銀行、保險公司及證券交易系統等等。
● 事務通過事務的整體性以保證資料的一致性。
● 事務能夠提高在向表中更新和插入資訊期間的可靠性。
總結來說,事務是一個操作序列,這些操作要麼都執行,要麼都不執行,它是一個不可分割的工作單位。

2. 事務的ACID特點

ACID是指在可靠資料庫管理系統(DBMS)中,事務(transaction)應該具有的四個特性:原子性(Atomicity)、一致性(Consistency)、隔離性(Isolation)、永續性(Durability),這是可靠資料庫所應具備的幾個特性。
在事務管理中,原子性是基礎,隔離性是手段,一致性是目的,永續性是結果、

(1)原子性

原子性:指事務是一個不可再分割的工作單位,事務中的操作要麼都發生,要麼都不發生。
事務是一個完整的操作,事務的各元素是不可分的。
事務中的所有元素必須作為一個整體提交或回滾。
如果事務中的任何元素失敗,則整個事務將失敗。

案例:
A給B轉賬100元錢的時候,只執行了扣款語句,就提交了,此時如果突然斷電,A賬號已經發生了扣款,B賬號卻沒收到加款,在生活中就會引起糾紛。這種情況就需要事務的原子性來保證事務要麼都執行,要麼就都不執行。

(2)一致性

一致性:指在事務開始之前和事務結束以後,資料庫的完整性約束沒有被破壞。
當事務完成時,資料必須處於一致狀態。
當事務開始前,資料庫中儲存的資料處於一致狀態。
當正在進行的事務中,資料可能處於不一致的狀態。
當事務成功完成時,資料必須再次回到已知的一致狀態。

案例:
對銀行轉賬事務,不管事務成功還是失敗,應該保證事務結束後表中A和B的存款總額跟事務執行前一致。

(3)隔離性

隔離性:指在併發環境中,當不同的事務同時操縱相同的資料時,每個事務都有各自的完整資料空間。
對資料進行修改的所有併發事務是彼此隔離的,表明事務必須是獨立的,它不應以任何方式依賴於或影響其他事務。
修改資料的事務可在另一個使用相同資料的事務開始之前訪問這些資料,或者在另一個使用相同資料的事務結束之後訪問這些資料。

①事務之間的相互影響

事務之間的相互影響分為以下幾種

  1. 髒讀:一個事務讀取了另一個事務未提交的資料,而這個資料是有可能回滾的。
  2. 不可重複讀:一個事務內兩個相同的查詢卻返回了不同資料,這是由於查詢時系統中其他事務修改的提交而引起的。
  3. 幻讀:一個事務對一個表中的資料進行了修改,這種修改涉及到表中的全部資料行。同時,另一個事務也修改這個表中的資料,這種修改是向表中插入一行新資料。那麼,操作前一個事務的使用者會發現表中還有沒有修改的資料行,就好像發生了幻覺一樣。
  4. 丟失更新:兩個事務同時讀取同一條記錄,A先修改記錄,B也修改記錄(B不知道A修改過),B提交資料後B的修改結果覆蓋了A的修改結果。
②MySQL事務支援的四種隔離
  1. 未提交讀(Read Uncommitted)
    允許髒讀,其他事務只要修改了資料,即使未提交,本事務也能看到修改後的資料值。也就是可能讀取到其他會話中未提交事務修改的資料。
  2. 提交讀(Read Committed)
    只能讀取到已經提交的資料。Oracle等多數資料庫預設都是該級別(不重複讀)。
  3. 可重複讀(Repeated Read)
    可重複度。無論其他事務是否修改並提交了資料,在這個事務中看到的資料值始終不受其他事務影響。MySQL預設使用該隔離級別。
  4. 序列讀(Serializable)
    完全序列化的讀,每次讀都需要獲得表級共享鎖,讀寫相互都會堵塞,相當於鎖表。
③主流資料庫的預設隔離級別
主流資料庫 預設隔離級別
MySQL repeatable read可重複讀
Oracle read committed提交讀
SQL Server read committed提交讀
④查詢全域性事務隔離級別

show global variables like '%isolation%';
select @@global.tx_isolaion;

⑤查詢會話事務隔離級別

show session variables like '%isolation%';
select @@session,tx_isolation;
select @@tx_isolation;

⑥設定全域性事務隔離級別

set global transaction isolation level read committed;

⑦設定會話事務隔離級別

set session transaction isolation level read committed;

(4)永續性

永續性:在事務完成以後,該事務對資料庫所做的更改便持久的儲存在資料庫之中,並不會被回滾。
不管系統是否發生故障,事務處理的結果都是永久的。
一旦事務被提交,事務的效果會被永久的保留在資料庫中。

3. 事務控制語句

begin 或 start transaction:顯式的開啟一個事務
commit 或 commit work:提交事務,並使已對資料庫進行的所有修改變為永久性。
rollback 或 rollback work:回滾會結束使用者的事務,並撤銷正在進行的所有未提交的資料。
savepoint S1:使用savepoint允許在事務中建立一個回滾點,一個事務中可以有多個savepoint,"S1"代表回滾點名稱。savepoint的作用類似於遊戲中的存檔。
rollback to [savepoint] S1:把事務回滾到標記點。類似於遊戲中的讀取存檔。

(1)案例

mysql> create table account (
    -> id int(10) primary key not null,
    -> name varchar(40),
    -> money double
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc account;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(10)     | NO   | PRI | NULL    |       |
| name  | varchar(40) | YES  |     | NULL    |       |
| money | double      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into account values(1,'A',1000);
Query OK, 1 row affected (0.00 sec)

mysql> insert into account values(2,'B',1000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

(2)測試提交事務

①不提交測試
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update account set money=money-100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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> 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
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)
②提交測試
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update account set money=money-100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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 * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

(3)測試回滾事務

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update test.account set money=money+100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

(4)測試多點回滾

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> update test.account set money=money+100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> savepoint s1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> update test.account set money=money+100 where name='B';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> savepoint s2;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> insert into test.account values(3,'C',1000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1100 |
|  3 | C    |  1000 |
+----+------+-------+
3 rows in set (0.00 sec)

mysql> rollback to s1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |  1000 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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 * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

4. 使用set設定控制事務

set autocommit=0; #禁止自動提交
set autocommit=1; #開啟自動提交,MySQL預設為1
show variables like 'autocommit'; #檢視MySQL中的autocommit值

如果沒有開啟自動提交,當前會話連線的MySQL的所有操作都會當成一個事務直到你輸入rollback或commit,當前事務才算結束。當前書屋結束前新的MySQL連線時,無法讀取到任何當前會話的操作結果。
如果開啟了自動提交,MySQL會把每個sql語句當成一個事務,然後自動的commit。
當然,無論開啟與否,begin;commit|rollback;都是獨立的事務。

(1)autocommit=0;

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> update test.account set money=money+100 where name='B';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> quit;
Bye
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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 * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

(2)autocommit=1

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> set autocommit=1;
Query OK, 0 rows affected (0.00 sec)

mysql> update test.account set money=money+100 where name='B';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> quit;
Bye
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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 * from test.account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | A    |   900 |
|  2 | B    |  1100 |
+----+------+-------+
2 rows in set (0.00 sec)

(3)檢視autocommit

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

二、MySQL儲存引擎

1. 儲存引擎的概率

● MySQL中的資料用各種不同的技術儲存在檔案中,每一種技術都是用不同的儲存機制、索引技巧、鎖定水平並最終提供不同的功能和能力,這些不同的技術以及配套的功能在MySQL中稱為儲存引擎。
● 儲存引擎是MySQL將資料儲存在檔案系統中的儲存方式或者儲存格式。
● MySQL常用的儲存引擎為:MyISAM/InnoDB。
● MySQL資料庫中的元件,負責執行實際的資料I/O操作。
● MySQL系統中,儲存引擎處於檔案系統之上,在資料儲存到資料檔案之前會傳輸到儲存引擎,之後按照各個儲存引擎的儲存格式進行儲存。

2. MyISAM

(1)MyISAM的特點

● MyISAM不支援事務,也不支援外來鍵約束,只支援全文索引,資料檔案和索引檔案是分開儲存的。
● 訪問速度快,對事務完整性沒有要求
● MyISAM適合查詢、插入為主的應用
● MyISAM在磁碟上儲存成三個檔案,檔名和表名都相同,但是副檔名分別為:
● .frm檔案儲存表結構定義
● .MYD(MYData)為資料檔案的副檔名
● .MYI(MYIndex)為索引檔案的副檔名
● 表級鎖定形式,資料在更新時鎖定整個表
● 資料庫在讀寫過程中相互阻塞
● 會在資料寫入的過程中阻塞使用者資料的讀取
● 也會在資料讀取的過程中阻塞使用者的資料寫入
● 資料單獨寫入或讀取,速度過程較快且佔用資源相對少
● MyISAM支援的儲存格式
● 靜態表
● 動態表
● 壓縮表

(2)MyISAM支援的儲存格式

①靜態(固定長度)表

靜態表是預設的儲存格式。靜態表中的欄位都是非可變欄位,這樣每個記錄都是固定長度,這種儲存方式的優點是儲存非常迅速,容易快取,出現故障容易恢復;缺點是佔用的空間通常比動態表多。

②動態表

動態表包含可變欄位,記錄不是固定長度的,這樣儲存的優點是佔用空間較少,但是頻繁的更新、刪除記錄會產生碎片,需要定期執行optimize table語句或myisamchk -r命令來改善效能,並且出現故障的時候恢復相對比較困難。

③壓縮表

壓縮表由myisamchk工具建立,佔據非常小的空間,因為每條記錄都是被單獨壓縮的,所以只有非常小的訪問開支。

(3)MyISAM適用的生產場景分析

● 公司業務不需要事務的支援
● 單方面讀取或寫入資料比較多的業務
● MyISAM儲存引擎資料讀寫都比較頻繁的場景不適合
● 適用讀寫併發訪問相對較低的業務
● 資料修改相對較少的業務
● 對資料業務一致性要求不是非常高的業務
● 伺服器硬體資源相對比較差

3. InnoDB

(1)InnoDB特點介紹

● 支援事務,支援4個事務隔離級別
● MySQL從5.5.5版本開始,預設的儲存引擎為InnoDB
● 讀寫阻塞與事務隔離級別相關
● 能非常高效的快取索引和資料
● 表與主鍵以簇的方式儲存
● 支援分割槽、表空間,類似於Oracle資料庫
● 支援外來鍵約束,5.5前不支援全文索引,5.5後支援全文索引
● 對硬體資源要求還是比較高的場合
● 行級鎖定,但是全表掃描仍然會是表級鎖定,如update table set a=1 where user like '%zhang%';
● InnoDB中不儲存表額行數,如select count(*) from table;時,InnoDB需要掃描一遍整個表來計算有多少行,但是MyISAM只要簡單的讀出儲存好的行數即可。需要注意的是,當count(*)語句包含where條件時MyISAM也需要掃描整個表
● 對於自增長的欄位,InnoDB中必須包含只有該欄位的索引,但是在MyISAM表中可以和其他欄位一起建立組合索引
● 清空整個表時,InnoDB是一行一行的刪除,效率非常慢。MyISAM則會重建表。

(2)InnoDB適用生產場景分析

● 業務需要食物的支援
● 行級鎖定對高併發有很好的適應能力,但需確保查詢是通過索引來完成
● 業務資料更新較為頻繁的場景,如論壇、微博等
● 業務資料一致性要求較高,如銀行業務
● 硬體裝置記憶體較大,利用InnoDB較好的快取能力來提高記憶體利用率,減少磁碟IO的壓力

4. 企業選擇儲存引擎的依據

需要考慮每個儲存引擎提供了哪些不同的核心功能及應用場景
● 支援的欄位和資料型別
● 所有引擎都支援通用的資料型別
● 但不是所有的引擎都支援其他的欄位型別,如二進位制物件
● 鎖定型別:不同儲存引擎支援不同級別的鎖定
● 表鎖定:MyISAM支援
● 行鎖定:InnoDB支援
● 索引的支援
● 建立索引在搜尋和恢復資料庫中的資料時能顯著提高效能
● 不同的儲存引擎提供不同的製作索引的技術
● 有些儲存引擎根本不支援索引
● 事務處理的支援
● 提高在向表中更新和插入資訊期間的可靠性
● 可根據企業業務是否要支援事務選擇儲存引擎

5. 對於引擎的操作

(1)檢視系統支援的儲存引擎

show engines;

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

(2)查看錶使用的儲存引擎

方法一:
show table status from 庫名 where name='表名'\G

mysql> show table status from test where name='account'\G
*************************** 1. row ***************************
           Name: account
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 3
 Avg_row_length: 5461
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2021-08-30 12:20:16
    Update_time: 2021-08-30 12:55:30
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

方法二:
use 庫名;
show create table 表名;

mysql> 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
mysql> show create table account;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                  |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| account | CREATE TABLE "account" (
  "id" int(10) NOT NULL,
  "name" varchar(40) DEFAULT NULL,
  "money" double DEFAULT NULL,
  PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

(3)修改儲存引擎

①通過alter table修改

use 庫名;
alter table 表名 engine=指定引擎;

mysql> use test;
Database changed
mysql> alter table account engine=MyISAM;
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> show table status from test where name='account'\G
*************************** 1. row ***************************
           Name: account
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 2
 Avg_row_length: 20
    Data_length: 40
Max_data_length: 281474976710655
   Index_length: 2048
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2021-08-30 13:46:48
    Update_time: 2021-08-30 13:46:48
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)
②通過修改/etc/my.cnf配置檔案,指定預設儲存引擎並重啟服務
[root@localhost ~]# vim /etc/my.cnf

......
[mysqld]
......
default-storage-engine=INNODB

[root@localhost ~]# systemctl restart mysqld.service 
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, 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> 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
mysql> create table engine_test(id int,engine_name varchar(30));
Query OK, 0 rows affected (0.00 sec)

mysql> show create table engine_test;
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                            |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| engine_test | CREATE TABLE "engine_test" (
  "id" int(11) DEFAULT NULL,
  "engine_name" varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

注:此方法只對修改了配置檔案並重啟mysql服務後新建立的表有效,已經存在的表不會有變更。

③通過create table建立表時指定儲存引擎

use 庫名;
create table 表名(欄位1 資料型別[,...]) engine=MyISAM;

mysql> create table engine_test(id int,engine_name varchar(30)) engine=MyISAM; 
Query OK, 0 rows affected (0.00 sec)

mysql> show create table engine_test;                                          
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                            |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| engine_test | CREATE TABLE "engine_test" (
  "id" int(11) DEFAULT NULL,
  "engine_name" varchar(30) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)