MySQL is one of the most widely used open-source relational database management systems (RDBMS) in the world. It powers everything from small applications to massive platforms such as Facebook, YouTube (historically), and many SaaS systems.
This article provides a complete, in-depth explanation of MySQL, including:
- Architecture
- Internal components
- Storage engines
- SQL execution flow
- Transactions & ACID
- Indexes
- Replication
- Security
- Performance optimization
- Real SQL examples
1. What Is MySQL?
MySQL is a relational database management system that:
- Stores data in tables (rows & columns)
- Uses SQL (Structured Query Language)
- Is multi-user, multi-threaded
- Follows a client–server architecture
- Is maintained by Oracle
2. MySQL Client–Server Architecture
MySQL follows a client–server model.
Client (App / CLI / GUI)
|
v
MySQL Server
|
v
Storage Engine
|
v
Disk / Memory
Clients
- MySQL CLI
- Backend applications (Node.js, Django, Java, PHP)
- GUI tools (MySQL Workbench, DBeaver)
Server
- Parses SQL
- Optimizes queries
- Manages transactions
- Communicates with storage engines
3. High-Level MySQL Architecture
MySQL architecture has three main layers:
1️⃣ Client Layer
2️⃣ SQL Layer
3️⃣ Storage Engine Layer
4. SQL Layer (Core of MySQL)
This layer is engine-independent and handles SQL logic.
Components of SQL Layer
4.1 Connection Manager
- Manages client connections
- Thread per connection
- Authentication & authorization
SHOW PROCESSLIST;
4.2 Query Cache (Deprecated)
- Cached SELECT results
- Removed in MySQL 8.0 due to scalability issues
4.3 SQL Parser
- Checks syntax
- Converts SQL into a parse tree
Example:
SELECT name FROM users WHERE id = 1;
4.4 Preprocessor
- Checks table & column existence
- Validates permissions
4.5 Query Optimizer
- Chooses best execution plan
- Uses statistics and indexes
Example:
EXPLAIN SELECT * FROM users WHERE email = 'a@test.com';
4.6 Query Executor
- Executes optimized plan
- Calls storage engine APIs
5. Storage Engine Layer
Storage engines define how data is stored, indexed, and retrieved.
Storage Engine Interface
MySQL uses a pluggable engine architecture.
SHOW ENGINES;
6. InnoDB (Default Engine)
Why InnoDB?
- ACID compliant
- Supports transactions
- Row-level locking
- Foreign keys
6.1 InnoDB Internal Architecture
Components:
- Buffer Pool
- Redo Log
- Undo Log
- Tablespaces
6.2 Buffer Pool
- Memory cache for data & indexes
- Critical for performance
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
6.3 Redo Log
- Ensures durability
- Crash recovery
6.4 Undo Log
- Used for rollback
- Enables MVCC
7. Transactions and ACID
ACID Properties
| Property | Meaning |
|---|---|
| Atomicity | All or nothing |
| Consistency | Valid state |
| Isolation | Concurrent safety |
| Durability | Persistent |
Transaction Example
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Rollback:
ROLLBACK;
8. Isolation Levels
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
| Level | Issues |
|---|---|
| READ UNCOMMITTED | Dirty reads |
| READ COMMITTED | Non-repeatable reads |
| REPEATABLE READ (default) | Phantom reads prevented |
| SERIALIZABLE | Full isolation |
9. Indexes in MySQL
Indexes improve read performance.
Types of Indexes
- PRIMARY KEY
- UNIQUE
- INDEX
- FULLTEXT
- SPATIAL
Create Index
CREATE INDEX idx_email ON users(email);
B-Tree Index Structure
- Balanced tree
- Logarithmic lookup time
Composite Index
CREATE INDEX idx_name_age ON users(name, age);
Order matters.
10. Constraints
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(255) UNIQUE,
age INT CHECK (age >= 18)
);
11. Foreign Keys
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(id)
);
12. MySQL Data Types
Numeric
- INT
- BIGINT
- DECIMAL
- FLOAT
String
- CHAR
- VARCHAR
- TEXT
- JSON
Date/Time
- DATE
- DATETIME
- TIMESTAMP
13. Views
CREATE VIEW active_users AS
SELECT id, name FROM users WHERE active = 1;
14. Stored Procedures
DELIMITER $$
CREATE PROCEDURE GetUsers()
BEGIN
SELECT * FROM users;
END$$
DELIMITER ;
15. Triggers
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
SET NEW.created_at = NOW();
16. Functions
CREATE FUNCTION square(x INT)
RETURNS INT
RETURN x * x;
17. Replication
Types
- Source–Replica
- Asynchronous
- Semi-synchronous
Used for:
- Read scaling
- Backup
- High availability
18. MySQL Security
Authentication
- Users & privileges
CREATE USER 'app'@'%' IDENTIFIED BY 'password';
GRANT SELECT, INSERT ON db.* TO 'app'@'%';
Encryption
- SSL/TLS
- Data at rest encryption (InnoDB)
19. Performance Optimization
Best Practices
- Proper indexing
- Avoid SELECT *
- Use EXPLAIN
- Tune buffer pool
EXPLAIN ANALYZE SELECT * FROM users WHERE id = 1;
20. MySQL Backup & Recovery
Logical Backup
mysqldump db > backup.sql
Restore
mysql db < backup.sql
21. MySQL vs PostgreSQL (Quick)
| Feature | MySQL | PostgreSQL |
|---|---|---|
| Speed | Fast reads | Complex queries |
| JSON | Good | Excellent |
| Extensions | Limited | Powerful |
22. When to Use MySQL?
Use MySQL if:
- You need high performance
- Simple relational data
- Web applications
- Large ecosystem support
Conclusion
MySQL is not just a simple database—it is a deep, highly optimized, production-grade system with:
- A layered architecture
- Sophisticated internals
- Pluggable storage engines
- Full transactional support
- Proven scalability
Understanding its architecture and internals allows you to write faster queries, safer systems, and scalable applications.
Top comments (0)