Install Galera for MySQL/MariaDB | Linux Setup Guide

Scene with engineers setting up Galera on Linux to optimize database clustering

Implementing database clusters, for data reliability across Linux servers at IOFLOOD, lead us to explore the installation and usage of Galera. Galera’s synchronous replication and multi-master capabilities offer a resilient solution for database clustering and failover management. This article provides a detailed guide on installing and using Galera in Linux, empowering our customers to build robust and scalable database clusters on their custom server configurations.

In this guide, we will navigate the process of installing Galera on your Linux system. We will provide you with installation instructions for APT-based distributions like Debian and Ubuntu, as well as YUM-based distributions like CentOS and AlmaLinux. We’ll also delve into how to compile Galera from the source, how to install a specific version, and finally, we will show you how to use the Galera command and ascertain that the correctly installed version is in use.

Let’s get started with the step-by-step Galera installation on your Linux system!

TL;DR: How Do I Install and Use Galera in Linux?

To install Galera on Debian-based systems like Ubuntu, run sudo apt-get install galera-3. For RPM-based systems like CentOS, use sudo yum install galera.

For example, in Ubuntu, you can run the following commands:

sudo apt-get update
sudo apt-get install galera-3 galera-arbitrator-3

This will install Galera and its arbitrator package on your Ubuntu system. However, this is just the tip of the iceberg. Galera offers a lot more features and functionalities that can be leveraged for efficient database management. Continue reading for a more detailed guide on installing and using Galera in Linux, including advanced installation methods, usage examples, troubleshooting tips, and more.

Getting Started with Galera

Galera is a powerful database replication system that allows for multi-master replication, meaning it allows you to write to any node and have the data replicated to all other nodes. It’s a crucial tool for anyone looking to set up a high-availability database cluster in Linux, providing redundancy, stability, and improved data access.

Now, let’s delve into how to install Galera on your Linux system.

Installing Galera with APT

If you’re using a Debian-based distribution like Ubuntu, you can install Galera using the Advanced Package Tool (APT). Here’s how:

sudo apt-get update
sudo apt-get install galera-4

In this example, sudo apt-get update updates the package list on your Linux system, ensuring you’re installing the latest version. sudo apt-get install galera-4 then installs the Galera package.

Installing Galera with YUM

For those using a Red Hat-based distribution like CentOS or AlmaLinux, the Yellowdog Updater, Modified (YUM) is your package manager. Here’s how to install Galera using YUM:

sudo yum update
sudo yum install galera-4

Just like with APT, sudo yum update updates your package list, and sudo yum install galera-4 installs Galera.

Installing Galera with Zypper

If you’re using an openSUSE distribution, you can install Galera using the Zypper package manager. Here’s how:

sudo zypper refresh
sudo zypper install galera-4

In this example, sudo zypper refresh updates the package list, ensuring you’re installing the latest version. sudo zypper install galera-4 then installs the Galera package.

By the end of these steps, you should have Galera installed on your Linux system, regardless of the distribution. In the next section, we’ll cover more advanced installation methods and how to use Galera effectively.

Installing Galera from Source Code

While package managers make installation easy, installing from source code gives you more control over the version you install and can help you understand the software better. Here’s how to install Galera from the source code:

# Download the source code
wget https://github.com/codership/galera/archive/refs/tags/release_26.4.8.tar.gz

# Extract the tarball
 tar -xzf release_26.4.8.tar.gz

# Go to the Galera directory
cd galera-release_26.4.8

# Build the software
scons

# Install the software
sudo scons install

This script first downloads the source code from the official Galera repository on GitHub. It then extracts the tarball, navigates to the newly extracted directory, builds the software using SCons (a software construction tool), and finally installs the software.

Installing Specific Galera Versions

Different versions of Galera might have different features, bug fixes, or compatibility with different versions of MySQL or MariaDB. Here’s how to install a specific version of Galera using APT or YUM, and from source code.

Installing Specific Versions with APT

# Update your package lists
sudo apt-get update

# Install a specific version of Galera
sudo apt-get install galera-3=3.28-1

Installing Specific Versions with YUM

# Update your package lists
sudo yum update

# Install a specific version of Galera
sudo yum install galera-3-3.28-1

Installing Specific Versions from Source Code

# Download the source code for a specific version
wget https://github.com/codership/galera/archive/refs/tags/release_26.4.8.tar.gz

# Follow the same steps as above to extract and install

Galera Version Comparison

VersionKey FeaturesCompatibility
Galera 3Multi-master replication, synchronous replication, automatic node joiningMySQL 5.6, MariaDB 10.0
Galera 4Improved foreign key support, streaming replication, improved error messagesMySQL 5.7, MariaDB 10.3

Basic Galera Usage

Once you’ve installed Galera, you can verify it’s installed correctly and start using it. Here’s how to check the version of Galera you’ve installed:

# Check the Galera version
mysqld --wsrep-provider-version

# Output:
# '26.4.8'

This command will output the version of Galera you’ve installed. If you see a version number, that means Galera is installed correctly.

Here’s a basic example of how to use Galera to create a new database:

# Log in to MySQL as root
mysql -u root -p

# Create a new database
CREATE DATABASE my_database;

# Output:
# 'Query OK, 1 row affected (0.00 sec)'

This script logs you in to MySQL as the root user, then creates a new database called ‘my_database’. If you see ‘Query OK, 1 row affected’, that means the database was created successfully.

Alternate Database Clusters

While Galera is a powerful tool for setting up high-availability database clusters, it’s not the only option available. Other popular alternatives include MySQL Cluster and PostgreSQL with replication. Let’s take a closer look at each of these methods.

MySQL Cluster for High-Availability

MySQL Cluster is a technology that enables clustering of in-memory databases in a shared-nothing system. The shared-nothing architecture allows the system to work with minimal need for synchronization, resulting in high availability and scalability, the main goals of MySQL Cluster.

Here’s a basic example of how to install MySQL Cluster on Ubuntu:

# Update your package lists
sudo apt-get update

# Install MySQL Cluster
sudo apt-get install mysql-cluster

# Output:
# 'Setting up mysql-cluster (7.6.9-1ubuntu18.04) ...'

This script updates your package list and installs MySQL Cluster. If you see ‘Setting up mysql-cluster’, that means MySQL Cluster was installed successfully.

PostgreSQL with Replication

Another alternative is PostgreSQL with replication, which is a technology that allows data from one PostgreSQL database cluster to be copied and stored on another cluster. This can be done in two ways: synchronous replication and asynchronous replication.

Here’s an example of how to install PostgreSQL on Ubuntu:

# Update your package lists
sudo apt-get update

# Install PostgreSQL
sudo apt-get install postgresql postgresql-contrib

# Output:
# 'Setting up postgresql (10+190) ...'

This script updates your package list and installs PostgreSQL along with some additional modules. If you see ‘Setting up postgresql’, that means PostgreSQL was installed successfully.

Galera vs. MySQL Cluster vs. PostgreSQL with Replication

While all three options provide high-availability database clusters, they each have their own strengths and weaknesses. Galera’s strengths lie in its automatic node joining and multi-master replication, but it requires a Galera-compatible database like MySQL or MariaDB. MySQL Cluster excels at in-memory databases and has a shared-nothing architecture, but requires specific hardware and software setups. PostgreSQL with replication is highly flexible and can be configured for either synchronous or asynchronous replication, but requires more manual setup and maintenance.

Ultimately, the choice between Galera, MySQL Cluster, and PostgreSQL with replication will depend on your specific needs, resources, and expertise. It’s recommended to test each option in a non-production environment to determine the best fit for you.

Troubleshooting Issues in Galera

Just like with any software, you might encounter some issues when installing or using Galera. Here are some common problems and how to solve them.

Issue: Galera Not Starting After Installation

After installing Galera, you might find that it doesn’t start automatically. This is a common issue that can be resolved by manually starting the service.

# Start the Galera service
sudo systemctl start mysql

# Check the status of the Galera service
sudo systemctl status mysql

# Output:
# '● mysql.service - MySQL Community Server'
# '   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)'
# '   Active: active (running) since Tue 2022-03-01 14:58:39 UTC; 1s ago'

This script starts the Galera service and checks its status. If you see ‘Active: active (running)’, that means Galera is running successfully.

Issue: Galera Version Mismatch

You might encounter a version mismatch issue if different nodes in your cluster are running different versions of Galera. To ensure all nodes are running the same version, you can check the version of Galera on each node.

# Check the Galera version on a node
mysqld --wsrep-provider-version

# Output:
# '26.4.8'

This command will output the version of Galera running on a node. Check this on all nodes to ensure they’re running the same version.

Issue: Galera Node Not Joining Cluster

If a node isn’t joining the cluster, it might be because it’s not configured correctly. Check the configuration file for any errors.

# Check the Galera configuration
sudo nano /etc/mysql/my.cnf

This command opens the Galera configuration file in a text editor. Check that the ‘wsrep_cluster_address’ line includes the correct IP addresses for all nodes.

These are just a few of the common issues you might encounter when using Galera. Remember, the key to successful troubleshooting is understanding the problem, being patient, and methodically testing solutions.

Core Concepts of Galera

To truly grasp the power of Galera, it’s essential to understand the concepts of database clustering and high-availability. These concepts are not only fundamental to Galera but also crucial in the realm of system administration and data management.

Database Clustering: An Overview

Database clustering refers to the arrangement of databases across multiple servers or nodes. This setup ensures that the databases function as a single logical unit, providing better performance, scalability, and availability.

Consider a library with a single bookshelf (a single database on a server). If the bookshelf gets overloaded or damaged, access to the books becomes difficult. Now, imagine if the library had multiple bookshelves (a database cluster), each carrying a replica of the books. Even if one bookshelf becomes inaccessible, you can still access the books from the other shelves.

# In a database cluster, data is replicated across multiple nodes
# Here's a simplified example using MySQL and Galera

# Node 1
mysql -u root -p -e "CREATE DATABASE library"

# Node 2
mysql -u root -p -e "SHOW DATABASES"

# Output:
# 'library'

In this example, we create a database called ‘library’ on Node 1. When we list the databases on Node 2, ‘library’ appears, showing that the data has been replicated across the nodes.

High-Availability: Why It Matters

High-availability is a characteristic of a system that aims to ensure an agreed level of operational performance for a higher than normal period. In the context of databases, high-availability means that your databases are operational and accessible for a vast majority of the time.

# With a high-availability setup, even if one node fails, the database is still accessible
# Here's a simplified example

# Node 1 fails
# But you can still access the database from Node 2
mysql -u root -p -e "USE library"

# Output:
# 'Database changed'

In this example, even though Node 1 (where we initially created the ‘library’ database) fails, we can still access the ‘library’ database from Node 2. This is the power of a high-availability setup.

Understanding database clustering and high-availability is crucial for system administrators and data managers. These concepts underpin the design and operation of Galera, enabling it to provide robust, scalable, and highly available database management for Linux systems.

Practical Uses of Database Clusters

High-availability database clusters like Galera are not just tools; they are crucial components in the world of system administration and data management. They provide redundancy, improve data access, and ensure that your system can handle increased load without sacrificing performance or availability.

Exploring Load Balancing

Load balancing is a technique used to distribute workloads across multiple computing resources, such as servers, databases, or network links. It aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any single resource.

# Example of a simple load balancing setup with Nginx

# Install Nginx
sudo apt-get install nginx

# Configure Nginx for load balancing
sudo nano /etc/nginx/nginx.conf

# Add the following configuration
upstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    listen 80;

    location / {
        proxy_pass http://backend;
    }
}

In this example, we install Nginx, a popular web server that can also be used as a reverse proxy and load balancer. We then configure Nginx to distribute incoming requests to two backend servers, effectively balancing the load between them.

Diving into Data Replication

Data replication is the process of storing data in more than one site or node to improve the data’s availability. It’s a crucial part of high-availability database clusters like Galera.

# Example of data replication with MySQL

# Install MySQL
sudo apt-get install mysql-server

# Configure MySQL for replication
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# Set the server ID and enable binary logging
[mysqld]
server-id=1
log_bin=mysql-bin

In this example, we install MySQL and configure it for replication by setting a unique server ID and enabling binary logging, a crucial component for MySQL replication.

Further Resources for Galera Mastery

For those who wish to delve deeper into Galera and related concepts, here are some resources:

Recap: Installing Galera for Linux

In this comprehensive guide, we’ve navigated the intricate process of installing and using Galera, a powerful tool for high-availability database clustering, on Linux systems.

We embarked on this journey with the basics, learning how to install Galera using various package managers in different Linux distributions. We then explored advanced installation methods, including installing Galera from source and installing specific versions, providing you with a greater degree of control and flexibility.

Along the way, we tackled common issues you might encounter when using Galera, such as the service not starting after installation, version mismatch, and nodes not joining the cluster. We offered practical solutions to these problems, ensuring a smooth and seamless Galera experience.

We also delved into alternative approaches to high-availability database clustering, comparing Galera with MySQL Cluster and PostgreSQL with replication. Here’s a quick comparison of these methods:

MethodProsCons
GaleraAutomatic node joining, multi-master replicationRequires Galera-compatible database
MySQL ClusterIn-memory databases, shared-nothing architectureRequires specific hardware and software setups
PostgreSQL with replicationHighly flexible, configurable for synchronous or asynchronous replicationRequires more manual setup and maintenance

As we conclude this guide, we hope you’ve gained a deeper understanding of Galera and its capabilities. Whether you’re setting up a high-availability database cluster for the first time or looking to enhance your existing setup, Galera offers a robust and reliable solution.

With its blend of automatic node joining, multi-master replication, and compatibility with popular databases like MySQL and MariaDB, Galera is an indispensable tool in your system administration and data management toolkit. Happy clustering!