[英]Error: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433 docker
[英]Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED
我正在嘗試使用 NodeJS 和mssql連接接口連接到 MSSQL 2012。
嘗試連接時出現以下錯誤:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED]
name: 'ConnectionError',
message: 'Failed to conncet to localhost:1433 - connect ECONNREFUSED',
code: 'ESOCKET' }
有想法該怎么解決這個嗎?
解決方案是啟用默認禁用的 TCP 連接。
我的情況與馬特的情況並不完全相同,但他的截圖足以讓我記住缺少的內容。
正如此處所說,當您使用 SQL Server 實例名稱連接到它時,您必須運行 SQL Server Browser。
options.instanceName
要連接的實例名稱。 SQL Server Browser 服務必須在數據庫服務器上運行,並且必須可以訪問數據庫服務器上的 UDP 端口 1434。
(無默認)
與 options.port 互斥。
如果啟用 TCP 連接后您的配置仍然無效。 這是我自己的配置。
var config = {
"user": 'admin',
"password": 'password',
"server": 'WINDOWS-PC',
"database": 'database_name',
"port": 61427, // make sure to change port
"dialect": "mssql",
"dialectOptions": {
"instanceName": "SQLEXPRESS"
}
};
如果有人盡管做了所有建議的事情,但仍然難以建立聯系。
就我而言,我必須在 SQL Server 網絡配置 -> 協議... -> TCP/IP -> IP 地址 -> IPAll 中手動將 TCP 端口屬性設置為 1433。
[
最佳做法是首先使用查詢分析器(SQL Management Studio (Windows) 或 SQLPro for MSSQL (Mac))驗證與 SQL 服務器的連接,使用與您希望通過應用程序使用相同的協議、端口和憑據。
在 Management Studio 中,格式為 Server,Port(例如 192.168.1.10,1433); 並且您可能會使用 SQL Server 身份驗證而不是 Windows 身份驗證。
配置 SQL Server 的步驟:
如果您打算使用 SQL Server 身份驗證,請使用混合身份驗證進行安裝。
設置 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);
//在瀏覽器上發布配置測試: http://localhost:8080/
就我而言,存在配置問題。這是錯誤的配置
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
}}
然后我在選項中添加了加密變量。它解決了我的問題。更正的配置
let config = {
server: 'localhost',
authentication: {
type: 'default',
options: {
userName: 'sa', // update me
password: 'xxxxxx' // update me
}
},
options: {
database: 'SampleDB',
validateBulkLoadParameters:false,
encrypt: false,
}
}
除了將 TCP 端口號設置為 1433。如果您收到“連接丟失 - 流被破壞后無法調用寫入”錯誤
如果您在帶有舊 SQL Server 版本的 Node v12+ 上使用 options.encrypt: true ,也會出現此錯誤。
這是由需要 TLS 1.2 的 Node v12 引起的。
使用向后兼容標志運行節點:
node --tls-min-v1.0
例如: node --tls-min-v1.0 app.js
通過設置禁用加密通信
options.encrypt: false
(可選)
配置對象:
const config = {
user: '...',
password: '...',
server: 'localhost',
database: '...',
'options.encrypt': false
}
參考: https : //github.com/tediousjs/tedious/issues/903#issuecomment-614597523
盡管我在 SQL Management Studio 和其他應用程序中使用了“localhost”,但我無法連接到“localhost”。 當我使用計算機名(網絡地址)時,它起作用了!
我的問題是我需要先在我的 mac 上使用這個命令使用 docker 啟動 sqlserver
sudo docker start sqlserver
對我來說,將此條件從“否”更改為“是”有效:
switch (key) {
case 'instance':
return this.config.options.instanceName
case 'trusted':
return this.config.options.trustedConnection ? 'Yes' : 'Yes'
即使我提供它,它也不會從選項對象加載值。
我在文件\\node_modules\\mssql\\lib\\msnodesqlv8\\connection-pool.js 中對此更改進行了硬編碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.