MySQL Forums
Forum List  »  Connector/Node.js

Understanding SSL connection with MySQL protocol
Posted by: austin cheney
Date: February 13, 2023 07:21AM

I am writing a MySQL connection utility from scratch in Node.js in order to better understand the details of the protocol and extend my knowledge of Node TLS. The problem I am having is how to execute the upgrade from a Net socket to a TLS socket.

Following the MySQL documentation I can create a socket and connect to MySQL, get back the greeting packet and parse it. I believe I am formulating the SSL Request packet properly:

<Buffer 20 00 00 01 ff ff ff cf 00 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

I cannot seem to get the SSL connection correct though.

I am performing the following:

let secureSocket = null;
const context = {
ca: "ca cert text",
cert: "cert text",
minVersion: "TLSv1.3"


secureSocket = new TLSSocket(socket, {
rejectUnauthorized : true,
requestCert : true,
secureContext : context,
isServer : false

host: env[environment].host,
port: env[environment].port,
secureContext: sslContext,
socket: secureSocket

I am getting this error:

throw er; // Unhandled 'error' event

Error: Client network socket disconnected before secure TLS connection was established
at connResetException (node:internal/errors:717:14)
at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
at TLSSocket.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on TLSSocket instance at:
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
path: undefined,
host: 'host location address',
port: 3306,
localAddress: undefined

Node.js v19.4.0

Any guidance or suggestions?

Options: ReplyQuote

Written By
Understanding SSL connection with MySQL protocol
February 13, 2023 07:21AM

Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.