P4TRYK P4TRYK - 11 months ago 127
Node.js Question

Node with SQL Server (Microsoft) connection to database

I need t o provide the SQL connection (using the library called mssql) for modules, so I provide the code like this:

var config = {
server: "149.xxx.xx.x",
database: "Database",
user: "User",
password: "Password",
connectionTimeout: 300000,
requestTimeout: 300000,
pool: {
idleTimeoutMillis: 300000,
max: 100

function getEmp() {
var connection = new sql.Connection(config);
var req = new sql.Request(connection);

connection.connect(function (error) {
if(error) {
req.query('Procedure', function(err, data) {
if (err) {
} else {


and I receive the error:

{ ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect
ETIMEDOUT 149.xxx.xx.x:1433 at Connection.<anonymous>
at Connection.g (events.js:292:16) at emitOne (events.js:96:13) at
Connection.emit (events.js:188:7) at Connection.socketError
at Socket.<anonymous>
at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at
emitErrorNT (net.js:1277:8) at _combinedTickCallback

name: 'ConnectionError', message: 'Failed to connect to
49.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433', code: 'ESOCKET' }

For sure the data are correct- DataGrip is connecting fine here.

I found at google the similar problem, the problem was Disabled TCP/IP. I checked this one and it's Enabled with this port 1443.

The TCP/IP, Named Pipes and Shared Memory is Enabled.

TCP Dynamic Ports: 1443 TCP Port: empty

Answer Source

To find the reason of 18456 error "login failed for user..." one should check SQLServer error log.

In case of

Reason: Failed to open the explicitly specified database 'Database Name'

one should first check the state of the database, if it's online, like this:

 select name, 
 from sys.databases where name='yourDatabase'

In case the database is online, the next step is to check is the user can access the database (if it's mapped to the database:)

 select * 
 from yourDB.sys.database_principals 
 where name = 'YourUserName';

This code is valid for checking existance of user corresponding to sql server login only (windows login can access the server using windows group login, so does the user)

In case the user does not exists we should create user for this login and give him appropriate permissions. Here is the link for user cteation CREATE USER (Transact-SQL)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download