MariaDB Galera Cluster using Docker

Introduction

MariaDB Galera Cluster is a robust solution that combines the power of MariaDB, an open-source relational database management system, with Galera Cluster, a synchronous multi-master replication plugin.

MariaDB, a popular MySQL fork, offers a feature-enhanced and community-driven alternative to MySQL.

Galera Cluster, on the other hand, is a sophisticated replication technology that operates in a synchronous manner. It allows multiple database nodes to work together as a cluster, ensuring that all nodes have an identical copy of the database.

Use Cases : MariaDB Galera Cluster

E-commerce platforms

Ensuring uninterrupted availability and consistent data across multiple locations.

Financial systems

Maintaining data integrity and eliminating single points of failure.

Mission-critical applications

Providing fault tolerance and high performance for critical business operations.

Key Features and Benefits: MariaDB Galera Cluster

High Availability

With MariaDB Galera Cluster, your database remains highly available even in the event of node failures. If one node becomes unreachable, the cluster automatically promotes another node as the new primary, ensuring continuous operation and minimal downtime.

Data Consistency

Galera Cluster’s synchronous replication ensures that data consistency is maintained across all nodes in real time. Each transaction is applied uniformly to every node, preventing any data discrepancies or conflicts.

Scalability and Load Balancing

By distributing the workload across multiple nodes, MariaDB Galera Cluster allows you to scale your database system horizontally. As your application grows, you can add additional nodes to handle increased traffic, providing enhanced performance and improved response times. Load balancing is inherent to the cluster setup, enabling efficient resource utilization.

Automatic Data Distribution

When you write data to the cluster, it is automatically synchronized across all nodes. This means that read queries can be executed on any node, promoting load balancing and reducing the load on individual nodes.

How to set up a MariaDB Galera Cluster using Docker

you can follow these steps

Install Docker

If you haven’t already, install Docker on your system. Refer to

Create a Docker network

Open a terminal and create a Docker network that will be used by the Galera cluster nodes:

MariaDB Galera Cluster using Docker

Create Galera Cluster Containers

Create multiple Docker containers, each representing a node in the Galera cluster.

You can use the official MariaDB Galera Cluster Docker image or a custom image. For example, creating three nodes:

# docker run -d --name=node1 --network=galera_network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e GALERA_CLUSTER=galera_cluster -e GALERA_NODE_NAME=node1 -e GALERA_NODE_ADDRESS=node1 mariadb:10.6 --wsrep-new-cluster

# docker run -d --name=node2 --network=galera_network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e GALERA_CLUSTER=galera_cluster -e GALERA_NODE_NAME=node2 -e GALERA_NODE_ADDRESS=node2 mariadb:10.6

# docker run -d --name=node3 --network=galera_network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e GALERA_CLUSTER=galera_cluster -e GALERA_NODE_NAME=node3 -e GALERA_NODE_ADDRESS=node3 mariadb:10.6

In the above commands, adjust the image version, container names, network, and other environment variables according to your requirements.

Verify Cluster Status

Check the cluster status by executing the following command in any of the cluster nodes:

docker exec -it node1 mysql -uroot -pmy-secret-pw -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

This command should display the number of nodes in the cluster

Conclusion

You now have a MariaDB Galera Cluster set up using Docker. You can connect to any of the nodes using the appropriate connection details (e.g., hostname, port, username, password) and start using the cluster.

I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

3 thoughts on “MariaDB Galera Cluster using Docker

  1. I run the 3 above docker commands to create the nodes. However, when I run the 4th docker commands, the output is 0. Even when I change the node name from node1 to node2 to node3, it still returns 0

    1. Hi Jacky,
      It seems like you are facing an issue with the cluster size returning 0 even after running the Docker commands. Here are a few troubleshooting steps you can follow:

      Correct Container Connection: Ensure that you are connecting to the correct container. Instead of using node1, try using node2 or node3 to see if you get a different result.

      docker exec -it node2 mysql -uroot -pmy-secret-pw -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
      Wait for Full Cluster Formation: When setting up a cluster, it takes some time for nodes to join and form a complete cluster. Make sure you have waited long enough (e.g., 1-2 minutes) before rechecking.

      Check Logs: Check the logs of each container for any error messages. Use the following command to check the logs of node1:

      docker logs node1
      Replace node1 with the names of other nodes to check their logs.

      Verify Cluster Configuration: Ensure that all nodes are using the same network (galera_network) and that environment variables like GALERA_CLUSTER, GALERA_NODE_NAME, and GALERA_NODE_ADDRESS are set correctly.

      If the issue persists after checking these points, please provide more information about any specific errors you encounter so that we can offer more detailed assistance.

      Good luck!

      1. Made everyting like in your tutorial but having the same issue like previous commentor can you pls reach out to tell what can be wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.