Galera Cluster: A Practical Guide to Setup on Debian/Ubuntu, Example With Two Nodes

Galera Cluster
Table of Contents

Galera Cluster is a high-availability solution for MySQL databases, offering synchronous replication across multiple database nodes.

Designed for MySQL, MariaDB, and Percona XtraDB Cluster, Galera ensures that data remains consistent across nodes while providing a fault-tolerant, load-balanced infrastructure.

This article will show setting up a Galera Cluster on Debian/Ubuntu servers, offering a real-world replication example between two nodes, as well as exploring its benefits and downsides.

What is Galera Cluster?

Galera Cluster is a multi-master replication system that provides a synchronized, consistent copy of the data across all database nodes.

Unlike traditional master-slave replication setups, Galera allows for read and write operations on any node, making it an excellent choice for applications that require high availability, redundancy, and scalability.

Key features include:

  • Synchronous Replication: All nodes hold identical copies of the data, ensuring consistency.
  • Multi-Master Replication: Each node can perform read and write operations, enhancing fault tolerance.
  • Automatic Node Provisioning: Adding a new node to the cluster is straightforward, with the new node syncing automatically.
  • Fault Tolerance: If one node goes down, the others can continue functioning with no data loss.

Setting Up Galera Cluster on Debian/Ubuntu

In this tutorial, we will guide you through the process of installing and configuring Galera Cluster on Debian or Ubuntu servers.

This setup will consist of two nodes, but you can easily extend it to more nodes following similar steps.

Prerequisites

Before diving into the installation, ensure you have:

  • Two servers running Debian 11 or Ubuntu 20.04 (minimum).
  • Root or sudo access on both servers.
  • MariaDB installed (since Galera Cluster works natively with MariaDB).
  • Stable network communication between the two servers.

We’ll assume the following IP addresses for the setup:

  • Node 1: 192.168.1.100
  • Node 2: 192.168.1.101

Step 1: Install MariaDB with Galera Support

First, install the necessary MariaDB and Galera packages on both servers.

  1. Update the system:
    sudo apt update && sudo apt upgrade
    
  2. Install MariaDB: MariaDB 10.5 and above come with Galera Cluster support built-in. To install it:
    sudo apt install mariadb-server mariadb-client galera-3
    

Step 2: Configure MariaDB for Galera

Next, we’ll configure MariaDB on both servers to function as part of a Galera Cluster. This requires modifying the MariaDB configuration file.

  1. Edit the MariaDB config file: Open /etc/mysql/mariadb.conf.d/50-server.cnf for editing:
    sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
    
  2. Modify the bind-address: Set the bind-address to the server’s IP to allow remote access. On Node 1, it will look like this:
    bind-address = 192.168.1.100
    

    For Node 2, use its respective IP:

    bind-address = 192.168.1.101
    
  3. Enable Galera replication settings: Add the following lines to the end of the same configuration file on both servers:
    [galera]
    # Galera settings
    wsrep_on=ON
    wsrep_cluster_address="gcomm://192.168.1.100,192.168.1.101"
    wsrep_cluster_name="my_galera_cluster"
    wsrep_node_address="192.168.1.100"   # Use Node 2's IP on its own config
    wsrep_node_name="node1"              # Use "node2" on the second server
    wsrep_sst_method=rsync
    
  4. Create a cluster user for Galera: Open MariaDB’s MySQL prompt:
    sudo mysql -u root -p
    

    Then, create a user and grant replication privileges:

    CREATE USER 'sstuser'@'%' IDENTIFIED BY 'password';
    GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'%';
    FLUSH PRIVILEGES;
    

    Ensure the user is present on both nodes.

Step 3: Start the Galera Cluster

Now that we’ve configured both servers, it’s time to bring the Galera Cluster online.

  1. Start the cluster on the first node: On Node 1, execute the following command to bootstrap the cluster:
    sudo systemctl stop mariadb
    sudo galera_new_cluster
    

    This command will initialize the cluster and bring the first node online.

  2. Start MariaDB on the second node: On Node 2, simply start MariaDB:
    sudo systemctl start mariadb
    

    The second node will automatically connect to the cluster defined by the wsrep_cluster_address setting.

Step 4: Verify Cluster Status

You can verify that the cluster is working correctly by checking the status on either node. Log in to MariaDB and run the following query:

SHOW STATUS LIKE 'wsrep_cluster_size';

This should return 2, indicating both nodes are part of the cluster.

Real-World Replication Example

Let’s test the replication between the two nodes by creating a database on Node 1 and verifying that it appears on Node 2.

  1. Create a new database on Node 1: On Node 1, enter the MySQL shell:
    sudo mysql -u root -p
    

    Then create a database:

    CREATE DATABASE test_db;
    
  2. Verify replication on Node 2: On Node 2, log into MySQL and check if the database exists:
    SHOW DATABASES;
    

    You should see test_db in the list, confirming that replication is working as expected.

Advantages of Galera Cluster

Galera Cluster brings numerous advantages, particularly for high-availability applications that require constant uptime and fault tolerance.

  1. True Synchronous Replication: Unlike traditional asynchronous replication in MySQL, Galera’s synchronous replication ensures that all nodes hold the same data at any given time.
  2. Multi-Master Architecture: Each node can accept read and write requests, significantly improving performance in distributed environments by eliminating the need for a single master.
  3. Automatic Failover: If one node fails, the others continue to operate seamlessly without any disruption to the application.
  4. Scalability: Adding more nodes to the cluster is simple and involves minimal setup. As your database grows, you can easily scale the cluster.
  5. Data Consistency: Galera ensures that data is consistent across all nodes, reducing the risk of divergence, which can occur with asynchronous replication models.
  6. Automatic Node Provisioning: New nodes can join the cluster easily by syncing data from existing nodes automatically.

Drawbacks of Galera Cluster

While Galera Cluster is powerful, it’s important to understand its limitations and potential drawbacks.

  1. Network Latency: Since Galera relies on synchronous replication, there is a performance penalty if the nodes are geographically distant from each other. Higher latency between nodes can increase response times for write operations.
  2. Split-Brain Scenarios: In the event of a network partition, where nodes cannot communicate with each other, a “split-brain” situation can arise. Proper quorum management and additional infrastructure (e.g., arbitrator nodes) are required to avoid this.
  3. Limited Write Scalability: While Galera allows multi-master writes, it does not necessarily improve write scalability due to the synchronization requirements. Heavy write workloads can experience latency as the cluster ensures data consistency across all nodes.
  4. Complexity in Configuration: While Galera’s setup is relatively straightforward, managing a cluster in production requires a solid understanding of quorum, networking, and replication methods. Incorrect configurations can lead to inconsistencies or performance bottlenecks.
  5. Limited Support for Large Transactions: Large transactions can slow down the cluster, as all nodes need to process the transaction in real-time.

Conclusion

Galera Cluster is an excellent solution for building a fault-tolerant, high-availability MySQL database infrastructure.

With features like synchronous replication and multi-master architecture, it ensures data consistency and reliability, making it ideal for mission-critical applications.

While there are challenges, particularly with network latency and large write loads, proper configuration and monitoring can make Galera a robust and scalable solution for any organization.

By following the steps outlined in this guide, you now have a functional Galera Cluster on Debian/Ubuntu and can see the benefits firsthand through real-time replication between two servers.

Latest articles
Picture of Endri Bedini
Endri Bedini

Endri Bedini is a laureate in Mechanical Engineering with over 20 years of experience in various technology fields, including Electronics, IT, and Healthcare Equipment. Throughout his career, Endri has honed his skills and expertise, earning a reputation for his exceptional problem-solving abilities and innovative thinking. In addition to his work in technology, Endri has a deep interest in Science, Astronomy, AI, Psychology, Sociology, Nature, and Evolution. He is committed to staying up-to-date with the latest developments in these fields, and his insights are informed by his broad range of knowledge and interests.

Read also

Receive new posts and updates at your e-mail address.

Subscription Form
Scroll to Top