Home > Back-end >  Why can't my node js script connect to my mssql database running in docker, but Microsoft SQL S
Why can't my node js script connect to my mssql database running in docker, but Microsoft SQL S

Time:12-09

I'm trying to connect node js script to MsSQL database server, but fails.

My Microsoft SQL Server Management is however able to connect to the database.

node.js code

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

However, when I try to connect to the database via this login info (same as node config variable) enter image description here

It connects and I get this view over the database enter image description here

For info about the database, it is running via docker on another pc, but is reachable

I moved the port to options and now the error is this

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

CodePudding user response:

As this issue shows the port should be specified in options:

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