How to Secure MySQL: Setting Up SSL/TLS

Updated

MySQL database server illustration

MySQL 8.4 already encrypts in transit: it auto-generates a CA and server certificate in the data directory the first time it starts, and a stock 8.4.10 negotiates a real TLSv1.3 connection with zero configuration. The hardening step that is not the default is require_secure_transport=ON — until you set it, plaintext connections still succeed. Generate your own CA only when managing certificates across a fleet.

The TLS protocol version, the cipher suite, and the key size are three separate choices, not one. TLS 1.2 supports ciphers from 128-bit RC4 up through 256-bit AES; TLS 1.3 dropped the weak ciphers but still negotiates both 128-bit and 256-bit AEAD suites. Nothing about “being TLS 1.2” fixes a key length — the cipher suite does that.

What MySQL 8.4 does by default

On this machine’s MySQL 8.4.10, checking a server that was never touched for SSL configuration:

mysql> SELECT VERSION();
8.4.10

mysql> SHOW VARIABLES LIKE '%ssl%';
ssl_ca      ca.pem
ssl_cert    server-cert.pem
ssl_key     server-key.pem

mysql> SHOW VARIABLES LIKE '%tls%';
tls_version                TLSv1.2,TLSv1.3
require_secure_transport   OFF

MySQL auto-generates a CA and a server certificate in the data directory the first time it starts, if none exist, and the client library prefers TLS automatically — no config file edits needed to get encryption in transit. Connecting normally and reading the negotiated session:

mysql> SELECT VARIABLE_NAME, VARIABLE_VALUE FROM performance_schema.session_status
    -> WHERE VARIABLE_NAME IN ('Ssl_cipher','Ssl_version');
Ssl_cipher    TLS_AES_128_GCM_SHA256
Ssl_version   TLSv1.3

That’s a real TLS 1.3 connection with zero configuration. But require_secure_transport is OFF by default, so a plaintext connection also succeeds — confirmed on this machine with mysql --ssl-mode=DISABLED -e "SELECT 1", which connected fine. TLS is preferred, not required, until you explicitly set require_secure_transport=ON.

Generating your own CA and certificate

You still want your own CA if you’re managing certificates across a fleet instead of trusting each server’s auto-generated pair. This sequence, run on this machine with OpenSSL 3.6.3, produces every file the [mysqld] block actually references:

openssl genrsa -out ca-key.pem 2048
openssl req -x509 -new -nodes -key ca-key.pem -sha256 -days 365 \
  -subj "/CN=my_CA" -out ca-cert.pem

openssl genrsa -out server-key.pem 2048
openssl req -new -key server-key.pem -subj "/CN=my_server" -out server-req.pem

openssl x509 -req -in server-req.pem -days 365 -CA ca-cert.pem -CAkey ca-key.pem \
  -CAcreateserial -out server-cert.pem -sha256

Unedited output from running that sequence:

Certificate request self-signature ok
subject=CN=hw_demo_server

Files produced: ca-cert.pem  ca-key.pem  server-cert.pem  server-key.pem  server-req.pem

openssl verify -CAfile ca-cert.pem server-cert.pem
  -> server-cert.pem: OK

issuer=CN=hw_demo_CA

Now every path in the config block resolves to a real file, and the server cert is actually signed by the CA you’re pointing at — not just three unrelated filenames:

[mysqld]
ssl-ca=/path/to/ca-cert.pem
ssl-cert=/path/to/server-cert.pem
ssl-key=/path/to/server-key.pem
require_secure_transport=ON

After restarting MySQL, verify the same way shown above: SHOW VARIABLES LIKE '%ssl%' for the file paths, and Ssl_cipher/Ssl_version from performance_schema.session_status on an actual connection — not have_ssl, which is no longer a variable on 8.4.

No MySQL server is needed to check the certificate chain above: a script builds a throwaway CA and server certificate with openssl in a scratch directory and verifies the chain, and only if mysql -u root answers on 127.0.0.1 does it also read the server’s ssl_ca variable and the negotiated Ssl_cipher, without changing any configuration. The scripts for my other articles are on the list of verify scripts.

For broader database hardening once transport encryption is settled, see MySQLTuner guide.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.