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

Node.js连接Docker部署的MS SQL Server失败(自签名证书问题)

解决Node.js连接Docker部署的SQL Server自签名证书错误

问题原因

Docker部署的SQL Server默认使用自签名SSL证书,而mssql库(依赖tedious驱动)默认会验证证书有效性,导致连接失败。

快速解决方案(开发环境)

在连接配置中添加trustServerCertificate: true选项,跳过证书验证:

const sql = require('mssql');

const config = {
    user: 'sa',
    password: 'mypasshere',
    server: 'localhost',
    database: 'TestDatabase',
    options: {
        trustServerCertificate: true // 添加该配置项
    }
};

sql.connect(config, (err) => {
    if (err) console.log('Failed to connect to SQL Server.', err);
    else {
        console.log('Connected to SQL Server.');

        const request = new sql.Request();
        request.query('SELECT * FROM dbo.Users', (err, result) => {
            if (err) console.log('Failed to execute SQL query.');
            else console.log(result);
        });
    }
});

补充说明

  • trustServerCertificate: true仅适合开发/测试环境,生产环境建议配置合法SSL证书并关闭此选项。
  • 额外检查:确保Docker容器已通过-p 1433:1433映射端口,sa密码正确,TestDatabase数据库已创建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 15:15:24