Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25577248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED
提问by Matt Carrier
I am trying to connect to MSSQL 2012 using NodeJS with the mssqlconnection interface.
我正在尝试使用 NodeJS 和mssql连接接口连接到 MSSQL 2012 。
When attempting to connect I get the following error:
尝试连接时出现以下错误:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED]
name: 'ConnectionError',
message: 'Failed to conncet to localhost:1433 - connect ECONNREFUSED',
code: 'ESOCKET' }
Any ideas on how to fix this?
有想法该怎么解决这个吗?
回答by Matt Carrier
The solution is to enable TCP connections which are disabled by default.
解决方案是启用默认禁用的 TCP 连接。


回答by MMalke
My case wasn't exactly the same as Matt's, but his screenshot was enough to remember me what was missing.
我的情况与马特的情况并不完全相同,但他的截图足以让我记住缺少的内容。


As it is said here, when you are using the SQL Server Instance Name to connect to it, you must have SQL Server Browser running.
正如此处所说,当您使用 SQL Server 实例名称连接到它时,您必须运行 SQL Server Browser。
options.instanceName The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1444 on the database server must be reachable. (no default) Mutually exclusive with options.port.
options.instanceName 要连接的实例名称。SQL Server Browser 服务必须在数据库服务器上运行,并且必须可以访问数据库服务器上的 UDP 端口 1444。(无默认)与 options.port 互斥。
回答by Player1
If after enabling the TCP connection and still your configuration is not working. Here's my own-configuration.
如果在启用 TCP 连接后仍然您的配置不起作用。这是我自己的配置。
var config = {
"user": 'admin',
"password": 'password',
"server": 'ALBERT-PC',
"database": 'database_name',
"port": '61427',
"dialect": "mssql",
"dialectOptions": {
"instanceName": "SQLEXPRESS"
}
};
回答by Alen
If somebody still struggles to connect despite doing all that was proposed.
In my case I had to manually set TCP Port property to 1433 in SQL Server Network Configuration -> Protocols for ... -> TCP/IP -> IP Addresses -> IPAll.
如果有人尽管做了所有建议的事情,但仍然难以建立联系。
就我而言,我必须在 SQL Server 网络配置 -> 协议... -> TCP/IP -> IP 地址 -> IPAll 中手动将 TCP 端口属性设置为 1433。
[![1]](/static/img/viewimg.png)
[![1]](/static/img/viewimg.png)
回答by Adam Gering
Best practice is to first verify the connection to the SQL server using a query analyzer (SQL Management Studio (Windows) or SQLPro for MSSQL (Mac)) using the same protocol, port and credentials as you wish to use via your application.
最佳做法是首先使用查询分析器(SQL Management Studio (Windows) 或 SQLPro for MSSQL (Mac))验证与 SQL 服务器的连接,使用与您希望通过应用程序使用相同的协议、端口和凭据。
In Management Studio, the format is Server,Port (e.g. 192.168.1.10,1433); and you'll probably be using SQL Server Authentication instead of Windows Authentication.
在 Management Studio 中,格式为 Server,Port(例如 192.168.1.10,1433);并且您可能会使用 SQL Server 身份验证而不是 Windows 身份验证。
Steps to configure the SQL Server:
配置 SQL Server 的步骤:
Install with Mixed Authentication, if you intend to use SQL Server Authentication.
如果您打算使用 SQL Server 身份验证,请使用混合身份验证进行安装。
Setup SQL Server to listen on TCP on a fixed port number:
设置 SQL Server 以在固定端口号上侦听 TCP:
- SQL Configuration Manager SQL Server Network Configuration
- Protocols for {Instance}
- TCP/IP - Enabled (double-click)
- IP Address (on all desired interfaces)
- TCP Dynamic Ports = BLANK! (not zero)
- TCP Port - 1433 (or desired port)
- Protocols for {Instance}
- SQL 配置管理器 SQL Server 网络配置
- {Instance} 的协议
- TCP/IP - 启用(双击)
- IP 地址(在所有需要的接口上)
- TCP 动态端口 = 空白!(非零)
- TCP 端口 - 1433(或所需端口)
- {Instance} 的协议
回答by Amresh Kumar
**Please follow the connection configuration and little test:**
//Declare global variable
var http = require('http');
var events = require('events');
var nodemailer = require('nodemailer');
var sql = require('mssql');<br/>
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
//Create an http server
http.createServer(function(req,res)
{
res.writeHead(200, {'Content-Type': 'text/html'});
var Connection = require('tedious').Connection;
//Configure the connection
var config = {
userName: '<user id>',
password: '<password>',
server: '<system ip>',
options: {database: '<database name>'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
console.log("Connected");
executeStatement();
});
function executeStatement() {
request = new Request("select getdate();", function(err) {
if (err) {
console.log(err);}
});
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
connection.execSql(request);
};
return res.end();
}).listen(8080);
//Post configuration test on browser: http://localhost:8080/
//在浏览器上发布配置测试: http://localhost:8080/
回答by DaNeSh
I couldn't connect with 'localhost' although I use 'localhost' in SQL Management Studio and other applications. When I used Computer Name (network address), it worked!
尽管我在 SQL Management Studio 和其他应用程序中使用了“localhost”,但我无法连接到“localhost”。当我使用计算机名(网络地址)时,它起作用了!
回答by Tusar
Apart from setting TCP port no to 1433. If you are getting "Connection lost - Cannot call write after a stream was destroyed" error
除了将 TCP 端口号设置为 1433。如果您收到“连接丢失 - 流被破坏后无法调用写入”错误
This error will also come if you use options.encrypt: true on Node v12+ with old SQL Server versions.
如果您在带有旧 SQL Server 版本的 Node v12+ 上使用 options.encrypt: true ,也会出现此错误。
This is caused by Node v12 requiring TLS 1.2.
这是由需要 TLS 1.2 的 Node v12 引起的。
- install the TLS 1.2 security patch for your SQL Server
run node with backwards compatibility flag:
node --tls-min-v1.0eg:
node --tls-min-v1.0 app.jsdisable encrypted communication by setting
options.encrypt: false(optional)
- 为您的 SQL Server 安装 TLS 1.2 安全补丁
使用向后兼容标志运行节点:
node --tls-min-v1.0例如:
node --tls-min-v1.0 app.js通过设置禁用加密通信
options.encrypt: false(可选的)
Config Object:
配置对象:
const config = {
user: '...',
password: '...',
server: 'localhost',
database: '...',
'options.encrypt': false
}
Ref:https://github.com/tediousjs/tedious/issues/903#issuecomment-614597523
参考:https : //github.com/tediousjs/tedious/issues/903#issuecomment-614597523

