#1
If you want to use MySQL without the hassle of installing it directly on your computer, Docker is a great solution. With Docker, you can run MySQL in a lightweight, isolated container that’s easy to set up, manage, and remove when you're done.
In this beginner-friendly guide, you’ll learn how to install and run MySQL using Docker, set up a user and password, and connect to it from your applications or database tools.
Let’s get started!

Why Use Docker for MySQL?

Docker allows you to run MySQL without actually installing it on your host machine. Here's why it's helpful:
  • Quick to set up
  • Easy to clean up
  • No risk of breaking your system
  • You can run multiple versions at once
  • Great for development and testing

Prerequisites

Before you begin, make sure Docker is installed on your system.

For macOS or Windows:

Install Docker Desktop: https://www.docker.com/products/docker-desktop

For Ubuntu/Linux:

sudo apt update && sudo apt install docker.io
Check your installation:
docker --version

Step 1: Pull the MySQL Docker Image

Open your terminal and run:

docker pull mysql
This command downloads the official MySQL image from Docker Hub. If you want a specific version, you can include it:
docker pull mysql:8.0

Step 2: Run a MySQL Container

Here’s a simple command to start a MySQL container:
docker run --name my-mysql \
  -e MYSQL_ROOT_PASSWORD=secret123 \
  -e MYSQL_DATABASE=mydb \
  -e MYSQL_USER=user1 \
  -e MYSQL_PASSWORD=pass123 \
  -p 3306:3306 \
  -d mysql:8.0

Explanation:

  • --name my-mysql: The name of your container
  • -e MYSQL_ROOT_PASSWORD: Sets the root password
  • -e MYSQL_DATABASE: Creates a new database
  • -e MYSQL_USER and -e MYSQL_PASSWORD: Creates a new user with a password
  • -p 3306:3306: Maps the MySQL port to your local machine
  • -d: Runs the container in the background
  • mysql:8.0: Specifies the MySQL image version

Step 3: Persist Data Using Docker Volumes

If you want to keep your data even after the container stops, use volumes:
docker run --name my-mysql \
  -e MYSQL_ROOT_PASSWORD=secret123 \
  -e MYSQL_DATABASE=mydb \
  -e MYSQL_USER=user1 \
  -e MYSQL_PASSWORD=pass123 \
  -p 3306:3306 \
  -v mysql-data:/var/lib/mysql \
  -d mysql:8.0
This creates a Docker-managed volume called mysql-data to store your MySQL data.

Step 4: Check If MySQL is Running

To check if your MySQL container is active:
docker ps
You should see your container listed with port 3306 open.

Step 5: Connect to MySQL

You can connect using:

MySQL CLI (if installed on your host):

mysql -h 127.0.0.1 -P 3306 -u user1 -p
Enter the password when prompted.

GUI Tools (e.g., DBeaver, MySQL Workbench):

Use these connection settings:
  • Host: 127.0.0.1 or localhost
  • Port: 3306
  • Username: user1
  • Password: pass123
  • Database: mydb

Step 6: Stop and Remove the Container

If you want to stop the container:

docker stop my-mysql
To start it again:
docker start my-mysql
To remove it completely (be careful!):
docker rm -f my-mysql && docker volume rm mysql-data

Best Practices

  • Always use Docker volumes to persist your database data
  • Avoid using root for application-level access—use separate users
  • For production, secure your passwords and manage secrets properly
  • Use Docker Compose for managing multiple containers

Bonus: Docker Compose (Optional)

Want to make your setup even easier? Use docker-compose.yml:

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    container_name: my-mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret123
      MYSQL_DATABASE: mydb
      MYSQL_USER: user1
      MYSQL_PASSWORD: pass123
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:
Then run:
docker-compose up -d

image quote pre code