Storage engines serve as the underlying architecture managing data storage, retrieval, and transactional integrity in MySQL. Key capabilities include:
- Data read/write operations
- Ensuring data security and consistency
- Performance optimization
- Hot backup support
- Automatic crash recovery
- High availability features
Common Storage Engines
MySQL supports multiple storage engines:
SHOW ENGINES;
Third-Party Engines
RocksDB, MyRocks, TokuDB
- Higher compression ratios
- Superior insert performance
- Comparable features to InnoDB
Frequently Used Engines
InnoDB, MyISAM, MEMORY, CSV
Default: InnoDB
Case Study: Zabbix Monitoring System Optimization
Original Environment
- Zabbix 3.2
- MariaDB 5.5 (default InnoDB)
- CentOS 7.3
Symptoms
- System latency
- Storage exhaustion every 3-4 months
- Single 500GB database file
Optimization Steps
- Upgrade MariaDB and Zabbix
- Migrate to TokuDB engine
- Partition monitoring data by month (custom development)
- Disable binary logging and
innodb_flush_log_at_trx_commit=1 - Adjust critical parameters
Results
- Stable monitoring operations
- TokuDB insert speed outperformed InnoDB
- Partitioning enabled efficient space reclamation via
TRUNCATE - Reduced I/O overhead from disabled binaries
Case Study: MyISAM to InnoDB Migration
Original Environment
- CentOS 5.8
- MySQL 5.0 (MyISAM)
- LNMP stack with 50GB data
Symptoms
- High concurrency lock contention
- Data loss after crashes
Analysis
- MyISAM's table-level locks caused bottlenecks
- Lack of transactional integrity led to data loss
Resolution
- Upgrade to MySQL 5.6.10
- Migrate all tables
- Enable doublewrite buffer (
innodb_doublewrite=ON)
InnoDB Engine Features
As MySQL's default engine since 5.5, InnoDB provides:
1. ACID-compliant transactions
2. Multi-Version Concurrency Control (MVCC)
3. Row-level locking
4. Automatic crash recovery
5. Hot backup support
6. Enhanced replication: Group Commit, GTID, multi-threaded SQL
Storage Engine Configuration
Check default engine:
SELECT @@default_storage_engine;
Modify permanently:
# /etc/my.cnf
[mysqld]
default_storage_engine=InnoDB
Verify table engines:
SHOW CREATE TABLE t1;
SHOW TABLE STATUS LIKE 't1'\G
SELECT table_schema, table_name, engine
FROM information_schema.tables
WHERE table_schema NOT IN ('sys','mysql','information_schema','performance_schema');
Altering Storage Engines
Convert a table:
ALTER TABLE t1 ENGINE=InnoDB;
Note: This operation rebuilds the table and reclaims fragmented space.
Fragmentation Management
Scenario:
- CentOS 7.4, MySQL 5.7.20, InnoDB
- Large datasets with monthly purge operations
- Unreleased disk space after deletes
Solutions:
- Partition tables by month
- Replace
DELETEwithTRUNCATE PARTITION - Schedule regular
OPTIMIZE TABLE
Batch conversion example (Zabbix):
SELECT CONCAT("ALTER TABLE ",table_schema,".",table_name," ENGINE=TOKUDB;")
FROM information_schema.tables
WHERE table_schema='zabbix';
InnoDB Physical Storage Structure
Core components in /var/lib/mysql:
ibdata1 # System dictionary, undo logs
ib_logfile* # Redo logs
tmp*.ibt # Temporary tablespace
*.frm # Table metadata (column definitions)
*.ibd # Row data and indexes (per-table)
Tablespace Evolution
- 5.5: Shared tablespace (
ibdata1) for all data - 5.6+: File-per-table (
innodb_file_per_table=ON) by default - 5.7: Temporary tablespace isolation (
ibtmp1) - 8.0: Undo logs separation
Configure shared tablespace during initialization:
innodb_data_file_path=ibdata1:512M;ibdata2:512M:autoextend
innodb_autoextend_increment=64
Verify file-per-table:
SELECT @@innodb_file_per_table;
Tablespace Recovery Case
Failure Scenario:
- IBM server, 500GB HDD (no RAID)
- CentOS 6.8, MySQL 5.6.33 (file-per-table)
- Power outage → filesystem corruption
- Confluence DB intact; Jira DB missing
Recovery Procedure:
- Create identical empty tables
- Discard tablespaces:
ALTER TABLE confulence.t1 DISCARD TABLESPACE; - Copy original
.ibdfiles and set permissions - Import tablespaces:
ALTER TABLE confulence.t1 IMPORT TABLESPACE;
Key Notes:
- Handle foreign keys with
SET foreign_key_checks=0 - Script generation via
INTO OUTFILE
Transaction Management
ACID Properties
- Atomicity: All operations succeed or fail as one unit
- Consistency: Valid state transitions
- Isolation: Concurrent transactions don't interfere
- Durability: Committed changes survive crashes
Transaction Control
BEGIN; -- Start transaction
UPDATE ... ; -- DML operations
COMMIT; -- Finalize changes
-- or
ROLLBACK; -- Abort changes
Autocommit settings:
SELECT @@autocommit; -- Default: 1 (ON)
SET SESSION autocommit=0; -- Disable for current session
Implicit Commit Triggers
DDL (CREATE/ALTER/DROP)
DCL (GRANT/REVOKE)
LOCK TABLES / UNLOCK TABLES
TRUNCATE TABLE
LOAD DATA INFILE
ACID Implementation Mechanics
Redo Logs (ib_logfile*)
- Record page changes in memory
- Enable Write-Ahead Logging (WAL)
- Ensure durability (
COMMITforces log writes) - Power failure recovery via redo replay (Crash-Safe Recovery)
Undo Logs (ibdata* or separate files)
- Enable rollbacks by storing pre-change data
- Support Multi-Version Concurrency Control (MVCC)
- Provide consistent reads across transactions
Key Concepts
- LSN (Log Sequence Number): Tracks page versions
- Dirty Page: Modified buffer pool page not yet flushed
- Checkpoint (CKPT): Flushing dirty pages to disk
- WAL Principle: Log writes precede data page updates
Locking and Isolation Levels
Lock Types
- Row Locks: Direct record-level locks
- Gap Locks: Prevent phantom inserts in ranges
- Next-Key Locks: Combination of row + gap locks
Isolation Levels
SET GLOBAL transaction_isolation='READ-COMMITTED';
| Level | Dirty Reads | Non-Repeatable Reads | Phantom Reads |
|---|---|---|---|
| READ UNCOMMITTED | ✓ | ✓ | ✓ |
| READ COMMITTED | ✗ | ✓ | ✓ |
| REPEATABLE READ | ✗ | ✗ | Controlled* |
| SERIALIZABLE | ✗ | ✗ | ✗ |
*Prevented via gap/next-key locks with indexed queries
Phantom Read Example
Sequence:
- Transaction A:
UPDATE t1 SET name='zhangsa' WHERE id>2(uncommitted) - Transaction B:
INSERT INTO t1 VALUES (11,'ls'); COMMIT; - Transaction A:
COMMIT; SELECT * FROM t1;→ Sees id=11 with 'ls'
Critical InnoDB Parameters
Engine Configuration
SHOW ENGINES;
SHOW VARIABLES LIKE 'default_storage_engine';
Tablespace Settings
Shared tablespace:
innodb_data_file_path=ibdata1:512M;ibdata2:512M:autoextend
File-per-table:
SHOW VARIABLES LIKE 'innodb_file_per_table'; -- Enable for new tables
Durability Controls (Doublewrite)
innodb_flush_log_at_trx_commit:
1: Full ACID (flush per commit)0: Flush logs every second2: Flush OS buffer per commit; disk every second
innodb_flush_method:
O_DIRECT: Bypass OS cache for data filesO_DSYNC: Bypass OS cache for log filesfsync: Default (OS cache used)
Recommended for durability:
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
Redo Log Tuning
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
# Flush when 75% of buffer dirty
innodb_max_dirty_pages_pct = 75