MySQL 5.5 之前默認存儲引擎為 MyISAM,5.5 及以後版本默認已改為 InnoDB。若您的實例仍為 MyISAM,或因升級/遷移導致默認引擎異常,需手動調整。InnoDB 支持事務、行級鎖、外鍵約束、崩潰恢復,是生產環境首選。
| 特性 | MyISAM | InnoDB |
|---|---|---|
| 事務支持 | ❌ | ✅ ACID |
| 鎖粒度 | 表鎖 | 行鎖 |
| 外鍵 | ❌ | ✅ |
| 崩潰恢復 | ❌ | ✅ Redo/Undo Log |
| 全文索引 | ✅ (早期) | ✅ (5.6+) |
| 查詢性能 | 單純讀略快 | 綜合場景優 |
mysql> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| ... | ... | ... | ... | ... | ... |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
若 InnoDB | DEFAULT 已顯示,則無需修改。
編輯 my.cnf (Linux) 或 my.ini (Windows),在 [mysqld] 區段添加:
[mysqld]
default-storage-engine=InnoDB
default-tmp-storage-engine=InnoDB
# Linux
systemctl restart mysqld
# 或
service mysqld restart
# Windows (管理員 cmd)
net stop mysql
net start mysql
mysql> SHOW ENGINES;
...
| InnoDB | DEFAULT | ... |
...
mysql> SHOW VARIABLES LIKE 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
修改默認引擎僅影響後續建表,現有 MyISAM 表需手動轉換:
# 單表轉換
ALTER TABLE table_name ENGINE=InnoDB;
# 批量生成轉換腳本
SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' ENGINE=InnoDB;')
FROM information_schema.TABLES
WHERE ENGINE='MyISAM' AND TABLE_SCHEMA NOT IN ('mysql','information_schema','performance_schema','sys');
注意:大表轉換會鎖表重建,安排維護窗口操作,或使用 pt-online-schema-change 在線轉換。
修改默認存儲引擎為 InnoDB 僅需三步:確認支持 → 修改配置 → 重啟驗證。生產環境務必同步處理存量表轉換,並配合 innodb_buffer_pool_size、innodb_log_file_size 等參數調優,充分發揮 InnoDB 優勢。