Cloud Native 5 min read

How to Deploy MySQL with Docker: A Step-by-Step Guide

This guide explains how to install Docker, pull the official MySQL image, run a MySQL container with persistent storage, connect to it, perform basic SQL operations, and finally stop and remove the container, demonstrating a complete end‑to‑end deployment using Docker.

DevOps Operations Practice
DevOps Operations Practice
DevOps Operations Practice
How to Deploy MySQL with Docker: A Step-by-Step Guide

Containerization has become popular in software development and deployment, and Docker is a widely adopted platform that simplifies building, deploying, and managing applications and their dependencies.

This article shows how to deploy a MySQL database using Docker.

1. Install Docker

First, ensure Docker is installed on your system by downloading it from the official Docker website for your operating system.

2. Pull the MySQL image

After Docker is installed, pull the official MySQL image from Docker Hub: docker pull mysql This downloads the latest MySQL version to your local environment.

3. Start the MySQL container

Run a MySQL container with the following command:

docker run -d --name mysql-demo -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

The flags mean: --name mysql-demo: assign a name to the container. -v /data/mysql:/var/lib/mysql: mount the container’s data directory to the host for persistence. -e MYSQL_ROOT_PASSWORD=123456: set the root password via an environment variable. -d: run the container in detached mode. mysql: specify the image to use.

4. Connect to MySQL

Once the container is running, connect to the MySQL server: docker exec -it mysql-demo mysql -uroot -p You will be prompted for the root password.

5. Perform database operations

After connecting, you can execute common SQL commands, for example:

CREATE DATABASE mydatabase;
USE mydatabase;
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL
);
INSERT INTO users (username, email) VALUES ('john_doe', '[email protected]');

6. Stop and remove the container

When you finish, stop and optionally delete the container:

docker stop mysql-demo
docker rm mysql-demo

Using Docker, you can quickly deploy and manage a MySQL database anywhere, avoiding configuration issues related to operating systems and dependencies.

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.

Cloud NativedatabaseDevOpscontainerizationmysql
DevOps Operations Practice
Written by

DevOps Operations Practice

We share professional insights on cloud-native, DevOps & operations, Kubernetes, observability & monitoring, and Linux systems.

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.