简体   繁体   中英

ConnectionError: Failed to connect to MyServer:1433

I'm trying to connect to a remote SQL Server from my machine, I'm working with node. I can connect to it through SQL Management Studio, and I could connect with a C# application as well (both using Windows Authentication). I've already searched in several forums (in some questions here as well) and couldn't find a solution.

Below you can find the error I got:

   message: 'Failed to connect to MyServer:1433 - getaddrinfo ENOTFOUND MyServer',
     code: 'ESOCKET' },
  name: 'ConnectionError' }

This is the code I'm using:

'use strict';
var http = require('http');
var express = require('express');
var port = process.env.PORT || 1337;

var app = express();

app.get('/', (req, resp) => {
    var sql = require("mssql");

    var config = {
        driver: 'msnodesqlv8',
        server: 'MyServer/Instance',
        database: 'MyDB',
        options: {
            trustedConnection: true
        }
    };

    sql.connect(config, (error) => {
        if (error)
            console.log(error);

        var request = new sql.Request();
        request.query('select * from MyTable', (err, recordset) => {
            if (err)
                console.log(err);

            resp.send(recordset);
        });

    });

});

var servidor = app.listen(port, function () {
    console.log("Server running");
});

I looked other questions ( here and here for example), but no luck.

I got resolve this problem. First of all I created an user on my db and after include in my config the user and password. I needed to specify the instance and db in options, cause node was not recognizing when put all in the same string.

var config = {
    user: 'user_name',
    password: '****',
    server: 'MyServer',
    options: {
        instance: 'MyInstance',
        database: 'MyDB'
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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