[英]Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED
I am trying to connect to MSSQL 2012 using NodeJS with the mssql connection 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?有想法该怎么解决这个吗?
The solution is to enable TCP connections which are disabled by default.解决方案是启用默认禁用的 TCP 连接。
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 options.instanceName
The instance name to connect to.要连接的实例名称。 The SQL Server Browser service must be running on the database server, and UDP port 1434 on the database server must be reachable. SQL Server Browser 服务必须在数据库服务器上运行,并且必须可以访问数据库服务器上的 UDP 端口 1434。
(no default) (无默认)
Mutually exclusive with options.port.与 options.port 互斥。
If after enabling the TCP connection and your configuration is still not working.如果启用 TCP 连接后您的配置仍然无效。 Here's my own-configuration.这是我自己的配置。
var config = {
"user": 'admin',
"password": 'password',
"server": 'WINDOWS-PC',
"database": 'database_name',
"port": 61427, // make sure to change port
"dialect": "mssql",
"dialectOptions": {
"instanceName": "SQLEXPRESS"
}
};
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。
[ [
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 (eg 192.168.1.10,1433);在 Management Studio 中,格式为 Server,Port(例如 192.168.1.10,1433); and you'll probably be using SQL Server Authentication instead of Windows Authentication.并且您可能会使用 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:
**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/
In my case there was a configuration issue.This was the wrong configuration就我而言,存在配置问题。这是错误的配置
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
}}
Then I have added encrypt variable to the options.It solved my issue.Corrected configuration然后我在选项中添加了加密变量。它解决了我的问题。更正的配置
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
encrypt: false,
}
} }
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 引起的。
run node with backwards compatibility flag:使用向后兼容标志运行节点:
node --tls-min-v1.0
eg: node --tls-min-v1.0 app.js
例如: node --tls-min-v1.0 app.js
disable encrypted communication by setting通过设置禁用加密通信
options.encrypt: false
(optional) 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
I couldn't connect with 'localhost' although I use 'localhost' in SQL Management Studio and other applications.尽管我在 SQL Management Studio 和其他应用程序中使用了“localhost”,但我无法连接到“localhost”。 When I used Computer Name (network address), it worked!当我使用计算机名(网络地址)时,它起作用了!
我的问题是我需要先在我的 mac 上使用这个命令使用 docker 启动 sqlserver
sudo docker start sqlserver
For me changing this condition from 'No' to 'Yes' worked:对我来说,将此条件从“否”更改为“是”有效:
switch (key) {
case 'instance':
return this.config.options.instanceName
case 'trusted':
return this.config.options.trustedConnection ? 'Yes' : 'Yes'
It just doesn't load the value from the options object even if I provide it.即使我提供它,它也不会从选项对象加载值。
I hardcoded this change in file \\node_modules\\mssql\\lib\\msnodesqlv8\\connection-pool.js我在文件\\node_modules\\mssql\\lib\\msnodesqlv8\\connection-pool.js 中对此更改进行了硬编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.