How to Create a MariaDB Galera Cluster on AlmaLinux
Categories:
MariaDB Galera Cluster is a powerful solution for achieving high availability, scalability, and fault tolerance in your database environment. By creating a Galera Cluster, you enable a multi-master replication setup where all nodes in the cluster can process both read and write requests. This eliminates the single point of failure and provides real-time synchronization across nodes.
AlmaLinux, a community-driven RHEL-based Linux distribution, is an excellent platform for hosting MariaDB Galera Cluster due to its reliability, security, and performance.
In this guide, we’ll walk you through the process of setting up a MariaDB Galera Cluster on AlmaLinux, ensuring a robust database infrastructure capable of meeting high-availability requirements.
Table of Contents
- What is a Galera Cluster?
- Benefits of Using MariaDB Galera Cluster
- Prerequisites
- Installing MariaDB on AlmaLinux
- Configuring the First Node
- Adding Additional Nodes to the Cluster
- Starting the Cluster
- Testing the Cluster
- Best Practices for Galera Cluster Management
- Troubleshooting Common Issues
- Conclusion
1. What is a Galera Cluster?
A Galera Cluster is a synchronous multi-master replication solution for MariaDB. Unlike traditional master-slave setups, all nodes in a Galera Cluster are equal, and changes on one node are instantly replicated to the others.
Key features:
- High Availability: Ensures continuous availability of data.
- Scalability: Distributes read and write operations across multiple nodes.
- Data Consistency: Synchronous replication ensures data integrity.
2. Benefits of Using MariaDB Galera Cluster
- Fault Tolerance: If one node fails, the cluster continues to operate without data loss.
- Load Balancing: Spread database traffic across multiple nodes for improved performance.
- Real-Time Updates: Changes are immediately replicated to all nodes.
- Ease of Management: Single configuration for all nodes simplifies administration.
3. Prerequisites
Before proceeding, ensure the following:
- AlmaLinux Instances: At least three servers running AlmaLinux for redundancy.
- MariaDB Installed: The same version of MariaDB installed on all nodes.
- Network Configuration: All nodes can communicate with each other over a private network.
- Firewall Rules: Allow MariaDB traffic on the required ports:
- 3306: MariaDB service.
- 4567: Galera replication traffic.
- 4568: Incremental State Transfer (IST) traffic.
- 4444: State Snapshot Transfer (SST) traffic.
Update and configure all servers:
sudo dnf update -y
sudo hostnamectl set-hostname <hostname>
4. Installing MariaDB on AlmaLinux
Install MariaDB on all nodes:
Add the MariaDB Repository:
sudo dnf install -y https://downloads.mariadb.com/MariaDB/mariadb_repo_setup sudo mariadb_repo_setup --mariadb-server-version=10.11
Install MariaDB Server:
sudo dnf install -y mariadb-server
Enable and Start MariaDB:
sudo systemctl enable mariadb sudo systemctl start mariadb
Secure MariaDB: Run the security script:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, and disable remote root login.
5. Configuring the First Node
Edit the MariaDB Configuration File: Open the configuration file:
sudo nano /etc/my.cnf.d/galera.cnf
Add the Galera Configuration: Replace
<node_ip>
and<cluster_name>
with your values:[galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_name="my_galera_cluster" wsrep_cluster_address="gcomm://<node1_ip>,<node2_ip>,<node3_ip>" wsrep_node_name="node1" wsrep_node_address="<node1_ip>" wsrep_sst_method=rsync
Key parameters:
- wsrep_on: Enables Galera replication.
- wsrep_provider: Specifies the Galera library.
- wsrep_cluster_name: Sets the name of your cluster.
- wsrep_cluster_address: Lists the IP addresses of all cluster nodes.
- wsrep_node_name: Specifies the node’s name.
- wsrep_sst_method: Determines the synchronization method (e.g.,
rsync
).
Allow Galera Ports in the Firewall:
sudo firewall-cmd --permanent --add-port=3306/tcp sudo firewall-cmd --permanent --add-port=4567/tcp sudo firewall-cmd --permanent --add-port=4568/tcp sudo firewall-cmd --permanent --add-port=4444/tcp sudo firewall-cmd --reload
6. Adding Additional Nodes to the Cluster
Repeat the same steps for the other nodes, with slight modifications:
- Edit
/etc/my.cnf.d/galera.cnf
on each node. - Update the
wsrep_node_name
andwsrep_node_address
parameters for each node.
For example, on the second node:
wsrep_node_name="node2"
wsrep_node_address="<node2_ip>"
On the third node:
wsrep_node_name="node3"
wsrep_node_address="<node3_ip>"
7. Starting the Cluster
Bootstrap the First Node: On the first node, start the Galera Cluster:
sudo galera_new_cluster
Check the logs to verify the cluster has started:
sudo journalctl -u mariadb
Start MariaDB on Other Nodes: On the second and third nodes, start MariaDB normally:
sudo systemctl start mariadb
Verify Cluster Status: Log in to MariaDB on any node and check the cluster size:
SHOW STATUS LIKE 'wsrep_cluster_size';
Output example:
+--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+
8. Testing the Cluster
Create a Test Database: On any node, create a test database:
CREATE DATABASE galera_test;
Check Replication: Log in to other nodes and verify the database exists:
SHOW DATABASES;
9. Best Practices for Galera Cluster Management
Use an Odd Number of Nodes: To avoid split-brain scenarios, use an odd number of nodes (e.g., 3, 5).
Monitor Cluster Health: Use
SHOW STATUS
to monitor variables likewsrep_cluster_status
andwsrep_cluster_size
.Back Up Data: Regularly back up your data using tools like
mysqldump
ormariabackup
.Avoid Large Transactions: Large transactions can slow down synchronization.
Secure Communication: Use SSL/TLS to encrypt Galera replication traffic.
10. Troubleshooting Common Issues
Cluster Fails to Start
- Check Logs: Look at
/var/log/mariadb/mariadb.log
for errors. - Firewall Rules: Ensure required ports are open on all nodes.
Split-Brain Scenarios
Reboot the cluster with a quorum node as the bootstrap:
sudo galera_new_cluster
Slow Synchronization
- Use
rsync
orxtrabackup
for faster state snapshot transfers (SST).
11. Conclusion
Setting up a MariaDB Galera Cluster on AlmaLinux is a powerful way to achieve high availability, scalability, and fault tolerance in your database environment. By following the steps in this guide, you can create a robust multi-master replication cluster capable of handling both read and write traffic seamlessly.
With proper monitoring, backup strategies, and security configurations, your MariaDB Galera Cluster will provide a reliable and resilient foundation for your applications.