Tedious: Failed to connect to local sqlexpress - but can connect in management studio

Created on 30 Nov 2017  ·  4Comments  ·  Source: tediousjs/tedious

I had this happen a while ago but enabling tcp/ip seemed to fix it but now I cannot connect again and I cant figure out why. Tcp seems to be enabled in configuration manager. Please am I doing something wrong?

As a feature request it would be good to have better feedback if we are doing this wrong or if possible to make tedious use the same logic to connect as sql-server management studio.

This is with sql-server express 2008r2, [email protected]

var Connection = require('tedious').Connection;
var config = {
  userName: 'myuser',
  password: 'password',
  server: 'localhost',
  options: {
      database: 'testdb',
      instanceName: 'sqlexpress'
  }
}
var connection = new Connection(config);
connection.on('connect', function(err) {
  if (err) {
    console.log(err);
  } else {
    console.log('Connected');
  }
});
{ ConnectionError: Failed to connect to localhost:undefined in 15000ms
    at ConnectionError (C:\work\sqltest\node_modules\tedious\lib\errors.js:12:12)
    at Connection.connectTimeout (C:\work\sqltest\node_modules\tedious\lib\connection.js:789:28)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)
  message: 'Failed to connect to localhost:undefined in 15000ms',
  code: 'ETIMEOUT' }

image

^ this works fine

Most helpful comment

Thanks so much - it was just the SQL Server Browser service was not started. I guess management studio must do some magic when connecting to localhost.

All 4 comments

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

@spacem since you're using options.instanceName, can you double check the above conditions? If those are not satisfied, try using options.port instead.

Failed to connect is a generic error, you can notice almost similar messages in other database drivers too, if instance or port is wrong. Error message with more useful details are available only after connection succeeds, as driver doesn't have much detail before that 😅

Thanks so much - it was just the SQL Server Browser service was not started. I guess management studio must do some magic when connecting to localhost.

Tedious Connect basically looking for the SQL Server Browser and SQL Server Agent mode to be on with TCP Enabled optioned for the Protocols.

So if any of the service or port is disabled then It prompts for timeout exception

I followed the directions on this SO answer and it worked for me:

  • Enable and Start SQL Server Browser
  • Enable TCP/IP Protocol for SQLEXPRESS

    • You can run SQL Server Configuration Manager with one of the commands listed on this page.

      Screenshot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tvrprasad picture tvrprasad  ·  5Comments

jstephens7 picture jstephens7  ·  5Comments

SaloniSonpal picture SaloniSonpal  ·  5Comments

malarcherc picture malarcherc  ·  9Comments

tvrprasad picture tvrprasad  ·  6Comments