Of course. Here is a comprehensive, step-by-step guide to enable SSL/TLS encryption for replication traffic between two MariaDB nodes in a Galera cluster using self-signed certificates with a 10-year validity.
Overview
We will perform the following steps:
- Create a Certificate Authority (CA): We'll act as our own CA to sign the server certificates. This is a best practice even for self-signed setups.
- Generate Server Certificates: Create a unique certificate and private key for each MariaDB node, signed by our CA.
- Distribute Certificates: Securely copy the necessary certificate files to each database node.
- Configure MariaDB: Update the
my.cnf configuration on each node to enable and require SSL for Galera replication.
- Restart and Verify: Safely restart the cluster and verify that replication traffic is now encrypted.
Prerequisites
- A working 2-node MariaDB Galera cluster. Let's assume the nodes are named
db1 and db2.
sudo or root access on both nodes.
openssl command-line tool installed (it's usually installed by default).
- Important Note: A 2-node Galera cluster is not recommended for production as it cannot handle network partitions safely (split-brain). A minimum of 3 nodes is required for a highly available setup. However, the SSL configuration process is the same.
Step 1: Generate Certificates
This process only needs to be done on one machine. It can be one of the DB nodes or a separate admin workstation.
First, create a dedicated directory to work in.
mkdir ~/galera-ssl
cd ~/galera-ssl
A. Create the Certificate Authority (CA)
The CA will be used to sign the certificates for each of our database servers. This establishes a chain of trust.
# 1. Generate the CA private key
openssl genrsa -out ca-key.pem 4096
# 2. Generate the CA's self-signed certificate (valid for 10 years = 3650 days)
openssl req -new -x509 -nodes -days 3650 -key ca-key.pem -out ca.pem
When prompted for the CA certificate details, you can fill them out as you see fit. The "Common Name" is important; make it something descriptive like Galera Cluster CA.
B. Generate Server Certificate for db1
Now, we'll create a private key and a certificate signing request (CSR) for our first node, db1.
Important: The Common Name (CN) must match the hostname or IP address that db2 will use to connect to db1. Using hostnames is recommended.
# 1. Generate the server's private key
openssl genrsa -out db1-server-key.pem 4096
# 2. Create a Certificate Signing Request (CSR) for db1
# When prompted for "Common Name", enter the hostname or IP of db1.
# Example: db1.yourdomain.com
openssl req -new -key db1-server-key.pem -out db1-server-req.pem
# 3. Sign the server's CSR with our CA key (valid for 10 years)
openssl x509 -req -in db1-server-req.pem -days 3650 -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out db1-server-cert.pem
C. Generate Server Certificate for db2
Repeat the same process for the second node, db2.
Important: The Common Name (CN) must match the hostname or IP address that db1 will use to connect to db2.
# 1. Generate the server's private key
openssl genrsa -out db2-server-key.pem 4096
# 2. Create a Certificate Signing Request (CSR) for db2
# When prompted for "Common Name", enter the hostname or IP of db2.
# Example: db2.yourdomain.com
openssl req -new -key db2-server-key.pem -out db2-server-req.pem
# 3. Sign the server's CSR with our CA key (valid for 10 years)
openssl x509 -req -in db2-server-req.pem -days 3650 -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out db2-server-cert.pem
At this point, your ~/galera-ssl directory should contain these important files:
ca.pem (CA certificate)
db1-server-cert.pem, db1-server-key.pem
db2-server-cert.pem, db2-server-key.pem
Step 2: Distribute the Certificates
Now, we need to securely copy the relevant files to each MariaDB node and set strict permissions.
# On both db1 and db2, create the SSL directory
sudo mkdir -p /etc/mysql/ssl
sudo chown -R mysql:mysql /etc/mysql/ssl
sudo chmod 700 /etc/mysql/ssl
On db1:
Copy the following files from your certificate generation machine to db1.
# From your admin machine, run:
scp ~/galera-ssl/ca.pem user@db1:/tmp/
scp ~/galera-ssl/db1-server-cert.pem user@db1:/tmp/
scp ~/galera-ssl/db1-server-key.pem user@db1:/tmp/
# Now, SSH into db1 and move them to the final location with correct permissions
ssh user@db1
sudo mv /tmp/ca.pem /etc/mysql/ssl/
sudo mv /tmp/db1-server-cert.pem /etc/mysql/ssl/server-cert.pem
sudo mv /tmp/db1-server-key.pem /etc/mysql/ssl/server-key.pem
sudo chown mysql:mysql /etc/mysql/ssl/*
sudo chmod 600 /etc/mysql/ssl/*-key.pem # Private keys must be protected
sudo chmod 644 /etc/mysql/ssl/*.pem # Certificates can be world-readable
On db2:
Copy the following files from your certificate generation machine to db2.
# From your admin machine, run:
scp ~/galera-ssl/ca.pem user@db2:/tmp/
scp ~/galera-ssl/db2-server-cert.pem user@db2:/tmp/
scp ~/galera-ssl/db2-server-key.pem user@db2:/tmp/
# Now, SSH into db2 and move them to the final location with correct permissions
ssh user@db2
sudo mv /tmp/ca.pem /etc/mysql/ssl/
sudo mv /tmp/db2-server-cert.pem /etc/mysql/ssl/server-cert.pem
sudo mv /tmp/db2-server-key.pem /etc/mysql/ssl/server-key.pem
sudo chown mysql:mysql /etc/mysql/ssl/*
sudo chmod 600 /etc/mysql/ssl/*-key.pem # Private keys must be protected
sudo chmod 644 /etc/mysql/ssl/*.pem # Certificates can be world-readable
Notice we rename the files to server-cert.pem and server-key.pem on each node for configuration consistency.
Step 3: Configure MariaDB
On both db1 and db2, you need to edit your MariaDB configuration file. This is often located at /etc/mysql/my.cnf or /etc/mysql/mariadb.conf.d/50-server.cnf.
We will add two sets of SSL parameters:
[mysqld] section: For standard client-to-server connections (optional but recommended).
wsrep_provider_options: For Galera's node-to-node replication traffic (the core of your request).
Edit the configuration file on db1 and db2:
[mysqld]
# ... other mysqld settings ...
# SSL for client connections
ssl-ca = /etc/mysql/ssl/ca.pem
ssl-cert = /etc/mysql/ssl/server-cert.pem
ssl-key = /etc/mysql/ssl/server-key.pem
[galera]
# ... other galera settings like wsrep_on, wsrep_cluster_address, etc. ...
# Add SSL options for Galera replication
wsrep_provider_options="...other_options...; socket.ssl=YES; socket.ssl_key=/etc/mysql/ssl/server-key.pem; socket.ssl_cert=/etc/mysql/ssl/server-cert.pem; socket.ssl_ca=/etc/mysql/ssl/ca.pem; socket.ssl_cipher=AES128-SHA"
Explanation of the wsrep_provider_options:
- You must append the SSL options to your existing
wsrep_provider_options, separated by semicolons.
socket.ssl=YES: Enables SSL. For production, you might change this to REQUIRED to strictly enforce SSL and prevent any unencrypted fallback.
socket.ssl_key: Path to the node's private key.
socket.ssl_cert: Path to the node's certificate.
socket.ssl_ca: Path to the Certificate Authority certificate. This is used to verify the certificate presented by the connecting node.
socket.ssl_cipher: (Optional but recommended) Defines the cipher suite to use.
Step 4: Restart the Galera Cluster (Rolling Restart)
A full cluster restart is required for these changes to take effect. Do not restart both nodes at the same time.
-
Stop MariaDB on db2:
sudo systemctl stop mariadb
-
Stop MariaDB on db1:
sudo systemctl stop mariadb
-
Bootstrap the cluster from db1:
This node will be the first one to form the new, SSL-enabled cluster.
sudo galera_new_cluster
Check its status and logs to ensure it started without errors.
sudo journalctl -u mariadb -f
-
Start MariaDB on db2:
This node will now join the cluster formed by db1, using an SSL-encrypted connection.
sudo systemctl start mariadb
Check its logs to confirm it successfully joined the cluster.
Step 5: Verify the SSL Connection
Once both nodes are running and synced, log in to the MariaDB client on either node and run these commands.
-
Check Galera Provider Options:
This is the most direct way to confirm Galera is using SSL.
SHOW STATUS LIKE 'wsrep_provider_options';
The output should contain all the socket.ssl_* parameters you configured.
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| wsrep_provider_options | ... socket.ssl=YES; socket.ssl_key=/etc/mysql/ssl/server-key.pem; socket.ssl_cert=/etc/mysql/ssl/server-cert.pem; socket.ssl_ca=/etc/mysql/ssl/ca.pem ... |
+------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-
Check Cluster Status:
Ensure the cluster is healthy and both nodes are part of it.
SHOW STATUS LIKE 'wsrep_cluster_size';
-- Should return 2
SHOW STATUS LIKE 'wsrep_local_state_comment';
-- Should return 'Synced' on both nodes
-
Check SSL Cipher for Client Connections:
This verifies the [mysqld] section SSL settings are active for your own connection.
-- First, connect using the --ssl-ca flag
-- mariadb -u root -p --ssl-ca=/etc/mysql/ssl/ca.pem
-- Then run this command inside MariaDB
SHOW STATUS LIKE 'Ssl_cipher';
If the Value is not empty (e.g., AES128-SHA), your client connection is encrypted.
You have now successfully enabled encrypted replication between your two Galera nodes.