Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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

node.js code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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,
    }
};  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading