← Back to list

Setup MySQL with Docker Compose

A setup guideline for MySQL and Docker Compose

Szili · 2024-06-29 17:39 · 1 claps · 1.6 min read paywalled
#mysql #docker-compose #tutorial #the-quantified-world
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

Photo by Rubaitul Azad on Unsplash

Photo by Rubaitul Azad on Unsplash

Setup MySQL with Docker Compose

A setup guideline for MySQL and Docker Compose

If you are not a member, you can read the article here.

In this article we are going to setup a MySQL instance with Docker Compose. This article is part of a series of tutorials related to Docker Compose. Check out more here.

[embed]Tutorials collection (updated continuously) DevOps, Metrics, Java and moremedium.com

Setup MySQL

MySQL is an open-source relational database management system (RDBMS). This tutorial will show you how to setup the MySQL in Docker Compose. We will use the official docker image from Docker Hub.

We will also expose the port 3306 to be able to connect to it from the local machine. Also it is recommended to add a network to the service in order to connect to it from other Docker containers.

networks:
  tutorials:
    external: true

The following template can be used to start a MySQL instance:

services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_DATABASE: 'DB_NAME'
      MYSQL_USER: 'DB_USER'
      MYSQL_PASSWORD: 'DB_PASSWORD'
      MYSQL_ROOT_PASSWORD: 'DB_ROOT_PASSWORD'
    ports:
      - '3306:3306'
    volumes:
      - DB_NAME-db:/var/lib/mysql
    networks:
      - tutorials

volumes:
  DB_NAME-db:

networks:
  tutorials:
    external: true

Feel free to change the template names based on your needs:

DB_NAME - the user name
DB_PASSWORD - the user password
DB_ROOT_PASSWORD - the admin password

Start MySQL instance:

$ docker compose up -d

Stop MySQL instance:

$ docker compose down

You can connect to the service:

  • from host machine using host localhost and port 3306
  • from other docker containers using host mysql and port 3306

Useful links

Other pieces from the tutorials series

[embed]Setup MongoDB Monitoring with Prometheus and Grafana in Docker Compose A comprehensive guide to setup MongoDB deployment monitoringmedium.com

[embed]Setup Grafana and Prometheus with Docker Compose How to run Grafana and Prometheus in Dockermedium.com

[embed]Setup nginx as Reverse Proxy with Docker Compose medium.com

[embed]Monitor Java metrics with Prometheus and Grafana Use predefined Grafana dashboards for monitoring your applicationmedium.com

[embed]Sensor monitoring with InfluxDB and Grafana Connect Grafana dashboard and InfluxDB services together for sensor monitoring.medium.com

[embed]Setup and scale Jenkins on Openshift 4 TL;DR The aim of this article is to give an overview about how one can setup the Jenkins to run and scale it on…medium.com

If you think that my guide was useful, you can support my work here.


메타데이터
post_id
82fe1951ecab
slug
setup-mysql-in-docker-compose-82fe1951ecab
url
https://medium.com/@szilardmatis/setup-mysql-in-docker-compose-82fe1951ecab
canonical_url
https://medium.com/@szilardmatis/setup-mysql-in-docker-compose-82fe1951ecab
author_url
https://medium.com/@szilardmatis
status
ok
fetched_at
2026-07-20 19:09:02