Node.js MSSQL tedius ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED

前端 未结 10 1552
情话喂你
情话喂你 2020-12-09 01:37

I am trying to connect to MSSQL 2012 using NodeJS with the mssql connection interface.

When attempting to connect I get the following error:

{ [Conn         


        
相关标签:
10条回答
  • 2020-12-09 01:42

    Apart from setting TCP port no to 1433. If you are getting "Connection lost - Cannot call write after a stream was destroyed" error

    This error will also come if you use options.encrypt: true on Node v12+ with old SQL Server versions.

    This is caused by Node v12 requiring TLS 1.2.

    • install the TLS 1.2 security patch for your SQL Server
    • run node with backwards compatibility flag:

      node --tls-min-v1.0

      eg: node --tls-min-v1.0 app.js

    • disable encrypted communication by setting

      options.encrypt: false (optional)

    Config Object:

    const config = {
          user: '...',
          password: '...',
          server: 'localhost', 
          database: '...',
          'options.encrypt': false   
       }
    

    Ref: https://github.com/tediousjs/tedious/issues/903#issuecomment-614597523

    0 讨论(0)
  • 2020-12-09 01:44

    Best practice is to first verify the connection to the SQL server using a query analyzer (SQL Management Studio (Windows) or SQLPro for MSSQL (Mac)) using the same protocol, port and credentials as you wish to use via your application.

    In Management Studio, the format is Server,Port (e.g. 192.168.1.10,1433); and you'll probably be using SQL Server Authentication instead of Windows Authentication.


    Steps to configure the SQL Server:

    Install with Mixed Authentication, if you intend to use SQL Server Authentication.

    Setup SQL Server to listen on TCP on a fixed port number:

    • SQL Configuration Manager SQL Server Network Configuration
      • Protocols for {Instance}
        • TCP/IP - Enabled (double-click)
        • IP Address (on all desired interfaces)
          • TCP Dynamic Ports = BLANK! (not zero)
          • TCP Port - 1433 (or desired port)
    0 讨论(0)
  • 2020-12-09 01:46

    My case wasn't exactly the same as Matt's, but his screenshot was enough to remember me what was missing.

    enter image description here

    As it is said here, when you are using the SQL Server Instance Name to connect to it, you must have SQL Server Browser running.

    options.instanceName

    The instance name to connect to. The SQL Server Browser service must be running on the database server, and UDP port 1434 on the database server must be reachable.

    (no default)

    Mutually exclusive with options.port.

    0 讨论(0)
  • 2020-12-09 01:53

    If somebody still struggles to connect despite doing all that was proposed.
    In my case I had to manually set TCP Port property to 1433 in SQL Server Network Configuration -> Protocols for ... -> TCP/IP -> IP Addresses -> IPAll.

    [1]

    0 讨论(0)
  • 2020-12-09 01:57

    The solution is to enable TCP connections which are disabled by default.

    enter image description here

    0 讨论(0)
  • 2020-12-09 01:58

    In my case there was a configuration issue.This was the wrong configuration

    let config = {
    server: 'localhost',
    authentication: {
        type: 'default',
        options: {
            userName: 'sa', // update me
            password: 'xxxxx' // update me
        }
    },
    options: {
        database: 'SampleDB',
        validateBulkLoadParameters:false,
    }}
    

    Then I have added encrypt variable to the options.It solved my issue.Corrected configuration

    let config = {
    server: 'localhost',
    authentication: {
        type: 'default',
        options: {
            userName: 'sa', // update me
            password: 'xxxxxx' // update me
        }
    },
    options: {
        database: 'SampleDB',
        validateBulkLoadParameters:false,
        encrypt: false,
    }
    

    }

    0 讨论(0)
提交回复
热议问题