NodeJS connect to SQL Server getaddrinfo ENOTFOUNT

i86rm4rw  于 2023-10-15  发布在  SQL Server
关注(0)|答案(2)|浏览(135)

I'm trying to connect to a Microsoft SQL Server using Knex.js, but I'm getting getaddrinfo ENOTFOUND. I'm aware that this is an indication, that NodeJS is unable to resolve the address, often through DNS or protocol problems.

const knex = require('knex')({
    client: 'mssql',
    connection: {
        server: 'filesrv\\docx',  // backslash in string needs to be escaped
        user: 'user',             // changed for privacy reasons
        password: 'secret',       // changed for privacy reasons
        options: {
            port: 1433,
            database: 'DX_Matching_DB'
        }
    }
});

UnhandledPromiseRejectionWarning: ConnectionError: Failed to connect to filesrv\docx:1433 - getaddrinfo ENOTFOUND filesrv\docx

at ConnectionError(.\node_modules\tedious\lib\errors.js:13:12)
at Connection.socketError(.\node_modules\tedious \lib\connection.js:1664:56)
...

SQL Server in SQL Server Object Explorer:

App is run on Windows. Resolve-DnsName is able to resolve filesrv\docx. No change when exchanging filesrv for the corresponding IP.

qcuzuvrc

qcuzuvrc1#

The knex configuration object uses the instance name inside the options, e.g.:

const knex = require('knex')({
    client: 'mssql',
    connection: {
        server: 'filesrv',
        user: 'user',
        password: 'secret',
        options: {
            database: 'DX_Matching_DB',
            instanceName: 'docx'
        }
    }
});
tf7tbtn2

tf7tbtn22#

  • You have to setting port in Sql Server Configuration Manager - SQL Server Network Configuration -> TCP/IP -> Properties -> IP Address -> Set IP 4 or 6 (example: 1466)

  • Restart Sql Server enter code here

const knex = require('knex')({
    client: 'mssql',
    connection: {
        server: 'filesrv',
        user: 'user',
        password: 'secret',
        options: {
            database: 'DX_Matching_DB',
            instanceName: 'docx',
            port: 1466
        }
    }
});

相关问题