Node-mssql: Can not connect to the database

Created on 10 May 2016  路  6Comments  路  Source: tediousjs/node-mssql

Hi,

I am having problems creating a connection string that works.

Server version: SQL Server 2014 Express.

I can login to SQL Management Studio with the same credentials.
Server name: (local)SQLEXPRESS2014
User: test
Pass: testtest

To login with nodejs i tried this:

var sql = require('mssql');

var config = {
  connectionString: 'Server=(local)\\SQLEXPRESS2014;Database=test-server;UID=test;PWD=testtest;',
};

var connection = new sql.Connection(config);
connection.connect(function(err) {
    console.log(err); // This is always getting hit
});

That however returns the following error:

{ [ConnectionError: Failed to connect to undefined:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to undefined:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }

Same error if i change the config to:

var connection = new sql.Connection({
    user: 'test',
    password: 'testtest',
    server: '(local)\\SQLEXPRESS2014', // or localhost\\SQLEXPRESS2014
    database: 'test-server'
});

Somehow it does not translate the connection correctly.

(My file only contains the first lines at the top to connect. Otherwise it's completely empty. I wanted to get the connection to work before writing any other code.)

discussion

Most helpful comment

This is how you should do it, if you're connecting to named instance.
http://stackoverflow.com/questions/21204411/connect-to-sql-server-with-mssql-for-node-js

All 6 comments

This is how you should do it, if you're connecting to named instance.
http://stackoverflow.com/questions/21204411/connect-to-sql-server-with-mssql-for-node-js

That worked a lot better. Thank you.

I am encountering this error when I try ti connect the Mssql with nodejs. I don't know what is going wrong.
my config will look like
var dbConfig = {
user: 'Harsha Varun',
password: '',
server: "localhost",
driver: 'tedious',
database: 'nodetest'
};

tedious deprecated The default value for options.encrypt will change from false to true. Please pass false explicitly if you want to retain current behaviour. node_modulesmssqllibtedious.js:212:23
{ ConnectionError: Login failed for user''
code: 'ELOGIN',
originalError:
{ ConnectionError: Login failed for user''
message: 'Login failed for user 'asdasd'.',
code: 'ELOGIN' },
name: 'ConnectionError' }

@PatrikFomin @snovak7 If you guys can help me it would be great. Thanks in advance.

@harshavarun25 this is not the right issue for your question, but your sql server clearly says that the user asdasd doesn't exist or the password is incorrect.

@snovak7 I know it looks pretty straightforward for me also when I looked into it. When i tried with the same user name and password I was able to login from ms sql management studio. That is why I was wondering what could go wrong.

@harshavarun25 Yes, you need access to the server as well as to the database. encrypt option is only for the connection encryption, unrelated to the problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agilitehub picture agilitehub  路  4Comments

AlexSenchenko picture AlexSenchenko  路  6Comments

sizovilya picture sizovilya  路  3Comments

ryankelley picture ryankelley  路  4Comments

danpetitt picture danpetitt  路  3Comments