Configuring Your MySQL Server for Mutual TLS

How to use TLS, client authentication, and CA certificates in MySQL

Create a private key and request a certificate for your MySQL server

Before you can teach your server to speak TLS, you will need a certificate issued by a trusted certificate authority (CA). If your organization already runs its own CA and you have a private key and certificate for your MySQL server, along with your CA's root certificate, you can skip to the next step.

To request a certificate from your CA using the step CLI, bootstrap your CA with step ca bootstrap and run the following command (sub the server name for the actual name / DNS name of your MySQL server).

Since you're using MySQL, which requires RSA keys, we'll pass the --kty=RSA flag to let step know to generate RSA keys instead of the default EC type.

$ step ca certificate --kty=RSA "" server.crt server.key

Your certificate and private key will be saved in server.crt and server.key respectively.

Request a copy of your CA root certificate, which will be used to make sure each application can trust certificates presented by other applications.

$ step ca root ca.crt

Your certificate will be saved in ca.crt.

Configure MySQL to authenticate itself with its TLS certificate

We now want to instruct our MySQL server to identify itself using the certificate issued in the last step and to force clients to connect over TLS.

Copy the server.crt, server.key, and ca.crt files to their expected locations in the MySQL data directory. The data directory is often at /var/lib/mysql but can be discovered by querying select @@datadir; as your MySQL root user.

$ sudo cp server.crt /var/lib/mysql/server-cert.pem $ sudo cp server.key /var/lib/mysql/server-key.pem $ sudo cp ca.crt /var/lib/mysql/ca.pem $ sudo chown msyql:mysql /var/lib/mysql/server-{cert,key}.pem /var/lib/mysql/ca.pem

Placing certificates and keys in those file paths should automatically signal to MySQL to allow secure TLS connections, but they can also be configured explicitly. Additionally, we'll need to configure require_secure_transport in my.cnf to require that TCP clients connect over TLS.


Restart your MySQL server for these changes to take effect.

An important note

For additional security, step certificates are signed by an intermediate CA by default rather than the root CA. The intermediate certificate is bundled into your server.crt file.

Unfortunately, MySQL has an outstanding bug that prevents it from being able to read the intermediate certificate out of server.crt. So, when opening a connection from a MySQL client, we'll have to instead bundle that intermediate CA certificate together with the root CA certificate we saved previously (ca.crt) and pass it into our client so that MySQL can read it and verify the server certificate that was signed by your CA's intermediate. This is covered in the Hello mTLS MySQL client docs.

Configure MySQL to require clients to authenticate with a certificate issued by your CA

To tell MySQL to use mutual TLS and not just one-way TLS, we must instruct it to require client authentication to ensure clients present a certificate from our CA when they connect.

MySQL requires client certificates on a per-user basis. The requirement can be configured using CREATE USER or ALTER USER statements. When set, MySQL will reject connections from these users if they don't present a valid certificate signed by your CA.

mysql> CREATE USER 'myuser'@'myhost' REQUIRE SUBJECT 'CN=myuser';
mysql> ALTER USER 'myuser'@'myhost' REQUIRE SUBJECT 'CN=myuser';

That's it! MySQL should now be able to receive TLS connections from clients who authenticate themselves using a certificate issued by your trusted CA.

Automate certificate renewal

By default, step-ca issues certificates with a 24 hour expiration. Short-lived certificates have many benefits but also require that you renew your certificates each day before they expire. How you renew certificates is often dependent on how you deploy your application. See the step-ca certificate lifecycle management docs for more information.

All documentation content from the Hello mTLS project is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Creative Commons License

Connect to Your MySQL Server from a Client