简体   繁体   English

使用Node-mssql连接到SQL Server的NodeJS不起作用

[英]NodeJS connecting to sql server using node-mssql does not work

I use a named instance of SQL Server Express 2012 If I try to connect to it using SSMS it works, using these parameters: 我使用SQL Server Express 2012的命名实例,如果尝试使用SSMS连接到它,则可以使用以下参数进行工作:

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

Using node-mssql: 使用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

It logs this error 它记录此错误

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

After browsing around I solved the problem, here's what I did 浏览后,我解决了问题,这是我所做的

  1. Open SQL Server Configuration Manager 打开SQL Server配置管理器
  2. Click SQL Server Network Configuration => Protocols for SQLEXPRESS2012 单击SQL Server网络配置=> SQLEXPRESS2012的协议
  3. Double click TCP/IP 双击TCP / IP
  4. Change Enabled to Yes 将启用更改为是
  5. Click IP Addresses 单击IP地址
  6. IPAll => Clear TCP Dynamic Ports, set TCP Port 1433 IPAll =>清除TCP动态端口,设置TCP端口1433
  7. Open services.msc 打开services.msc
  8. Start SQL Server Browser Service 启动SQL Server浏览器服务
  9. Restart SQL Server 重新启动SQL Server

I'm not sure that every single one of the steps above are necessary, but they worked for me 我不确定上面的每个步骤是否都必须,但是它们对我有用

I think mit-007 is not your network adress. 我认为mit-007不是您的网络地址。

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

There is a flaw in the tedious driver logic that indicates that the port is "undefined" when running on a mac platform. 繁琐的驱动程序逻辑中存在一个缺陷,表明在Mac平台上运行时端口是“未定义的”。

The program (testtedious.js) runs fine when using the following configuration settings on a MS Windows platform: 在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
        }
    }
};

However, when running on a mac I get the following error: 但是,在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

Here's the "fix": 这是“修复”:

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
        }
    }
};

Note the movement of the port setting into options and the removal of the instanceName: 'SQLEXPRESS' option setting. 注意将port设置移动到选项中,并删除了instanceName: 'SQLEXPRESS'选项设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM