Configuring SSL/TLS for Production MySQL: Creating OpenSSL Certificates and Enforcing REQUIRE SSL

MySQL tutorial - IT technology blog
MySQL tutorial - IT technology blog

Why Do You Still Need SSL/TLS on a Private Network?

Try running tcpdump on your internal network while debugging a database, and you will see all SQL queries and sensitive data traveling across the wire in plaintext. Many people assume placing MySQL inside a private VPC is secure enough. In reality, it is not that simple. If even a single service in the same subnet is compromised, an attacker can immediately start sniffing traffic or launch a Man-in-the-Middle (MITM) attack.

SSL/TLS encrypts the entire data stream between your application and the MySQL server. In terms of performance, CPUs with AES-NI support combined with TLS 1.3 add only about 1–3% CPU overhead. The handshake adds 1 RTT (roughly 0.5–2 ms on a LAN), which is negligible when your application uses a connection pool.

Note before touching Production: Any changes to my.cnf and MySQL restarts carry a risk of downtime. Verify your latest backup and make sure you can restore from it before getting started.

Generating SSL/TLS Certificates with OpenSSL

First, log in to MySQL to check whether the server already supports SSL:

SHOW VARIABLES LIKE '%ssl%';

If have_ssl returns YES or DISABLED, your MySQL build is already compiled with OpenSSL support. Instead of using mysql_ssl_rsa_setup (which was removed in MySQL 8.4), generate your certificates manually with OpenSSL. This gives you full control over validity periods, algorithms, and SAN (Subject Alternative Name) entries.

Create the /etc/mysql/ssl directory and run the following commands in order:

sudo mkdir -p /etc/mysql/ssl
cd /etc/mysql/ssl

# 1. Create the CA (Certificate Authority) key and certificate (valid for 10 years)
sudo openssl genrsa -out ca-key.pem 2048
sudo openssl req -new -x509 -nodes -days 3650 \
  -key ca-key.pem -out ca.pem \
  -subj "/CN=MySQL-Production-CA"

# 2. Create the Server key and CSR (valid for 3 years)
sudo openssl req -newkey rsa:2048 -days 1095 -nodes \
  -keyout server-key.pem -out server-req.pem \
  -subj "/CN=mysql-prod.internal"

# Sign the Server certificate with SAN so VERIFY_IDENTITY mode works
sudo openssl x509 -req -in server-req.pem -days 1095 \
  -CA ca.pem -CAkey ca-key.pem -set_serial 01 \
  -extfile <(printf "subjectAltName=DNS:mysql-prod.internal,IP:10.0.1.10") \
  -out server-cert.pem

# 3. Create the Client key and certificate (for mTLS if needed)
sudo openssl req -newkey rsa:2048 -days 1095 -nodes \
  -keyout client-key.pem -out client-req.pem \
  -subj "/CN=app-client"

sudo openssl x509 -req -in client-req.pem -days 1095 \
  -CA ca.pem -CAkey ca-key.pem -set_serial 02 -out client-cert.pem

Many MySQL startup failures after enabling SSL are caused simply by incorrect file permissions. Remove the leftover .pem CSR files and lock down permissions for the mysql user:

sudo rm -f server-req.pem client-req.pem
sudo chown -R mysql:mysql /etc/mysql/ssl
sudo chmod 600 /etc/mysql/ssl/*-key.pem
sudo chmod 644 /etc/mysql/ssl/ca.pem /etc/mysql/ssl/*-cert.pem

Configuring SSL/TLS and Enforcing REQUIRE SSL

1. Configure MySQL Server (my.cnf)

Open your configuration file (/etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu/Debian or /etc/my.cnf on RHEL/CentOS) and add the following to the [mysqld] section:

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

# Allow only TLS 1.2 and 1.3
tls_version = TLSv1.2,TLSv1.3

# Require encryption for all TCP/IP connections (Unix socket connections still work normally)
require_secure_transport = ON

Zero-downtime rollout tip: If you have 10–15 microservices connecting to the database and aren’t sure all of them have SSL enabled yet, don’t set require_secure_transport = ON right away. Configure the three certificate files first, restart MySQL (or run ALTER INSTANCE RELOAD TLS; on MySQL 8.0.16+ to load certificates without a restart). Once every application has been updated, simply run SET GLOBAL require_secure_transport = ON; and persist it in my.cnf.

2. Per-User Enforcement (REQUIRE SSL vs. REQUIRE X509)

Want granular control per account instead of locking down the entire server? MySQL provides two clear options:

  • REQUIRE SSL: Forces the client to connect over SSL/TLS without requiring a client certificate. Suitable for most web apps and internal APIs.
  • REQUIRE X509: Enforces mutual authentication (mTLS). The client must present a valid certificate signed by the same CA, making it ideal for payment gateways or connections originating outside the VPC.

Apply it directly to an existing user:

ALTER USER 'app_user'@'10.0.1.%' REQUIRE SSL;
-- Or use mTLS for sensitive services:
-- ALTER USER 'payment_svc'@'%' REQUIRE X509;

3. Client and Application Configuration

In the MySQL CLI, always specify --ssl-mode explicitly to prevent connection downgrades to plaintext:

# Encrypt the connection (compatible with REQUIRE SSL)
mysql -h mysql-prod.internal -u app_user -p --ssl-mode=REQUIRED

# Verify both CA and hostname (recommended for Production)
mysql -h mysql-prod.internal -u app_user -p \
  --ssl-mode=VERIFY_IDENTITY \
  --ssl-ca=/etc/mysql/ssl/ca.pem

For applications (Node.js, Python, PHP), copy the ca.pem file to the app server and pass it into your connection config. For example, in Node.js (mysql2):

const pool = mysql.createPool({
  host: 'mysql-prod.internal',
  user: 'app_user',
  password: process.env.DB_PASSWORD,
  ssl: {
    ca: fs.readFileSync('/etc/ssl/certs/mysql-ca.pem'),
    rejectUnauthorized: true
  }
});

Testing & Monitoring Certificate Expiration

Once configured, test both cases: SSL connections must succeed, while unencrypted connections must be blocked.

Log in to MySQL over SSL and check the active cipher:

SHOW SESSION STATUS LIKE 'Ssl_cipher';
SHOW SESSION STATUS LIKE 'Ssl_version';

If the output shows an algorithm name (such as TLS_AES_256_GCM_SHA384) along with TLSv1.3 or TLSv1.2, you are good to go. To audit all open connections and check if any service forgot to enable SSL, run the following query:

SELECT sbt.variable_value AS tls_version,
       t.processlist_user,
       t.processlist_host
FROM performance_schema.status_by_thread sbt
JOIN performance_schema.threads t ON sbt.thread_id = t.thread_id
WHERE sbt.variable_name = 'Ssl_version'
  AND t.processlist_user IS NOT NULL;

Now try disabling SSL to verify that unencrypted connections are rejected:

mysql -h 127.0.0.1 -u app_user -p --ssl-mode=DISABLED

If the server returns ERROR 1045 (28000): Access denied (when using REQUIRE SSL) or ERROR 3159 (HY000): Connections using insecure transport are prohibited (when require_secure_transport is enabled), your configuration is working properly.

Don’t forget to monitor certificate expiration: A certificate expiring at midnight will cause all your applications to drop connections at once. You can quickly check the expiration date directly via SQL:

SHOW GLOBAL STATUS LIKE 'Ssl_server_not_after';

Or set up a weekly cron job running the OpenSSL command below to send a Slack alert 30 days before the certificate expires:

openssl x509 -enddate -checkend 2592000 -noout -in /etc/mysql/ssl/server-cert.pem

Just follow the right order: generate SAN-enabled certificates, enable TLS before enforcing require_secure_transport, and set up certificate expiration alerts. That way, your MySQL setup will both block internal eavesdropping and prevent unexpected service outages.

Share: