Prerequisites
Before starting, ensure you have:
192.168.1.11, 192.168.1.12, 192.168.1.13).3306 (MariaDB), 4567 (Galera sync), 4568 (IST), 4444 (SST)).ntp or chrony on all nodes).Step 1: Prepare All Nodes
Update packages and install dependencies:
sudo apt update && sudo apt upgrade -y
sudo apt install -y rsync galera-4 mariadb-server
rsync: Ensures data consistency during node synchronization.galera-4: Galera provider library for multi-master replication.mariadb-server: Installs MariaDB with Galera support (bundled since MariaDB 10.1+).Secure the installation:
sudo mysql_secure_installation
Follow prompts to set a root password, remove anonymous users, disable remote root login, and delete the test database.
Step 2: Configure the First Node (Primary)
Create a Galera config file (/etc/mysql/conf.d/galera.cnf) on the first node:
sudo nano /etc/mysql/conf.d/galera.cnf
Add the following (replace IPs with your node addresses):
[mysqld]
# Basic settings
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address=0.0.0.0
skip-name-resolve
# Galera provider (mandatory)
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Cluster configuration (name must be identical across nodes)
wsrep_cluster_name="my_galera_cluster"
wsrep_cluster_address="gcomm://192.168.1.11,192.168.1.12,192.168.1.13"
# Node identification
wsrep_node_address="192.168.1.11" # IP of this node
wsrep_node_name="node1" # Unique name for this node
# SST (State Snapshot Transfer) method (choose one: rsync, xtrabackup, mariabackup)
wsrep_sst_method=rsync
Save and exit. This config enables Galera replication and defines the cluster topology.
Step 3: Initialize the Cluster
Stop MariaDB on all nodes:
sudo systemctl stop mariadb
Only on the first node, start it with the --wsrep-new-cluster flag to bootstrap the cluster:
sudo systemctl start mariadb --wsrep-new-cluster
Verify the first node is running:
sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
You should see wsrep_cluster_size: 1 (indicating the primary node is up).
Step 4: Add Remaining Nodes
On each additional node (e.g., node2, node3), edit /etc/mysql/conf.d/galera.cnf with its own IP and name:
[mysqld]
# ... (same basic settings as node1) ...
wsrep_cluster_name="my_galera_cluster"
wsrep_cluster_address="gcomm://192.168.1.11,192.168.1.12,192.168.1.13"
wsrep_node_address="192.168.1.12" # IP of this node
wsrep_node_name="node2" # Unique name
wsrep_sst_method=rsync
Start MariaDB on each node normally:
sudo systemctl start mariadb
Check cluster status from any node:
sudo mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
The output should show wsrep_cluster_size: 3 (all nodes joined).
Step 5: Validate Replication
On node1, create a test database and table:
sudo mysql -u root -p -e "CREATE DATABASE test_cluster;"
sudo mysql -u root -p -e "USE test_cluster; CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50)); INSERT INTO users (name) VALUES ('Alice');"
On node2 and node3, verify the data was replicated:
sudo mysql -u root -p -e "USE test_cluster; SELECT * FROM users;"
You should see the Alice record on all nodes, confirming successful replication.
Step 6: Secure Remote Access (Optional but Recommended)
Edit the MariaDB config (/etc/mysql/mariadb.conf.d/50-server.cnf) on all nodes to allow remote connections:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
Comment out the bind-address line (or change it to 0.0.0.0):
#bind-address = 127.0.0.1
Restart MariaDB:
sudo systemctl restart mariadb
Grant remote access to the root user (run on any node):
sudo mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_strong_password'; FLUSH PRIVILEGES;"
Update your firewall to allow MariaDB (3306) and Galera (4567, 4568, 4444) ports:
sudo ufw allow 3306/tcp
sudo ufw allow 4567/tcp # Galera sync
sudo ufw allow 4568/tcp # Incremental state transfer
sudo ufw allow 4444/tcp # State snapshot transfer
Troubleshooting Tips
sudo journalctl -u mariadb -f) for errors. Common issues include mismatched cluster names/IPs or firewall blocking ports.wsrep_cluster_address includes all node IPs and wsrep_node_address is correct.wsrep_provider path and wsrep_sst_method (e.g., rsync requires write permissions on the target node).For production, consider adding monitoring (e.g., Prometheus + Grafana) and high availability (e.g., HAProxy for load balancing) to your cluster.