tedious icon indicating copy to clipboard operation
tedious copied to clipboard

ConnectionError: Failed to connect to <azure-server-name>:1433 - Could not connect (sequence)

Open renatocamara opened this issue 3 years ago • 3 comments

I'm trying to run one sample microservices(Nodejs) deployed on Azure Openshift(4.8.11) but no success.

The microservices must connect to Azure SQL databases(PaaS).

Locally the microservices works normally.

What could be causing this issue?

You can see the code here: https://github.com/renatocamara/api-nodejs-mssql

As you can see below. I'm receiving the error "ConnectionError: Failed to connect to mvpserverdb.database.windows.net:1433 - Could not connect (sequence)"

Environment:
	DEV_MODE=false
	NODE_ENV=production
	DEBUG_PORT=5858
Launching via npm...
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info lifecycle [email protected]~prestart: [email protected]
npm info lifecycle [email protected]~start: [email protected]

> [email protected] start /opt/app-root/src
> node server.js

WARNING: NODE_ENV value of 'production' did not match any deployment config file names.
WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
consign v0.1.6 Initialized in /opt/app-root/src
+ ./config/database.js
+ ./app/routes/cdhusts.routes.js
+ ./app/routes/cnhboleto.routes.js
+ ./app/routes/cnhrenov.routes.js
+ ./app/routes/cnhsit.routes.js
+ ./app/routes/cnhstatusproc.routes.js
+ ./app/routes/cnhstsrenov.routes.js
+ ./app/routes/person.routes.js
+ ./app/controllers/cdhusts.controller.js
+ ./app/controllers/cnhboleto.controller.js
+ ./app/controllers/cnhrenov.controller.js
+ ./app/controllers/cnhsit.controller.js
+ ./app/controllers/cnhstsproc.controller.js
+ ./app/controllers/cnhstsrenov.controller.js
+ ./app/controllers/person.controller.js
Server ON running on Port 8080
Database Connection Failed! Bad Config:  ConnectionError: Failed to connect to mvpserverdb.database.windows.net:1433 - Could not connect (sequence)
    at /opt/app-root/src/node_modules/mssql/lib/tedious/connection-pool.js:71:17
    at Connection.onConnect (/opt/app-root/src/node_modules/tedious/lib/connection.js:1043:9)
    at Object.onceWrapper (events.js:520:26)
    at Connection.emit (events.js:400:28)
    at Connection.emit (/opt/app-root/src/node_modules/tedious/lib/connection.js:1071:18)
    at Connection.socketError (/opt/app-root/src/node_modules/tedious/lib/connection.js:1669:12)
    at /opt/app-root/src/node_modules/tedious/lib/connection.js:1428:21
    at SequentialConnectionStrategy.connect (/opt/app-root/src/node_modules/tedious/lib/connector.js:129:14)
    at Socket.onError (/opt/app-root/src/node_modules/tedious/lib/connector.js:149:12)
    at Socket.emit (events.js:400:28) {
  code: 'ESOCKET',
  originalError: ConnectionError: Failed to connect to mvpserverdb.database.windows.net:1433 - Could not connect (sequence)
      at ConnectionError (/opt/app-root/src/node_modules/tedious/lib/errors.js:13:12)
      at Connection.socketError (/opt/app-root/src/node_modules/tedious/lib/connection.js:1669:56)
      at /opt/app-root/src/node_modules/tedious/lib/connection.js:1428:21
      at SequentialConnectionStrategy.connect (/opt/app-root/src/node_modules/tedious/lib/connector.js:129:14)
      at Socket.onError (/opt/app-root/src/node_modules/tedious/lib/connector.js:149:12)
      at Socket.emit (events.js:400:28)
      at emitErrorNT (internal/streams/destroy.js:106:8)
      at emitErrorCloseNT (internal/streams/destroy.js:74:3)
      at processTicksAndRejections (internal/process/task_queues.js:82:21) {
    code: 'ESOCKET'
  }
}

As you can see, we can connect to the database inside pod. image

When I run locally on my macbook. image

Expected behaviour:

Connection is established and fetching works as usual.

database.js:

const sql = require('mssql')

const config = {
  user: process.env.DATABASE_USER,
  password: process.env.DATABASE_PWS,
  database: process.env.DATABASE_NAME,
  server: process.env.SERVER_NAME,
  "pool": {
      "max": 10,
      "min": 0,
      "idleTimeoutMillis": 30000
  },
  "options": {
      "encrypt": true,
      "trustServerCertificate": false
  },
}

const poolPromise = new sql.ConnectionPool(config)
  .connect()
  .then(pool => {
    console.log('Connected to SQLServer...');
    return pool;
  })
  .catch(err => console.log('Database Connection Failed! Bad Config: ', err));

module.exports = {
  sql, poolPromise
};

Configuration:

--file:package.json
{
  "name": "api-node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.1",
    "consign": "^0.1.6",
    "config": "3.3.6",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mssql": "^7.3.0",
    "path": "^0.12.7",
    "util": "^0.12.4"
  }
}

Software versions

  • NodeJS: 14.17.5
  • node-mssql: 7.3.0
  • SQL Server: Microsoft SQL Azure (RTM) - 12.0.2000.8 Sep 18 2021 19:01:34 Copyright (C) 2019 Microsoft Corporation

renatocamara avatar Dec 13 '21 12:12 renatocamara

Hi @renatocamara, I noticed that you are using process.env to pick up the credentials from the system environment variables. This will definitely work within an azure environment like azure VM or app service since they prepare those environment variables ahead among the creation of these environments. You mentioned the term "pod" in your description, not quite sure what is that, but I guess it is also an azure side environment? When you are trying to run this locally, and if I am correct, those environment variables may not be successfully picked up. This could be the cause of the connection issue.

MichaelSun90 avatar Dec 14 '21 18:12 MichaelSun90

Hi @MichaelSun90 in fact I deployed the microservices on ARO - Azure RedHat OpenShift. The connection with the database not works when I using Istio because I need to implements a circuit breaker.

When I deploy the microservices on ARO without use Istio the connection with database works normally.

I changed the type of the database, from MSSQL Server(PaaS) to MySQL(PaaS) and worked perfectly with Istio.

Maybe there is some "incompatibility" between when I use ARO with istio and MSSQL Server(PaaS) and node-mssql driver.

The problem is not related to environment variables. This is working perfectly. The microservices can read the variables.

renatocamara avatar Dec 16 '21 13:12 renatocamara

Thank you for sharing this with me. Is it ok for me to close this one, or do you have some further questions?

MichaelSun90 avatar Dec 17 '21 19:12 MichaelSun90