ConnectionError: Login failed for user 'MyUser'
Hello, I have a Microsoft SQL server on my laptop and Im trying to connect to it but It just keeps giving me login failed. This is what my code looks like
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var config = {
server: "localhost",
authentication: {
type: "default",
options: {
userName: "MyUser",
password: "pass"
}
}
};
var connection = new Connection(config);
I dont know why this is happening, please help :)
Did you enable TCP/IP connections? They're disabled by default. Can you connect using another tool (e.g. SQL Server Management Studio)?
Did you enable TCP/IP connections? They're disabled by default. Can you connect using another tool (e.g. SQL Server Management Studio)?
Yes I have enabled it and SQL server management studio works fine with the database
Hi @filipanton, you could also double-check the user account that you were using for the connection is enabled for the server? Just wondering, which login method were you using for the server within the SQL server management studio?
Hi @filipanton, you could also double-check the user account that you were using for the connection is enabled for the server? Just wondering, which login method were you using for the server within the SQL server management studio?
Im not sure I understand what you mean.
All I did was "CREATE USER MyUser WITH PASSWORD = 'pass';" in the management studio and then give it all privileges. If I click on the database in the management studio and then security > users, the MyUser user shows up in the list.
@filipanton What's the port for your SQL Server? You can check in SSMS:
USE master
GO
xp_readerrorlog 0, 1, N'Server is listening on'
GO
And then set the port in the options under your config that the above sql command gives, like:
{
"config": {
"server": "localhost",
"authentication": {
"type": "default",
"options": {
"userName": "xxx",
"password": "xxx"
}
},
"options": {
"port": 60543,
"database": "master"
}
}
}
In my case, the default port 1433 didn't work, so using my SQL Server's specific port allowed me to gain access. Hopefully that works for you also?
Hi @filipanton, you could also double-check the user account that you were using for the connection is enabled for the server? Just wondering, which login method were you using for the server within the SQL server management studio?
I used the windows login for the database, and i couldn't connect using tedious on NodeJS app!! what should I do ?
Hi @EG-DEV5, if you do not own the data base and you are not on NodeJS 17, then you can use out NTLM authentication type wich allows you to provide you window credential for logging in.
config = {
server: "localhost",
authentication: {
type: "ntlm",
options: {
domain: "<Windows user domain>",
userName: "<Windows user name>",
password: "<Windows user password>",
intanceName: "<SQL server instance name>"
}
},
}
If you own the SQL Server instance, then you can easily create a user using SQL Server Management Studio, then use that user for logging via tedious default authentication.
What about if someone tries with a windows login,?, Then how to do that? instead of using specific user login in the SQL server.
Hi @erpramod , unfortunately, tedious does not support windows login yet. If you can only work with windows login then may be this comment can help you: https://github.com/tediousjs/node-mssql/issues/1270#issuecomment-878363118. We will also look into the window login support on our side as well.
@filipanton What's the port for your SQL Server? You can check in SSMS:
USE master GO xp_readerrorlog 0, 1, N'Server is listening on' GOAnd then set the port in the
optionsunder your config that the above sql command gives, like:{ "config": { "server": "localhost", "authentication": { "type": "default", "options": { "userName": "xxx", "password": "xxx" } }, "options": { "port": 60543, "database": "master" } } }In my case, the default port
1433didn't work, so using my SQL Server's specific port allowed me to gain access. Hopefully that works for you also?
That worked for me!