【问题标题】:NodeJS connecting to sql server using node-mssql does not workNodeJS使用node-mssql连接到sql server不起作用
【发布时间】:2016-09-09 08:25:10
【问题描述】:

我使用 SQL Server Express 2012 的命名实例 如果我尝试使用 SSMS 连接到它,则可以使用以下参数:

Server name: mit-007\SQLEXPRESS2012
Authentication: SQL Server Authentication
    Login: sa
    Password: mit

使用 node-mssql:

var sql = require('mssql');
var config = {
    user: 'sa',
    password: 'mit',
    server: 'mit-007',
    driver: 'tedious',
    database: 'Delvi',
    options: {
        instanceName: 'SQLEXPRESS2012'
    }
};

sql.connect(config).then(function(){ // and so on

它会记录这个错误

{ [ConnectionError: Failed to connect to mit-007:undefined in 15000ms]
  name: 'ConnectionError',
  message: 'Failed to connect to mit-007:undefined in 15000ms',
  code: 'ETIMEOUT' }

【问题讨论】:

    标签: sql-server node.js node-mssql


    【解决方案1】:

    我认为mit-007 不是您的网络地址。

    https://msdn.microsoft.com/pl-pl/library/ms189921(v=sql.110).aspx

    【讨论】:

    • 这是服务器的主机名,如果我使用 PHP 连接,它可以工作,而不是来自 nodejs
    • 然后尝试定义端口。默认为 1433。
    • github.com/patriksimek/node-mssql/issues/155 - 类似的问题,也许有帮助。
    • 谢谢,那个链接最终帮我解决了问题
    【解决方案2】:

    浏览后我解决了问题,这就是我所做的

    1. 打开 SQL Server 配置管理器
    2. 单击 SQL Server 网络配置 => SQLEXPRESS2012 的协议
    3. 双击 TCP/IP
    4. 将启用更改为是
    5. 点击 IP 地址
    6. IPAll => 清除 TCP 动态端口,设置 TCP 端口 1433
    7. 打开 services.msc
    8. 启动 SQL Server Browser 服务
    9. 重启 SQL Server

    我不确定上述每一个步骤是否都是必要的,但它们对我有用

    【讨论】:

      【解决方案3】:

      繁琐的驱动逻辑中存在一个缺陷,表明在 mac 平台上运行时端口“未定义”。

      在 MS Windows 平台上使用以下配置设置时,程序 (testtedious.js) 运行良好:

      var connectionConfig = {
          userName: 'xxx',
          password: 'yyy',
          database: 'zzz',
          server: '127.0.0.1',
          port: 1443,
          debug: true,
          driver: 'tedious',
          options: {
              encrypt: false,
              instanceName: 'SQLEXPRESS',
              database: 'www',
              useColumnNames: false,
              debug: {
                  packet: true,
                  data: true,
                  payload: true,
                  token: true,
                  log: true
              }
          }
      };
      

      但是,在 mac 上运行时出现以下错误:

      $ node testtedious.js
      Tedious-Connection-Pool: filling pool with 2
      Tedious-Connection-Pool: creating connection: 1
      Tedious-Connection-Pool: creating connection: 2
      Tedious-Connection-Pool: connection connected: 1
      Tedious-Connection-Pool: connection closing because of error
      { ConnectionError: Failed to connect to 127.0.0.1:undefined in 15000ms
          at ConnectionError (/project-dir/node_modules/tedious/lib/errors.js:12:12)
          at Connection.connectTimeout (/project-dir/node_modules/tedious/lib/connection.js:467:28)
          at ontimeout (timers.js:365:14)
          at tryOnTimeout (timers.js:237:5)
          at Timer.listOnTimeout (timers.js:207:5)
        message: 'Failed to connect to 127.0.0.1:undefined in 15000ms',
        code: 'ETIMEOUT' }
      Tedious-Connection-Pool: connection connected: 2
      Tedious-Connection-Pool: connection closing because of error
      { ConnectionError: Failed to connect to 127.0.0.1:undefined in 15000ms
          at ConnectionError (/project-dir/node_modules/tedious/lib/errors.js:12:12)
          at Connection.connectTimeout (/project-dir/node_modules/tedious/lib/connection.js:467:28)
          at ontimeout (timers.js:365:14)
          at tryOnTimeout (timers.js:237:5)
          at Timer.listOnTimeout (timers.js:207:5)
        message: 'Failed to connect to 127.0.0.1:undefined in 15000ms',
        code: 'ETIMEOUT' }
      Tedious-Connection-Pool: creating connection: 3
      Tedious-Connection-Pool: creating connection: 4
      

      这里是“修复”:

      var connectionConfig = {
          userName: 'xxx',
          password: 'yyy',
          database: 'zzz',
          server: '127.0.0.1',
          port: 1443,
          debug: true,
          driver: 'tedious',
          options: {
              port: 1443,
              encrypt: false,
              database: 'www',
              useColumnNames: false,
              debug: {
                  packet: true,
                  data: true,
                  payload: true,
                  token: true,
                  log: true
              }
          }
      };
      

      注意将port 设置移动到选项中并删除instanceName: 'SQLEXPRESS' 选项设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 2020-03-18
        • 1970-01-01
        • 2016-04-21
        • 2018-06-20
        • 2011-08-31
        相关资源
        最近更新 更多