How to Setup a MySQL DB Cluster in Docker ( Docker DB Cluster)


Video Tutorial

Blog Tutorial

1. At first clone the repo

     git clone

2. Then open the repo or switch to the repo folder and apply the docker compose yml

    docker-compose up -d

3.  Then exec into the docker container

      docker exec -it docker-mysql-db-cluster-mysql-dev1-1 bash

4.  Login to mysql shell cli

     mysqlsh -u clusteradmin -p

5. Next, Check Configuration


6.  Configure the instance


7. Create the Cluster

    var cluster = dba.createCluster("dbcluster")

8. Check Status


9. Add the second DB Instance


10.  Create Database and Insert some data in the Primary Container

Exit and exec again. Then Login using mysql db cli

mysql -u clusteradmin -p 

Then Create a Database


use developer;

Create a Table



Insert Data

INSERT INTO dev VALUE("dev1",1);


11.  Exec into secondary container & check the data. 

SELECT * from

You can see the data that you inserted from primary container as the data will be replicated automatically. That means, You have successfully created the DB Cluster in Docker!!!!


Why Do You Need a DB Cluster in Docker

Using a MySQL database cluster in a Docker environment can provide several benefits, particularly in terms of scalability, flexibility, and ease of management. Here are some of the key advantages:

  1. Scalability:

    • Horizontal Scaling: Docker makes it easy to deploy multiple instances of MySQL databases, allowing for horizontal scaling. This is crucial for handling increased workloads as your application grows.
    • Container Orchestration: Container orchestration tools like Kubernetes can be used to manage and scale MySQL containers dynamically based on demand.
  2. Flexibility:

    • Consistent Environment: Docker containers encapsulate the application, dependencies, and configuration, ensuring a consistent environment across different stages of development, testing, and production.
    • Isolation: Each MySQL instance runs in its own container, providing isolation. This isolation makes it easier to manage dependencies and avoids conflicts between different versions of MySQL or other software.
  3. Efficient Resource Utilization:

    • Resource Isolation: Docker containers share the host OS kernel, making them lightweight compared to virtual machines. This results in more efficient use of resources and allows running multiple MySQL instances on the same host without significant overhead.
  4. Easy Deployment and Rollback:

    • Containerization: Packaging MySQL as a container allows for easy deployment across various environments, reducing the likelihood of "it works on my machine" issues.
    • Rollback Capabilities: Docker allows for versioning of container images, enabling straightforward rollback to a previous version if issues arise during an update.
  5. Rapid Development and Testing:

    • DevOps Practices: Docker facilitates DevOps practices by streamlining the development and testing process. Developers can work in a containerized environment that mirrors production, reducing the chances of discrepancies between environments.
  6. Isolation and Security:

    • Isolation of Processes: Containers provide process isolation, ensuring that each MySQL instance runs independently.
    • Security Updates: Docker allows you to update individual containers without affecting the entire system, making it easier to apply security patches.
  7. High Availability and Fault Tolerance:

    • Replication and Clustering: MySQL clusters can be set up using Docker Compose or Kubernetes, providing high availability and fault tolerance.
    • Automated Failover: Tools like Orchestrator or ProxySQL, when combined with Docker and container orchestration, can automate failover processes in the event of a node failure.
  8. Infrastructure as Code (IaC):

    • Declarative Configuration: Docker Compose or Kubernetes configurations can be used to declare the desired state of the MySQL cluster. This aligns with Infrastructure as Code principles, making infrastructure configurations version-controlled and reproducible.
  9. Reduced Development Time:

    • Quick Setup for Developers: Developers can quickly set up a MySQL cluster locally using Docker containers, reducing the time needed for environment configuration.
  10. Cost-Effective:

    • Resource Efficiency: Docker's efficient resource utilization allows for running multiple containers on the same host, potentially reducing infrastructure costs.

While using MySQL clusters in Docker brings numerous benefits, it's essential to carefully plan the deployment, consider data persistence, and implement appropriate backup and recovery strategies to ensure the reliability and availability of your database systems.


Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment


Post a Comment