Calíope blog

How to encrypt the connection to MariaDB and MySQL with TLS certificates, and why a strong password isn't enough

SecurityMariaDBMySQL

Translated with AI for everyone’s convenience; the original language is Spanish. Read the original

“It is easy to be wise after the event.” Sherlock Holmes, in “The Problem of Thor Bridge” (1922).

Today this seems obvious to me, but it took me years to realize it. For a long time I thought that, to connect securely to a database server, a username and a really strong password were enough. The rest wasn't my business.

I understood it during my master's degree, in a class on security. We were talking about how important it is for us database administrators to change our passwords regularly, because it's our responsibility. And I was left thinking: I can change the password every month, but I type it on my computer and it has to reach the server. How does it travel there? That's where I started pulling the thread.

What travels over the network

The client and the server talk over TCP, and if nobody configures anything else, what they say to each other crosses the network as is. Anyone on the path can read it: someone on the same Wi-Fi network, a compromised piece of network equipment, the data center provider. That's called sniffing, and it takes nothing special: tcpdump or Wireshark is enough.

I tried it with a throwaway MariaDB. I told the client not to encrypt (--skip-ssl), connected with the user david and a really long password, and queried a customers table. Meanwhile, on the client's own machine, I was capturing the traffic:

tcpdump -i eth0 -s0 -A port 3306

This is part of what showed up in the capture:

david..Z3+.PN....~6........mysql_native_password
SELECT nombre, tarjeta FROM ventas.clientes
...."......Ana Mora.4111111111111111

The first line has my username. The query is there in full, and so is the answer: the customer's name and her card number.

The password, on the other hand, doesn't appear. With the usual authentication method (mysql_native_password), the server sends a random challenge, and the client answers with a calculation that mixes that challenge with the password. The password never travels, but that calculation does, and whoever captures it can try passwords on their own machine until they find the one that produces the same answer, without the server ever noticing. A long password makes that slower. It doesn't make it impossible.

And there are engines, or authentication methods, in which the password does travel in plain text: I cover that at the end.

So my question from the master's had two answers, and the second was worse than the first. The password traveled more or less protected. Everything else, which is precisely what the password protects, traveled with no protection at all.

Encrypting the connection doesn't encrypt the data

Before going on, a distinction that took me a while. What I'm about to describe encrypts the communication: what goes from my computer to the server and what comes back. The data on the server's disk stays as it was, and so does the data once it reaches my computer. If someone walks off with the server's files, they can read them just the same. Encrypting stored data is another topic, with other tools.

Three different things

For years I confused three things that are not the same:

  • Encrypting the connection. For this, it's enough for the server to have a certificate and its private key. The client needs nothing.
  • Checking that the server is mine. Encrypting without checking leaves a door open: someone in the middle can present their own certificate to the client, receive the encrypted connection, read it and pass it on to the real server. To prevent that, the client keeps the certificate of the authority that signed the server's, and verifies it when connecting.
  • Having the server recognize me. If I also give the client a certificate signed by the same authority, the server can require it, and then the password alone is no longer enough to get in.

Encrypting is the essential part. Checking the server is what almost always gets forgotten. Being recognized is for the accounts that need the most care.

Today it comes half switched on

This has changed over the years, and it's worth knowing what your version does:

  • MySQL, since 5.7, generates its own certificates at startup if it finds none, and the client tries to encrypt by default. The connection is encrypted, but the client checks nothing: the certificate is self-signed and nobody verifies it. Checking the server remains unsolved.
  • MariaDB, since 11.4, also generates a certificate at startup, and its client does check it, with a clever trick: after authenticating, the server proves to it that it knows the password and that nobody in the middle swapped the certificate. It works with mysql_native_password and with ed25519. It doesn't work with PAM or GSSAPI, or with a user without a password.
  • MariaDB before 11.4: if nobody configured certificates, the connection traveled unencrypted.

So why create your own certificates? Because MariaDB's automatic certificate lives in memory and changes on every restart, because not all connectors (JDBC, Python, ODBC) do the password check, and because without your own authority the server can't recognize me by my certificate. And because I'd rather know which certificate my server has than let it decide.

Step by step in MariaDB

1. The certificates

You need three key and certificate pairs: one for your own authority (the CA), one for the server and one for the client. I created them with OpenSSL 3.5 on Linux:

openssl genrsa -out ca-key.pem 4096
openssl req -x509 -new -key ca-key.pem -sha256 -days 3650 -subj "/CN=CA de mis servidores" -out ca.pem

openssl genrsa -out server-key.pem 2048
openssl req -new -key server-key.pem -subj "/CN=db.ejemplo.lan" -out server-req.pem
echo "subjectAltName=DNS:db.ejemplo.lan" > server-ext.cnf
openssl x509 -req -in server-req.pem -CA ca.pem -CAkey ca-key.pem -CAcreateserial \
    -days 825 -sha256 -extfile server-ext.cnf -out server-cert.pem

openssl genrsa -out client-key.pem 2048
openssl req -new -key client-key.pem -subj "/CN=david" -out client-req.pem
openssl x509 -req -in client-req.pem -CA ca.pem -CAkey ca-key.pem -CAcreateserial \
    -days 825 -sha256 -out client-cert.pem

openssl verify -CAfile ca.pem server-cert.pem client-cert.pem

Three details that matter:

  • The server name (db.ejemplo.lan) has to be the same one the clients connect with, and it goes in the subjectAltName, which is where current clients look for it. When I connected by IP address instead of by name, the client refused: ERROR 2026 (HY000): TLS/SSL error: Hostname verification failed. That's exactly what it should do.
  • ca-key.pem doesn't go on the server. It's the key that signs everything else: whoever has it can make certificates my servers will accept. I keep it off the network.
  • Certificates expire. I gave the server's and the client's 825 days, and the CA ten years. The lesson from the master's applies here too: just like passwords, certificates get rotated, and it's worth putting their expiry date in the calendar before some Monday morning nobody can connect.

2. The server

I copy ca.pem, server-cert.pem and server-key.pem to the server, into a folder readable by the user MariaDB runs as, with the key readable only by that user. Then, in the configuration:

[mariadb]
ssl_ca   = /etc/mysql/ssl/ca.pem
ssl_cert = /etc/mysql/ssl/server-cert.pem
ssl_key  = /etc/mysql/ssl/server-key.pem
require_secure_transport = ON

require_secure_transport (since 10.5.2) makes the server reject every network connection that isn't encrypted. With that, the same client that earlier connected with --skip-ssl now gets this:

ERROR 3159 (08004): Connections using insecure transport are prohibited while --require_secure_transport=ON.

After restarting, I check that the server has TLS:

SHOW VARIABLES LIKE 'have_ssl';
Runs onMariaDB

It has to say YES. And when it's time to renew the certificates, there's no need to restart: you replace the files and run FLUSH SSL.

3. The users

require_secure_transport applies to the whole server. If I can't switch it on yet, because some old application doesn't know how to encrypt, I can require it account by account. And this is where the client certificate comes in:

CREATE USER 'informes'@'%' IDENTIFIED BY 'Una-Clave-Bien-Larga-2026' REQUIRE SSL;

ALTER USER 'informes'@'%' REQUIRE SUBJECT '/CN=informes' AND ISSUER '/CN=CA de mis servidores';
Runs onMariaDBMySQL
  • REQUIRE SSL forces that account to connect encrypted.
  • REQUIRE SUBJECT … AND ISSUER … also requires a client certificate with that name, signed by my CA. There's a shorter form, REQUIRE X509, which accepts any certificate signed by a CA the server trusts.

I set that same requirement on my user david, with /CN=david, and connected with the right password but without the certificate. I was locked out, with the same message as if the password were wrong:

ERROR 1045 (28000): Access denied for user 'david'@'172.19.0.3' (using password: YES)

4. Connecting

From the client, with the three files:

mariadb -h db.ejemplo.lan -u david -p \
    --ssl-ca=ca.pem --ssl-verify-server-cert \
    --ssl-cert=client-cert.pem --ssl-key=client-key.pem

To avoid typing it every time, it goes in the client's option file (~/.my.cnf):

[client-mariadb]
ssl-ca   = /ruta/a/ca.pem
ssl-cert = /ruta/a/client-cert.pem
ssl-key  = /ruta/a/client-key.pem
ssl-verify-server-cert

Once connected, the session tells you whether it's encrypted:

SHOW SESSION STATUS LIKE 'Ssl_version';
Runs onMariaDBMySQL
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| Ssl_version   | TLSv1.3 |
+---------------+---------+

Empty means it's unencrypted. The client's status command says it even more clearly: SSL: Cipher in use is TLS_AES_256_GCM_SHA384, cert is OK.

And the test that really convinced me: I repeated the same capture as before, with the encrypted connection, and searched it for my username, the query and the card number. None of them showed up.

In MySQL

The same certificates work, and almost everything has the same name. Three things change:

  • The configuration section is [mysqld], with the same variables ssl_ca, ssl_cert, ssl_key and require_secure_transport.
  • The client doesn't have --ssl-verify-server-cert: it uses --ssl-mode. The default is PREFERRED, which encrypts if it can and checks nothing; to check the certificate and the server name you use --ssl-mode=VERIFY_IDENTITY together with --ssl-ca.
  • Authentication by default since 8.0 is caching_sha2_password, which doesn't send the password in the clear either: without TLS, when it has to send it, it encrypts it with the server's public key.

REQUIRE SSL, REQUIRE X509 and REQUIRE SUBJECT … AND ISSUER … are written the same. I tested it with MySQL 8.4 and the same files.

And in the other engines?

The idea is the same in all of them: a certificate on the server, the CA on the client to check it and, optionally, a client certificate. What changes is where it's configured, what comes switched on and, above all, what happens to the password when there's no encryption:

  • PostgreSQL: ssl = on and the files in postgresql.conf; to require it, the hostssl lines in pg_hba.conf. Clients, by default (sslmode=prefer), encrypt if they can and don't check, like MySQL; what checks is sslmode=verify-full. With SCRAM, the default method since 14, the password doesn't travel.
  • SQL Server got there first: it always encrypts the login packet, even if it has to invent a certificate. What comes after depends on the client, and Microsoft's current drivers encrypt and check by default.
  • Oracle has two paths: its native network encryption, configured in sqlnet.ora and not using certificates, or TLS (TCPS) with certificates kept in a wallet.
  • ClickHouse listens for encrypted traffic on separate ports (9440 for native and 8443 for HTTPS). On the unencrypted ports, the password travels in plain text.
  • MongoDB: net.tls.mode: requireTLS and its files. Authentication (SCRAM) doesn't send the password, but documents travel in the clear without TLS.
  • Redis and Valkey support TLS since Redis 6, on a port declared separately. The AUTH command sends the password in plain text. And here the default is the reverse: if you switch TLS on, by default it requires a client certificate.
  • Cassandra and ScyllaDB: client_encryption_options in their configuration file. With username and password, credentials travel in plain text: here encrypting isn't optional.
  • Elasticsearch, since 8, configures TLS on its own on first startup.
  • TiDB speaks the MySQL protocol: on the client side, everything about MySQL applies the same.
  • SQLite and DuckDB don't use the network: the database is a file. What applies to them is encrypting the disk.

In Calíope, encryption is switched on with the “SSL / TLS” toggle in the connection form, with the client certificates. How to set it up is in the Help: Configure SSL/TLS.

The job doesn't end at the engine

Many of us DBAs believe our job ends at the database engine: users, permissions, backups, performance. What happens between the server and whoever connects to it seems like someone else's job, the person who runs the network. But the data is our responsibility from the moment it leaves the disk until it reaches the screen of whoever asked for it, and in between there's a network we don't control.

That day, during my master's, changing the password every month seemed enough to me. Today I know that a new password is of little use if everything it protects travels in plain sight.

All entries