Skip to main content

SSL

As part of the connection options, you can specify the ssl object property or a string containing the SSL profile content (deprecated).

ssl?: string | SslOptions;

See full list of SslOptions, which are in the same format as tls.createSecureContext.

SSL Optionsโ€‹

To enable SSL without manually providing certificates and assuming they are already trusted by the host machine, you can specify an empty object, for example:

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {},
});

You can also specify custom certificate(s) as an individual string or array of strings. Please note the arguments expect a string of the certificate, not a file name to the certificate:

import fs from 'node:fs';

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {
ca: fs.readFileSync(__dirname + '/mysql-ca.crt'),
},
});

When a certificate is read from an environment variable, it's recommended to replace escaped \n characters with proper new line characters, for example:

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {
ca: process.env.DB_SSL_CA?.replace(/\\n/gm, '\n'),
},
});

Key Exchange Groupsโ€‹

ecdhCurve sets the groups offered for the key exchange, as a colon-separated list in order of preference, for example 'P-256' or 'X25519:P-256'. A TLS 1.3 client only sends key shares for its first groups, so when the server accepts none of them it answers with a HelloRetryRequest and every connection pays an extra round trip. Putting a group the server accepts first avoids it:

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {
ca: fs.readFileSync(__dirname + '/mysql-ca.crt'),
ecdhCurve: 'P-256',
},
});

Unlike tls.DEFAULT_ECDH_CURVE, it only affects the connections that use this ssl object, not every other TLS client and server in the process.

OpenSSL Optionsโ€‹

secureOptions takes the SSL_OP_* flags from crypto.constants, combined with |, and applies them to the connections that use this ssl object, unlike OPENSSL_CONF, which changes the whole process.

For example, OpenSSL 3 refuses a server that does not support secure renegotiation (RFC 5746) and fails with unsafe legacy renegotiation disabled:

import { constants } from 'node:crypto';

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {
ca: fs.readFileSync(__dirname + '/mysql-ca.crt'),
// Beware, this leaves the connection open to the renegotiation attack RFC 5746 fixes:
secureOptions: constants.SSL_OP_LEGACY_SERVER_CONNECT,
},
});

Only allow it for a server you cannot upgrade.

SSL Certificate Bundleโ€‹

Alternatively, you can use a bundle with CA certificates. For example for Amazon RDS you could use:

import awsCaBundle from 'aws-ssl-profiles';

const connection = await mysql.createConnection({
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});

For detailed instructions, please follow aws-ssl-profiles documentation.

SSL Profile (deprecated)โ€‹

There is also a deprecated option allowing to specify a string containing name of SSL profile:

const connection = await mysql.createConnection({
host: 'localhost',
ssl: 'Amazon RDS',
});

Following profiles are included in the package:

TLS Session Resumptionโ€‹

Every new TLS connection normally runs a full handshake, in which the server sends its certificate chain and the client verifies it. MySQL2 keeps the session ticket issued at the end of a successful handshake in memory and presents it on later connections to the same server, which lets the server skip the certificate exchange and, on TLS 1.2, one round trip. This happens automatically and is never persisted to disk.

A session is only reused when the new connection shares the same ssl object, host and port as the one that established it, together with the same rejectUnauthorized and verifyIdentity settings, so a session established under a lax configuration is never resumed by a strict one. Connections created by a pool or a pool cluster already share their ssl object. When you create connections one by one, reuse a single ssl object to get the same benefit:

const ssl = { ca: fs.readFileSync(__dirname + '/mysql-ca.crt') };

const first = await mysql.createConnection({ host: 'localhost', ssl });
const second = await mysql.createConnection({ host: 'localhost', ssl });

A server that rejects or no longer knows the ticket silently falls back to a full handshake, and a failed handshake drops the cached session.

Ignoring Unauthorized SSL Errorsโ€‹

You can also connect to a MySQL server without providing an appropriate CA to trust. This is highly discouraged as being insecure.

const connection = await mysql.createConnection({
host: 'localhost',
ssl: {
// Beware, set `rejectUnauthorized` as `false` is strongly discouraged for security reasons:
rejectUnauthorized: false,
},
});