Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server 2012 ECONNREFUSED #65

Closed
adviner opened this issue Dec 8, 2012 · 20 comments
Closed

SQL Server 2012 ECONNREFUSED #65

adviner opened this issue Dec 8, 2012 · 20 comments

Comments

@adviner
Copy link

adviner commented Dec 8, 2012

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

@pekim
Copy link
Collaborator

pekim commented Dec 30, 2012

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.

@flybluewolf
Copy link

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

@pekim
Copy link
Collaborator

pekim commented Dec 30, 2012

    debug=> connection to undefined:1433 closed

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

@flybluewolf
Copy link

dbServer: 192.168.11.8\SQLEXPRESS2008
dbPort: 1433

@pekim
Copy link
Collaborator

pekim commented Dec 30, 2012

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

@adviner
Copy link
Author

adviner commented Dec 30, 2012

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 = '';
}

@flybluewolf
Copy link

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

@pekim
Copy link
Collaborator

pekim commented Dec 30, 2012

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.

@adviner
Copy link
Author

adviner commented Dec 30, 2012

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

@pekim
Copy link
Collaborator

pekim commented Dec 30, 2012

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.

@pekim pekim closed this as completed Dec 30, 2012
@adviner
Copy link
Author

adviner commented Dec 30, 2012

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

@abaltra
Copy link

abaltra commented Mar 27, 2013

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.

@pekim
Copy link
Collaborator

pekim commented Mar 27, 2013

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 #65 (comment) 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.

@abaltra
Copy link

abaltra commented Mar 27, 2013

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.

@abaltra
Copy link

abaltra commented Mar 27, 2013

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

@pekim
Copy link
Collaborator

pekim commented Mar 27, 2013

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

@abaltra
Copy link

abaltra commented Mar 27, 2013

Cloned the latest version from master

@Beniy
Copy link

Beniy commented Aug 23, 2016

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.

@arthurschreiber
Copy link
Collaborator

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

@Beniy
Copy link

Beniy commented Aug 23, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants