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

使用Windows身份验证连接MSSQL时出现ELOGIN登录失败问题

解决Windows身份验证连接MSSQL时报“Login failed for user ''”的问题

你的代码未显式启用Windows身份验证,导致驱动默认尝试SQL Server身份验证,但未提供用户名密码,因此出现空用户登录失败的错误。按以下步骤修改即可:

1. 修改连接配置

在config中明确指定使用Windows身份验证,有两种常见写法:

写法一(推荐,适配新版node-mssql)

添加authentication字段指定NTLM类型:

const config = {
  server: "D15\\SQLEXPRESS",
  database: "DBToDo",
  port: 1433,
  authentication: {
    type: 'ntlm',
    options: {
      domain: '你的机器名或域名' // 本地连接可省略,默认使用当前Windows用户
    }
  },
  options: {
    encrypt: false,
    trustServerCertificate: true
  }
};

写法二(兼容旧版本)

在options中添加trustedConnection: true:

const config = {
  server: "D15\\SQLEXPRESS",
  database: "DBToDo",
  port: 1433,
  options: {
    encrypt: false,
    trustServerCertificate: true,
    trustedConnection: true // 启用Windows身份验证
  }
};

2. 确认SQL Server配置

确保你的SQL Server实例已启用混合身份验证模式:

  • 打开SQL Server Management Studio(SSMS),右键目标服务器→属性→安全性
  • 选择“SQL Server和Windows身份验证模式”,重启SQL Server服务生效

3. 检查权限

运行Node.js进程的Windows用户,需要拥有访问该SQL Server实例以及DBToDo数据库的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 00:47:41