Databases 7 min read

Installing MySQL with Docker – A Complete Step‑by‑Step Guide

This article explains how to quickly set up MySQL (as well as MariaDB and Percona) inside Docker containers, covering official images, running the latest version, connecting to the server, managing container lifecycle, passing server options, and handling multiple or specific MySQL versions.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
Installing MySQL with Docker – A Complete Step‑by‑Step Guide

In many development and testing scenarios you may need to spin up a specific version of MySQL, MariaDB, or Percona, and Docker provides a fast way to do so without affecting a production environment.

Docker offers two official MySQL images: one maintained by the Docker team (accessible via mysql:latest) and another by the MySQL team (e.g., mysql/mysql-server:latest). The latest tag refers to the most recent version in the repository.

Running the latest MySQL image

docker run --name mysql-latest \
  -p 3306:3306 -p 33060:33060 \
  -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
  -d mysql/mysql-server:latest

This command starts a MySQL container with remote root access (not recommended for production) and exposes the standard MySQL ports.

Connecting to the MySQL server

Since Docker containers do not provide host‑side client tools, you can use Docker exec to run the MySQL client or MySQL Shell inside the container:

docker exec -it mysql-latest mysql -uroot -pstrongpassword
docker exec -it mysql-latest mysqlsh -uroot -pstrongpassword

Managing the container

To stop the container use: docker stop mysql-latest To restart a stopped container use: docker start mysql-latest View logs with: docker logs mysql-latest To recreate a fresh container, stop and remove the old one before running the docker run command again.

Passing MySQL server options

docker run --name mysql-latest \
  -p 3306:3306 -p 33060:33060 \
  -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
  -d mysql/mysql-server:latest \
  --innodb_buffer_pool_size=256M \
  --innodb_flush_method=O_DIRECT

Running a specific MySQL version

docker run --name mysql-8.0.17 \
  -p 3306:3306 -p 33060:33060 \
  -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
  -d mysql/mysql-server:8.0.17

Replace the tag (e.g., 5.7) to launch other versions.

Running multiple MySQL versions simultaneously

When containers need to be accessed externally, map each to different host ports to avoid conflicts:

docker run --name mysql-latest \
  -p 3306:3306 -p 33060:33060 \
  -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
  -d mysql/mysql-server:latest
docker run --name mysql-8.0.17 \
  -p 3307:3306 -p 33070:33060 \
  -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='strongpassword' \
  -d mysql/mysql-server:8.0.17

For more details see the Docker Hub page for MySQL and the official MySQL Docker installation documentation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerdatabasemysqlContainers
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.