NestJS 中使用 TLS 连接的 MySQL

问题描述 投票:0回答:1

在我的 NestJS 后端中,我想为 MySQL 中的数据库添加 TLS 连接。我的 MySQL 位于 Azure 上,我已经关闭了

require_secure_transport = OFF
,现在如果我打开,我会收到此错误:

[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)

我的TypeORM配置是这样的:

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 {}
azure nestjs mysql2 azure-mysql-database
1个回答
0
投票

得到答案:

下面同步true 只需添加如下代码:

 ssl: { secureProtocol: 'TLSv1_3_method'},

如果您使用 1.2 Tls,则可以更改为 TLSv1_2_method;如果您使用 1.3 Tls,则可以更改为 TLSv1_3_method

终于成功了。

© www.soinside.com 2019 - 2024. All rights reserved.