You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS连接AWS RDS SQL Server报错:无法获取本地颁发者证书

问题原因

你的连接配置存在3处错误,直接导致连接失败:

  • 开启了集成身份认证:IntegratedSecurity: true 是Windows集成身份验证专用参数,开启后会忽略你填写的用户名、密码,尝试用当前运行程序的系统身份登录RDS,认证自然失败。
  • 加密参数大小写错误:mssql驱动识别的加密参数是小写的encrypt,大写Encrypt不会生效,导致连接时加密逻辑异常。
  • 证书校验配置不匹配:trustServerCertificate: false 会强制校验数据库服务端证书,AWS RDS默认使用AWS自签证书,本地未导入对应根证书时会触发校验失败。

修正后的配置代码

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath:'.env',
      isGlobal: true
    }),
    TypeOrmModule.forRoot({
      type: 'mssql',
      host: 'xxx.xxxxxx.xxxxxx.rds.amazonaws.com',
      port: 1433,
      username: 'root',
      password: 'root',
      database: 'DB',
      autoLoadEntities: true,
      // 注意:生产环境必须关闭synchronize,避免自动修改表结构丢失数据
      synchronize: process.env.NODE_ENV === 'development',
      extra: { 
        trustServerCertificate: true,
        encrypt: true
      }
    }),
  ],
  controllers: [AppController, EstadoProyController],
  providers: [AppService],
})
export class AppModule {}

额外注意事项

如果生产环境需要严格校验证书,可将trustServerCertificate改回false,同时在extra中添加对应证书路径配置,传入AWS官方发布的RDS根证书文件路径即可。

内容的提问来源于stack exchange,提问作者alex schobel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 16:15:02