简体   繁体   English

为什么我的node js脚本连接不上我在docker中运行的mssql数据库,但是微软SQL服务器管理可以

[英]Why can't my node js script connect to my mssql database running in docker, but Microsoft SQL Server Management can

I'm trying to connect node js script to MsSQL database server, but fails.我正在尝试将节点 js 脚本连接到 MsSQL 数据库服务器,但失败了。

My Microsoft SQL Server Management is however able to connect to the database.但是,我的 Microsoft SQL 服务器管理能够连接到数据库。

node.js code node.js代码

var Connection = require('tedious').Connection;  
var config = {  
    server: "172.168.200.35",
    dialect: "mssql",
    port:51433,
    authentication: {
        type: 'default',
        options: {
            userName: 'testuser',
            password: 'abc123'
        }
    },
    options: {
        database: 'IoTDb'
    }
};  
var connection = new Connection(config);  
// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
if (err) {
    console.error(err.message);
} else {
    executeStatement();
}
});

When running the code, I get this error运行代码时,我收到此错误

Failed to connect to 172.168.200.35:51433:1433 - getaddrinfo ENOTFOUND 172.168.200.35:51433

I don't understand why its fails to connect port 1433 , as the port is defined as 51433我不明白为什么它无法连接端口1433 ,因为端口定义为51433

However, when I try to connect to the database via this login info (same as node config variable)但是,当我尝试通过此登录信息连接到数据库时(与节点配置变量相同) 在此处输入图像描述

It connects and I get this view over the database它连接了,我通过数据库获得了这个视图在此处输入图像描述

For info about the database, it is running via docker on another pc, but is reachable有关数据库的信息,它通过 docker 在另一台电脑上运行,但可以访问

I moved the port to options and now the error is this我将port移动到options ,现在错误是这个

Failed to connect to 172.168.200.35:51433:51433 - getaddrinfo ENOTFOUND 172.168.200.35:51433

As this issue shows the port should be specified in options :如此问题所示,应在options中指定端口:

var config = {  
    server: "172.168.200.35",
    dialect: "mssql",
...
    options: {
        database: 'IoTDb',
        port:51433,
    }
};  

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

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