azurenestjsmysql2azure-mysql-database

MySQL with TLS connection in NestJS


In my NestJS backend I wanted to add a TLS connection for my database which is in MySQL. My MySQL is on Azure and I have turned off the require_secure_transport = OFF now if I turn on, I get this error:

[Nest] 1  - 04/23/2024, 6:17:10 AM   ERROR [ExceptionHandler] Connections using insecure transport are prohibited while --require_secure_transport=ON.
Error: Connections using insecure transport are prohibited while --require_secure_transport=ON.
    at Packet.asError (/usr/src/app/node_modules/mysql2/lib/packets/packet.js:728:17)
    at ClientHandshake.execute (/usr/src/app/node_modules/mysql2/lib/commands/command.js:29:26)
    at PoolConnection.handlePacket (/usr/src/app/node_modules/mysql2/lib/connection.js:456:32)
    at PacketParser.onPacket (/usr/src/app/node_modules/mysql2/lib/connection.js:85:12)
    at PacketParser.executeStart (/usr/src/app/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/usr/src/app/node_modules/mysql2/lib/connection.js:92:25)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)

My TypeORM configuration is this way:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
    }),
  ],
})
export class AppModule {}

Solution

  • got the answer:

    below synchronize true just add code as below:

    ssl: { secureProtocol: 'TLSv1_3_method'},

    you can change to TLSv1_2_method if you are using 1.2 Tls or if you are using 1.3 Tls then use TLSv1_3_method

    finally got it working.