Skip to content
intermediate Phase 11 · Docker & Databases

MySQL in Docker

Deploy MySQL containers with replication and backup strategies.

1h
0 problems
Topic Progress 0%

First Launch and Storage

First Launch and Storage

The official image provisions accounts and databases through environment variables on first boot:

docker run -d --name mysql \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=app \
  -e MYSQL_USER=app \
  -e MYSQL_PASSWORD=apppass \
  -v mysqlData:/var/lib/mysql \
  mysql:8

State lives in /var/lib/mysql, so the volume is as mandatory as with Postgres. Health checks use mysqladmin ping -h 127.0.0.1 (it waits for the temporary server used during init to finish). Note that MySQL 8 defaults to caching_sha2_password; older clients that only speak mysql_native_password fail to authenticate until you configure the user's plugin.

Primary-Replica Replication

Primary-Replica Replication

Binlog-based replication needs three pieces: enabled binary logging (default in 8.0), a replication account on the primary, and the replica pointed at it:

-- primary
CREATE USER 'repl'@'%' IDENTIFIED BY 'replpass';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

-- replica
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='primary', SOURCE_USER='repl',
  SOURCE_PASSWORD='replpass', SOURCE_AUTO_POSITION=1;
START REPLICA;

SOURCE_AUTO_POSITION=1 lets GTID coordinates resume automatically after restarts. Verify with SHOW REPLICA STATUS\G — both Replica_IO_Running and Replica_SQL_Running must be Yes. Each container needs its own server-id.

Backups and Pitfalls

Backups and Pitfalls

For InnoDB, a transactionally consistent logical backup:

docker exec mysql mysqldump -u root -p --single-transaction \
  --routines --triggers app > app.sql

--single-transaction takes a consistent snapshot without locking writes; skip it for MyISAM tables, which need --lock-all-tables. Restore with docker exec -i mysql mysql -u root -p app < app.sql.

Pitfalls. Init SQL in /docker-entrypoint-initdb.d runs only on an empty datadir. Test restores — an untested dump is a hope, not a backup. Prefer replica-based backups to keep load off the primary.

Interview: describe the replication handshake, name the two status fields you monitor, and explain why --single-transaction is safe for InnoDB.

Quiz

1. Why must every replica set a unique server-id?

Question 1 options

2. What does mysqldump --single-transaction guarantee?

Question 2 options

3. A legacy JDBC driver fails with caching_sha2_password. First thing to check?

Question 3 options

Flashcards

Question

MYSQL_* env init

Answer

Root password, database, and app user are created only on first boot of an empty datadir.

Question

CHANGE REPLICATION SOURCE

Answer

MySQL 8 command pointing a replica at its primary (replaces CHANGE MASTER TO).

Question

SOURCE_AUTO_POSITION

Answer

GTID-based resume so replicas catch up without manual binlog file/position math.

Question

--single-transaction

Answer

Consistent non-locking InnoDB dump; MyISAM needs --lock-all-tables instead.

Revision Notes

Key Takeaways

  • 1. Volume /var/lib/mysql plus env-based init on first boot only
  • 2. Replication = binlog + repl user + unique server-id + START REPLICA
  • 3. Dump with --single-transaction and test restores regularly

Interview Tips

  • • Walk the replica handshake and which status columns you alert on
  • • Explain when logical dumps lose to physical backups (size, time, PITR)

Cheat Sheet

mysql:8 + -v /var/lib/mysql; unique server-id; CHANGE REPLICATION SOURCE + START REPLICA; check SHOW REPLICA STATUS IO+SQL Yes; mysqldump --single-transaction.

Continue Learning