Set up MariaDB with SSL/TLS

Since MariaDB 11.4 TLS is on by default and the client verifies the server certificate. That is good, but it causes connection errors in older setups. With your own certificate you are back in control.

What changed with MariaDB 11.4

From 11.4 the server generates a certificate at startup if none is configured, and encrypts all non-local connections. The client verifies the server certificate by default. It can confirm a self-generated certificate via the user’s password, which works with the authentication methods mysql_native_password, ed25519 and parsec. With other methods, certificates from other CAs or older clients, the connection ends with:

ERROR 2026 (HY000): TLS/SSL error: Certificate verification failure

Set up your own certificate

sudo mkdir -p /etc/mysql/ssl
sudo cp server.crt server.key ca.crt /etc/mysql/ssl/
sudo chown -R mysql:mysql /etc/mysql/ssl
sudo chmod 600 /etc/mysql/ssl/server.key

In a file under /etc/mysql/mariadb.conf.d/ (Debian, Ubuntu) or /etc/my.cnf.d/ (RHEL):

[mariadb]
ssl_cert = /etc/mysql/ssl/server.crt
ssl_key  = /etc/mysql/ssl/server.key
ssl_ca   = /etc/mysql/ssl/ca.crt
tls_version = TLSv1.2,TLSv1.3

server.crt contains the certificate followed by the intermediates. If you have a PFX, the certificate converter splits it into PEM files. Then systemctl restart mariadb.

Check

SHOW VARIABLES LIKE 'have_ssl';          -- YES
SHOW VARIABLES LIKE 'ssl_%';
SHOW STATUS LIKE 'Ssl_version';           -- TLSv1.3
SHOW STATUS LIKE 'Ssl_cipher';

In the client, \s shows the line SSL: Cipher in use is …. From outside use openssl s_client -starttls mysql -connect db.example.com:3306 (OpenSSL 1.1.1 or later).

Enforce encryption

# for all connections (MariaDB 10.5.2 and later), in the server configuration
require_secure_transport = ON

-- or per user
ALTER USER 'app'@'%' REQUIRE SSL;
-- with client certificate
ALTER USER 'app'@'%' REQUIRE SUBJECT '/CN=app01';

Configure the client properly

mariadb -h db.example.com -u app -p --ssl-ca=/etc/ssl/certs/corp-ca.pem --ssl-verify-server-cert

The name after -h must be in the certificate. The same applies to applications: provide the CA and enable verification, e.g. sslMode=VERIFY_IDENTITY with the Java connector. Turning verification off (--disable-ssl-verify-server-cert) removes error 2026 but makes the connection vulnerable.

Renew the certificate without restart

FLUSH SSL;

From MariaDB 10.4 this reloads certificate and key; existing connections stay open. With Let's Encrypt it belongs in the deploy hook, after copying the files and setting permissions. In MySQL the command is ALTER INSTANCE RELOAD TLS.

FAQ

Do I need to do anything at all with MariaDB 11.4?

Not for connections between current MariaDB clients and servers with password login. As soon as applications with other drivers, older clients or checks against a fixed name come into play, your own certificate is better.

Does this apply to MySQL too?

The settings ssl_cert, ssl_key and ssl_ca are the same. MySQL also generates certificates in the data directory on first start, but they contain no server name. The client option there is --ssl-mode=VERIFY_IDENTITY.

And replication?

On the replica, set CHANGE MASTER TO … MASTER_SSL=1, MASTER_SSL_CA and MASTER_SSL_VERIFY_SERVER_CERT=1.

Sources and documentation