Introduction to MySQL
MySQL is the world’s most popular open-source relational database management system (RDBMS). It stores data in tables with rows and columns, queried with SQL (Structured Query Language), and powers everything from WordPress blogs to billion-row analytics pipelines.
What is MySQL?
MySQL is a client-server database:
Application → mysql driver → MySQL Server → Data files on disk
Your application sends SQL statements; the server parses, optimizes, executes them, and returns result sets. Multiple applications can connect concurrently — MySQL handles locking and transaction isolation.
Key Features
- Open source — Community Edition is free under GPL; Enterprise Edition adds Oracle support and advanced tooling
- High performance — optimized B-tree indexes, query cache (removed in 8.0), InnoDB buffer pool
- ACID transactions — InnoDB engine provides atomicity, consistency, isolation, durability
- Replication — asynchronous and semi-sync replication for read scaling and disaster recovery
- Cross-platform — Linux, Windows, macOS, Docker, and every major cloud (RDS, Cloud SQL, Azure Database)
- Wide ecosystem — drivers for PHP, Python, Java, Node.js, Go; ORMs like Sequelize, SQLAlchemy, Hibernate
MySQL vs MariaDB
MariaDB is a community fork created by MySQL’s original developers after Oracle acquired Sun. Both share compatible SQL syntax and client protocols.
| Aspect | MySQL 8 | MariaDB |
|---|---|---|
| Owner | Oracle | MariaDB Foundation |
| JSON support | Native JSON type | Native JSON type |
| Default auth | caching_sha2_password | mysql_native_password (varies) |
| Extra features | HeatWave analytics | Galera clustering, more engines |
Choose based on your cloud provider, hosting panel, and team familiarity. Migrating between them is straightforward for typical web apps.
Storage Engines
| Engine | Use Case |
|---|---|
| InnoDB | Default — transactions, foreign keys, row-level locking |
| MyISAM | Legacy — full-text search (largely replaced by InnoDB FTS) |
| MEMORY | Temporary in-memory tables |
Always use InnoDB for production applications.
Your First Database Session
# Install (macOS)
brew install mysql
brew services start mysql
# Connect
mysql -u root -p
-- Create a database and table
CREATE DATABASE shop;
USE shop;
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO products (name, price) VALUES
('Widget', 19.99),
('Gadget', 29.99);
SELECT * FROM products;
-- +----+--------+-------+---------------------+
-- | id | name | price | created_at |
-- +----+--------+-------+---------------------+
-- | 1 | Widget | 19.99 | 2024-06-13 10:00:00 |
-- | 2 | Gadget | 29.99 | 2024-06-13 10:00:00 |
-- +----+--------+-------+---------------------+
Common Use Cases
| Use Case | Why MySQL |
|---|---|
| Web applications | Default for LAMP/LEMP stacks; PHP, WordPress, Laravel |
| Content management | WordPress, Drupal, Joomla store posts and users in MySQL |
| E-commerce | Product catalogs, orders, inventory with transactional integrity |
| SaaS backends | Multi-tenant relational data with row-level security patterns |
| Reporting | Export to BI tools; combine with read replicas for analytics |
MySQL in Application Code
Node.js with mysql2:
const mysql = require('mysql2/promise');
async function getProducts() {
const conn = await mysql.createConnection({
host: 'localhost',
user: 'app',
password: 'secret',
database: 'shop'
});
const [rows] = await conn.execute('SELECT id, name, price FROM products');
await conn.end();
return rows;
}
Python with SQLAlchemy:
from sqlalchemy import create_engine, text
engine = create_engine("mysql+pymysql://app:secret@localhost/shop")
with engine.connect() as conn:
result = conn.execute(text("SELECT id, name, price FROM products"))
for row in result:
print(row)
Editions
- MySQL Community Server — free, full-featured, GPL licensed
- MySQL Enterprise Edition — monitoring (Enterprise Monitor), backup, firewall, Oracle premium support
For learning and most startups, Community Edition is sufficient.
Architecture Overview
┌─────────────┐ ┌──────────────────────────────┐
│ Clients │────▶│ Connection / Thread layer │
└─────────────┘ ├──────────────────────────────┤
│ SQL parser & optimizer │
├──────────────────────────────┤
│ InnoDB storage engine │
│ (buffer pool, redo log, data) │
└──────────────────────────────┘
Understanding this stack helps when tuning slow queries and configuring buffer pool size.
Security Basics
- Create application users with least privilege — not
root - Use prepared statements to prevent SQL injection
- Enable SSL/TLS for connections over untrusted networks
- Keep MySQL patched; 8.0 is the current major line
CREATE USER 'app'@'localhost' IDENTIFIED BY 'strong_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'localhost';
FLUSH PRIVILEGES;
What Comes Next
This track progresses from SQL basics through data types, CRUD, joins, indexing, transactions, stored procedures, replication, backup, security, and performance tuning. MySQL skills transfer directly to MariaDB, PostgreSQL syntax (with differences), and cloud-managed database services.