composebuilder

Docker Compose: MySQL 8.4 + phpMyAdmin 5

Docker Compose stack for MySQL 8.4 LTS with a healthcheck, a persistent volume and phpMyAdmin 5.2 on localhost. Credentials come from .env, with checks.

When to use this stack

MySQL is one of the most widely deployed relational databases and the default choice for many PHP, WordPress and Laravel projects. This stack runs MySQL 8.4 LTS with a healthcheck, stores its data in a named Docker volume, and adds phpMyAdmin as a browser client for tables, queries, imports and exports. It is a practical setup for a single server that needs a stable database and an admin screen, without installing MySQL or PHP tools directly on the host.

Use this stack when a local or single-VPS application needs MySQL, when you want a development environment that matches a long-term support release, or when you need phpMyAdmin for occasional administration. Do not use it as the only protection for critical data. A single MySQL container has no replication or automatic failover, so you need scheduled dumps and a tested restore procedure, or a managed database for high availability. Also do not publish port 3306 to the internet. Only the phpMyAdmin port is published, and only on localhost, so you can reach it through an SSH tunnel.

docker-compose.yml

services:
  mysql:
    image: mysql:8.4
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p$$MYSQL_ROOT_PASSWORD --silent"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 60s

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:5.2.1
    restart: unless-stopped
    environment:
      PMA_HOST: mysql
    ports:
      - "127.0.0.1:8081:80"
    depends_on:
      mysql:
        condition: service_healthy

volumes:
  mysql_data:

.env

.env
# Root password is used for administration only
MYSQL_ROOT_PASSWORD=CHANGE_ME_ROOT_PASSWORD

# Application database and its dedicated user
MYSQL_DATABASE=app_db
MYSQL_USER=app_user
MYSQL_PASSWORD=CHANGE_ME_APP_PASSWORD

Start and verify

The variables are loaded into the shell only for the checks below. Compose reads .env by itself. The first start takes longer than later starts because MySQL initializes its data directory, so wait until docker compose ps shows the mysql service as healthy before testing the login.

Shell
set -a; . ./.env; set +a
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=50 mysql
docker compose exec mysql mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -e "SELECT VERSION();" "$MYSQL_DATABASE"
curl -s -o /dev/null -w "phpMyAdmin HTTP status: %{http_code}\n" http://127.0.0.1:8081/

Production checklist

  • Replace every CHANGE_ME value with a long random password, and keep the root password for administration only.
  • Keep port 3306 unpublished. Applications reach MySQL over the Compose network through the host name mysql.
  • Grant the application user rights only on its own database, for example SELECT, INSERT, UPDATE and DELETE on app_db.
  • Back up with mysqldump or mysqlpump on a schedule, store the files off the server, and test a restore.
  • Set a memory limit and size innodb_buffer_pool_size to a fraction of the host RAM through the command section.
  • Keep phpMyAdmin on localhost behind an SSH tunnel, or behind a TLS proxy with access control, and remove it when nobody needs it.
  • Update the images on a regular schedule, reading the release notes for the 8.4 line before each upgrade.
  • Check that the data volume has enough free space, because InnoDB stops accepting writes when the disk is full.

Common problems

  • Symptom: MySQL logs access denied after you change MYSQL_ROOT_PASSWORD in .env. Cause: the root password is set only when the data directory is first created. Fix: change it inside MySQL with ALTER USER, or remove the volume with docker compose down -v if the data is disposable.

  • Symptom: phpMyAdmin shows the error #2002 Cannot log in to the MySQL server. Cause: MySQL is still initializing, or PMA_HOST does not match the service name. Fix: wait for the healthy status in docker compose ps and check that PMA_HOST is set to mysql.

  • Symptom: the mysql container is marked unhealthy on a slow VPS during the first start. Cause: the initialization takes longer than the healthcheck allows. Fix: increase start_period to 120s and keep retries at 10.

  • Symptom: an older client library fails with an error about the caching_sha2_password plugin. Cause: the client does not support the default authentication plugin of MySQL 8.x. Fix: update the client library. Avoid switching the server to the older plugin for a production database.

Variations

  • MariaDB instead of MySQL: the MariaDB stack in this category uses the same layout with a different image and variable names.
  • Read-only reporting user: create a separate account with SELECT rights only and use it for dashboards or exports.
  • Reverse proxy with basic authentication: place Caddy or Nginx in front of phpMyAdmin, add HTTP basic authentication and HTTPS, and expose only the proxy ports.

Questions

Why does MySQL take a minute on the first start?

On an empty volume the image creates the data directory, the root account and the application database, then restarts the server. The healthcheck start_period gives it time, and phpMyAdmin waits until MySQL reports healthy.

Why does phpMyAdmin show a #2002 cannot log in error?

The PMA_HOST value must match the MySQL service name, which is mysql in this file, and the database must already be healthy. Run docker compose logs phpmyadmin to see the exact error.

Can I use MySQL 8.0 instead of 8.4?

You can change the tag, but 8.4 is the current long-term support line. Test your application against the new tag before changing it in production.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • WordPress + MySQL + phpMyAdmin

    Run WordPress with MySQL 8.4 and phpMyAdmin in Docker Compose, with persistent volumes, healthchecks, a local-only admin panel and a production checklist.

    • wordpress
    • mysql
    • phpmyadmin
    Difficulty: Beginner
  • ClickHouse 25.8 Analytics Database

    Docker Compose stack for ClickHouse 25.8 with a dedicated user, a healthcheck, raised file limits and HTTP and native ports on localhost for analytical queries.

    • clickhouse
    Difficulty: Advanced
  • Elasticsearch 8 + Kibana Search Stack

    Docker Compose stack for Elasticsearch 8.19 and Kibana 8.19 with security enabled, a persistent volume, a healthcheck and localhost-only ports.

    • elasticsearch
    • kibana
    Difficulty: Advanced
  • FastAPI + MongoDB 7 + Mongo Express

    Docker Compose stack for a Python FastAPI API on MongoDB 7 with authentication, a healthcheck, persistent storage and a protected Mongo Express UI on localhost.

    • api
    • mongodb
    • mongo-express
    Difficulty: Intermediate

Browse more Databases & Search stacks or the full library.