Tedious: SQL Server 2012 ECONNREFUSED

Created on 8 Dec 2012  ·  20Comments  ·  Source: tediousjs/tedious

Using latest nodejs and tedious versions and the following code:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

var config =
{
server: '192.168.1.113',
userName: 'myUserName',
password: 'myPassword',
options:
{
database: 'MyDatabase',
port: '1433',
encrypt: true,
driver: 'SQL Server Native Client 11.0'
},
debug:
{
packet: true,
data: true,
payload: true,
token: true,
log: true
}
};

var connection = new Connection(config);

connection.on('connect', connected);
connection.on('infoMessage', infoError);
connection.on('errorMessage', infoError);
connection.on('end', end);
connection.on('debug', debug);

function connected(err)
{
console.log('connected=> ' + err);
}

function infoError(info)
{
console.log('infoError=> ' + info);
}

function debug(message)
{
console.log('debug=> ' + message);
}

function end()
{
}

I get the following error after i run it:

node test_sqlserver_tedious.js
debug=> connection to 192.168.1.113:1433 - failed Error: connect ECONNREFUSED
debug=> State change: Connecting -> Final
debug=> connection to 192.168.1.113:1433 closed
debug=> State change: Final -> Final

Any ideas? I could help you test the source if you show me how.

Thanks

Most helpful comment

ECONNREFUSED suggests that 192.168.1.113 is not listening on port 1433.

Does the SQL Server instance definitely have TCP enabled?
Can you telnet to port 1433 on 192.168.1.113 (from the system where you are using tedious)? If not, can you telnet to port 1433 directly from 192.168.1.113? If no, then it suggest that the SQL Server is not listening on the port. If yes, then it suggests that something (maybe a firewall) is the cause of the problem.

All 20 comments

ECONNREFUSED suggests that 192.168.1.113 is not listening on port 1433.

Does the SQL Server instance definitely have TCP enabled?
Can you telnet to port 1433 on 192.168.1.113 (from the system where you are using tedious)? If not, can you telnet to port 1433 directly from 192.168.1.113? If no, then it suggest that the SQL Server is not listening on the port. If yes, then it suggests that something (maybe a firewall) is the cause of the problem.

I get the error:

debug=> connection to undefined:1433 - failed Error: connect ECONNREFUSED
debug=> State change: Connecting -> Final
debug=> connection to undefined:1433 closed
debug=> State change: Final -> Final

    debug=> connection to undefined:1433 closed

The undefined looks suspicious. I'd expect to see the server name or IP address there.

dbServer: 192.168.11.8\SQLEXPRESS2008
dbPort: 1433

@flybluewolf please take a look at issue #68, as I think that my answer there is relevant.

OK it seems to connect now. But when i try to do a simple select it just hangs.

Here is the debug output:

debugger listening on port 51981
debug=> connected to 127.0.0.1:1433
debug=> State change: Connecting -> SentPrelogin
debug=> State change: SentPrelogin -> SentLogin7WithStandardLogin
infoError=> [object Object]
infoError=> [object Object]
debug=> Packet size changed from 4096 to 4096
debug=> State change: SentLogin7WithStandardLogin -> LoggedInSendingInitialSql
debug=> State change: LoggedInSendingInitialSql -> LoggedIn

Here is my code:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

var config =
{
server: "127.0.0.1",
userName: "username",
password: "userpassword",
database: "MyDatabase",
options:
{
database: "MyDatabase",
// port: "1433",
//encrypt: true,
// driver: "SQL Server Native Client 11.0"
},
debug:
{
packet: true,
data: true,
payload: true,
token: true,
log: true
}
};

var connection = new Connection(config);

connection.on('connect', connected);
connection.on('infoMessage', infoError);
connection.on('errorMessage', infoError);
connection.on('end', end);
connection.on('debug', debug);

function connected(err)
{
executeStatement();
}

function executeStatement()
{
var request = new Request("Select * from Property.PropertyGroups", function(err, row_count)
{
if(err)
{
}
else
{
console.log(row_count);
}

    connection.close();
});

request.on('row', function(columns)
{
    columns.forEach((function(column)
    {
        console.log(column.value);
    }))
})

}

function infoError(info)
{
var dd = info;
console.log('infoError=> ' + info);
}

function debug(message)
{
var dd = message;
console.log('debug=> ' + message);
}

function end()
{
var dd = '';
}

I execute 'netstat -na', but I don't find UDP 1444 port

I don't see any call to connection.exec...(request) in your code. So the request is not being executed. That's consistent with the debug=> State change: ... messages. They stop once authentication is complete.

Ugh, OK, that did the trick. Thank you so much :D

I take it that @adviner and @flybluewolf are the same person, and that this issue is only dealing with one problem?
If I'm mistaken, please feel free to re-open.

No they are two different people. I started the post and now have it working.

Hello, sorry for reviving this, but I'm getting an issue using the last code snippet posted by @adviner (with credentials properly changed, of course). I can never get past the SentPreLogin phase. Using the same code, all the output I get is:

debug=> connected to SERVERt:1433
debug=> State change: Connecting -> SentPrelogin
debug=> State change: SentPrelogin -> SentLogin7WithStandardLogin
debug=> connection to SERVER:1433 closed
debug=> State change: SentLogin7WithStandardLogin -> Final
debug=> connection to SERVER:1433 closed
debug=> State change: Final -> Final

(server url obscured).

The database is running in Azure and I can telnet to it from the machine running node.

Any help would be appreciated.

As I understand it, Azure databases require an encrypted connection.
So presumably you're connecting with encrypt: true in the connection options? I ask because https://github.com/pekim/tedious/issues/65#issuecomment-11765693 has this commented out.

The code you're using doesn't check for an error in the Connection callback, the connected function. You should fix that, as you may be getting an error reported there.

Encrypted tedious connections with node v0.10 (and v0.9) were broken. See #86 for a bit more info, and a commit that I believe fixes the problem.

The same commit that fixes the v0.10 issue, also defaults the encryption cipher to RC4-MD5, because I've not managed to get encrypted connections working with DES-CBC3-SHA yet. This applies to node v0.8 as well.

Can you try using the latest code in master, to pick up the fix, please?

If that still fails, can you put all of the debug output in to a gist or pastebin please? You should get lots of detail, including hex dumps of the TDS packets. You will probably want to redact some details from the login7 packet.

Thanks for the fast response. I'm past the previous error, but now I get the following output:

debug=> connected to XXX.database.windows.net:1433
debug=> State change: Connecting -> SentPrelogin
debug=> State change: SentPrelogin -> SentTLSSSLNegotiation
debug=> TLS negotiated (RC4-MD5, TLSv1/SSLv3)
debug=> State change: SentTLSSSLNegotiation -> SentLogin7WithStandardLogin
infoError=> [object Object]
{"number":40531,"state":1,"class":11,"message":"Server name cannot be determined. It must appear as the first segment of the server's dns name (servername.database.windows.net). Some libraries do not send the server name, in which case the server name must be included as part of the user name (username@servername). In addition, if both formats are used, the server names must match.","serverName":"","procName":"","lineNumber":1,"name":"ERROR","event":"errorMessage"}
Login failed; one or more errorMessage events should have been emitted
debug=> State change: SentLogin7WithStandardLogin -> Final
debug=> connection to XXXX.database.windows.net:1433 closed
debug=> State change: Final -> Final

The servername is marked as XXX (I assume), and it's part of the connection string for the azure db.

The last issue was a problem with our connection string Everything's working now. Thank you!

Fantastic.
Was this with the latest code from master, or a released version of tedious?

Cloned the latest version from master

Dear Experts,
I have faced same error but the problem is my error is happenning after a connection is established and it's inserting big files to database, I do not have any issue with small files:

adding data to databse
getting columns from db
got table columns successfully
adding rows to DB
rows inserted succesffuly
adding data to databse
getting columns from db
got table columns successfully
adding rows to DB
rows inserted succesffuly
adding data to databse
getting columns from db
got table columns successfully
adding rows to DB
rows inserted succesffuly
adding data to databse
getting columns from db
got table columns successfully
adding rows to DB
{ [ConnectionError: Failed to connect to localhost:1433 - connect EADDRINUSE 127 .0.0.1:1433] name: 'ConnectionError', message: 'Failed to connect to localhost:1433 - connect EADDRINUSE 127.0.0.1:1 433', code: 'ESOCKET' }

Your support is appreciated.

@Beniy Do you have example code that we can use to reproduce this?

Dear Arthurschreiber,
Actually the problem cannot be related to code because sometimes it is parsing text file and inserting to db but sometimes it's showing that error, even with same file and same condition but in different time.
I want to know any other dependencies to this task?

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

David-Engel picture David-Engel  ·  5Comments

arthurschreiber picture arthurschreiber  ·  8Comments

spacem picture spacem  ·  4Comments

SaloniSonpal picture SaloniSonpal  ·  5Comments

aniltomar10 picture aniltomar10  ·  5Comments