Home›Learn›MySQL 2: how it stores your data
MySQL · Chapter 2

How MySQL stores your data

What is running, what is in memory, and which file holds my table?

Here is the whole machine before we follow a write: the server and InnoDB threads, the buffer pool, the redo and undo logs, the binlog and the tablespace files.

The architecture: processes, memory, disk

Process, memory, disk: a request enters at the process layer, memory serves it when it can, and disk makes it permanent. Name the layer first, then diagnose inside it. The full rule is on the PostgreSQL page: name the layer first, then diagnose inside it.

Listen to the whole architecture, 4 min 48 s

The same layers as a still diagram, with every part named:

MySQL · InnoDB: architecture

Request path

Processesone mysqld · a thread per connection · page cleaner threads · purge thread · log writer threads
→
MemoryInnoDB buffer pool (most of the RAM budget) + redo log buffer + change buffer inside it
→
Disk: the datadir*.ibd per table · #innodb_redo/#ib_redoN · mysql-bin.* · undo_001/002 · ibdata1
Who does what: Connection threads run queries. Page cleaners flush dirty pages to the .ibd files. The purge thread deletes undo records no reader still needs. Threads share one process. A crash takes all of them. Redo rebuilds the state.

See the real thing: the official InnoDB Architecture diagram, Figure 17.1. One picture, in-memory structures on one side, on-disk structures on the other.

Files on disk · what ls actually shows

Same rule as PostgreSQL: there is no hidden state. Every file here is inspectable with ls and du while the server runs. The full version is on the PostgreSQL page.

MySQL: the datadir
$ ls /var/lib/mysql
ibdata1           # InnoDB system tablespace:
                  # internal bookkeeping
#innodb_redo/    # THE REDO LOG folder (8.0.30+;
                  # before that: ib_logfile0/1 here)
  #ib_redo0       # ┐ 32 files, #ib_redo0 to #ib_redo31,
  #ib_redo31      # ┘ each 1/32 of innodb_redo_log_capacity
mysql-bin.000001  # ┐ THE BINARY LOG: every change,
mysql-bin.000002  # │ numbered, for replicas + PITR
mysql-bin.index   # ┘ list of binlog files that exist
undo_001  undo_002 # undo tablespaces: old row versions
#ib_16384_0.dblwr  # doublewrite buffer: anti-torn-page
mysql/            # system schema (users, privileges)
shop/             # one folder per database you create
  users.ibd       # ← table "users": data + primary-key
                  #   tree in ONE file
  orders.ibd
<hostname>.err    # the error log
Ask the server where it lives: SHOW VARIABLES LIKE 'datadir'; One table is one .ibd file when innodb_file_per_table is on. That is the default.
The log files · one by one

Same pattern as PostgreSQL: five logs, one job each, and every operational budget is a time window. The full version is on the PostgreSQL page.

MySQL · InnoDB

Redo log

#ib_redo0 · #ib_redo31
Where it sits
In #innodb_redo/ in the datadir: 32 files, #ib_redo0 to #ib_redo31, each 1/32 of innodb_redo_log_capacity, written as a ring. Before 8.0.30 they were a pair of files, ib_logfile0/1, in the datadir root. Same log, new home.
What it does
InnoDB's write-ahead log. A physical record of every page change, written before the page itself. On startup after a crash, InnoDB replays it to rebuild every page that was dirty in the buffer pool.
What breaks without it
InnoDB refuses to start without a valid redo log. With good reason. Without it, a committed transaction could vanish in a crash and dirty pages would be unrecoverable.
Look at it
$ ls -lh /var/lib/mysql/#innodb_redo/ -- in mysql: SHOW ENGINE INNODB STATUS\G -- read the "LOG" section: Log sequence number
MySQL

Binary log (binlog)

mysql-bin.000001 · mysql-bin.index
Where it sits
In the datadir (path set by log_bin). Numbered files that roll over as they fill. mysql-bin.index lists the ones that exist. Each event can carry a GTID. That is a permanent, unique ID for the transaction.
What it does
A logical record of every committed change. "Insert this row", not "change these bytes". Replicas replay it to stay identical. It is also the second half of point-in-time recovery. Restore a backup, then replay the binlog up to the second before the mistake.
How old files go away
By time, not by replica. binlog_expire_logs_seconds sets the age, 2592000 seconds (30 days) by default, with auto-purge on by default. Expired files go at startup and when the binlog is flushed. A connected replica that has not yet read a file keeps that one file. A disconnected replica holds nothing back.
What breaks without it
No replication, and no point-in-time recovery. You can only restore whole backups, losing everything since the last one.
Look at it
-- in mysql: SHOW BINARY LOGS; SHOW BINLOG EVENTS IN 'mysql-bin.000001'; $ mysqlbinlog mysql-bin.000001 | less

Checkpoint age: the redo ring's headroom

Writesdirty pages pile up in the buffer pool
→
Redo ring32 files, #ib_redo0 to #ib_redo31
→
Checkpoint age near capacityInnoDB throttles writers to flush speed
→
Leverraise innodb_redo_log_capacity, live

The other logs: PostgreSQL: WAL · MongoDB · WiredTiger: journal and oplog · the pattern behind all five, and where they break

Next · chapter 3What happens on every writeBuffer pool, redo, undo, binlog, locks, the optimizer← Chapter 1: Why MySQL
Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect