How to Set Up a MariaDB Cluster on Ubuntu
How to Set Up a MariaDB Cluster on Ubuntu
MariaDB is a popular, open-source relational database management system (RDBMS) that is a fork of MySQL. One of its most powerful features is the ability to set up a high-availability cluster. This guide walks you through setting up a MariaDB Cluster on an Ubuntu server. A cluster allows you to distribute data across multiple servers, offering load balancing, redundancy, and increased availability.
Prerequisites
Before diving into the setup, make sure you meet the following prerequisites:
- Ubuntu 20.04 or newer installed on all participating nodes.
- SSH access to each node.
- A non-root user with sudo privileges on all nodes.
- At least three servers for a production-grade Galera cluster.
- Proper firewall settings allowing necessary ports (3306 for MariaDB, 4567 for replication, 4444 for state transfer, and 4568 for Incremental State Transfer).
Step 1: Update Your System
Before installing any software, update your package lists and upgrade your system packages. Log in to each server and run:
sudo apt update && sudo apt upgrade -y
This ensures your system is up to date, which minimizes the chances of compatibility issues later in the process.
Step 2: Install MariaDB
The first step in setting up a MariaDB cluster is installing MariaDB on all the nodes. MariaDB’s repository provides the latest stable version, which may be newer than the default Ubuntu repositories.
Add the MariaDB repository:
sudo apt install software-properties-common
sudo add-apt-repository ‘deb [arch=amd64] http://mirror.stshosting.co.uk/mariadb/repo/10.5/ubuntu focal main’
Install the MariaDB server and client:
sudo apt update
sudo apt install mariadb-server mariadb-client -y
Repeat this process on each node in your cluster.
Step 3: Configure MariaDB for Clustering
To create a MariaDB cluster, the configuration file (/etc/mysql/my.cnf) on each node must be modified. Follow these steps:
Edit the configuration file:
sudo nano /etc/mysql/my.cnf
Add or modify the following settings under the [mysqld] section to enable Galera clustering:
[mysqld]
bind-address = 0.0.0.0
default-storage-engine = InnoDB
innodb_autoinc_lock_mode = 2
binlog_format = ROW
query_cache_type = 0
wsrep_on = ON
wsrep_provider = /usr/lib/galera/libgalera_smm.so
wsrep_cluster_address = “gcomm://NODE1_IP,NODE2_IP,NODE3_IP”
wsrep_cluster_name = “my_cluster”
wsrep_node_address = “NODE_IP”
wsrep_node_name = “NodeName”
wsrep_sst_method = rsync
Replace NODE1_IP, NODE2_IP, and NODE3_IP with the IP addresses of your nodes, and adjust the node-specific settings such as wsrep_node_address and wsrep_node_name accordingly.
Step 4: Allow Galera Ports in Firewall
Ensure that your firewall rules allow traffic on ports 3306, 4567, 4444, and 4568. You can use ufw to manage firewall settings.
sudo ufw allow 3306/tcp
sudo ufw allow 4567/tcp
sudo ufw allow 4568/tcp
sudo ufw allow 4444/tcp
Repeat this process on each node.
Step 5: Start the MariaDB Cluster
The first node in the cluster must be bootstrapped manually. To do this, run the following command on the first node:
sudo galera_new_cluster
On the remaining nodes, simply start the MariaDB service:
sudo systemctl start mariadb
If everything is configured correctly, the second and third nodes should automatically join the cluster.
Step 6: Verify the Cluster Setup
Once all nodes are running, you can verify the status of your cluster by logging into the MariaDB shell and running a query:
sudo mysql -u root -p
SHOW STATUS LIKE ‘wsrep_cluster_size’;
This query will show the number of nodes in the cluster. The result should match the number of nodes you’ve configured (e.g., three if you have three nodes).
Step 7: Testing the Cluster
To test if the cluster is working as expected, create a sample database on one node and check if it replicates across the other nodes.
On one node, log in to MariaDB and create a test database:
CREATE DATABASE testdb;
Then, log in to MariaDB on another node and check if the testdb database exists:
SHOW DATABASES;
If the database is present on the second node, replication is working as expected.
Step 8: Configure High Availability (Optional)
For added redundancy and failover, you can configure a load balancer (like HAProxy) to distribute traffic across the nodes in the MariaDB cluster. This ensures that even if one node fails, the others continue to serve traffic.
Troubleshooting Tips
- Check the MariaDB logs: If you encounter issues during setup, the first place to look is the logs. The log files can be found at /var/log/mysql/error.log.
- Firewall issues: Ensure all the necessary ports are open, and there are no network issues between the nodes.
- Node desynchronization: If a node gets out of sync, restart it, and it should resynchronize with the cluster automatically.
Thank you for visiting our page! If you’re interested in reading more articles about deploying Docker on Linux systems, feel free to check out the links below.
How to Use Docker to Deploy WordPress on Ubuntu
Moreover, by renting a server from our site, you can conduct your tests without limitations in a reliable and effective environment, helping you to enhance your skills more rapidly. Great job! 🙂