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.
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:latestThis 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 -pstrongpasswordManaging 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_DIRECTRunning 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.17Replace 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.17For more details see the Docker Hub page for MySQL and the official MySQL Docker installation documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
