简体   繁体   English

使用 MSSQL 将 SQL Server 与 Nodejs 连接时出现 SQL Server 错误“[ConnectionError: Login failed for user '****'.]

[英]SQL Server Error "[ConnectionError: Login failed for user '****'.] "while connecting SQL Server with Nodejs using MSSQL

I am getting below error我得到以下错误

      [ConnectionError: Login failed for user '****'.]
             name: 'ConnectionError',
             message: 'Login failed for user \'****\'.',
             code: 'ELOGIN' } "

Below is the code that i used to connect sql server with nodejs using mssql package.下面是我使用 mssql 包将 sql server 与 nodejs 连接的代码。

var sql = require('mssql');

var config = {
        server: "localhost\\",// You can use 'localhost\\instance' to connect to named instance 
        database: "***",
        user: "***",
        password: "",
        port: 1433
    };

function getdata() {
    var conn = new sql.Connection(config);
    var req = new sql.Request(conn);

    conn.connect(function (err) {
        If (err) {
            console.log(err);
            return;
        }
        req.query("select * From" ,function (err,recordset)  {
        If (err) {
            console.log(err);
            return;
        }
        else {
            console.log(recordset);

        }
        conn.close();


        enter code here
        });

    });

    }

    getdata();

Below is the message displayed when i add console to "conn" ,Kindly let me know if i have missed anything.以下是我将控制台添加到“conn”时显示的消息,如果我错过了什么,请告诉我。

config:
      { server: '',
        database: '',
        user: '',
        password: '',
        port: 1433,
        driver: 'tedious',
        options: {},
        stream: false,
        parseJSON: false },
     driver:
      { Connection:
         { [Function: TediousConnection]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        Transaction:
         { [Function: TediousTransaction]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        Request:
         { [Function: TediousRequest]
           EventEmitter: [Object],
           usingDomains: false,
           defaultMaxListeners: 10,
           init: [Function],
           listenerCount: [Function],
           __super__: [Object] },
        fix: [Function] } }
    started

I see that nobody answered this question here, 我看到这里没有人回答这个问题,

Below is the working code using 'mssql' , 以下是使用'mssql'的工作代码,

var sql = require('mssql');
var config = {
    server: 'localhost',
    database: 'sampleDB',
    user: 'YourUser',
    password: 'YourPassword',
    port: '1433'
};

function listProducts() {
    var conn = new sql.ConnectionPool(config);
    conn.connect().then(function () {
        var request = new sql.Request(conn);
        request.query("select * from products").then(function (recordSet) {
            console.log(recordSet);
            conn.close();
        }).catch(function (err) {
            console.log(err);
            conn.close();
        });
    }).catch(function (err) {
        console.log(err);
    });
}

listProducts();

In your solution, try to put port number in quotes and use 在您的解决方案中,尝试将端口号放在引号中并使用

var conn=new sql.ConnectionPool(config);

instead of, 代替,

var conn=new sql.Connection(config);

There is one other solution for making connection to sql server using 'tedious' , 还有一种解决方案,可以使用“乏味”与sql server建立连接,

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

var config = {
            server: "localhost", 
            userName: "YourUser",
            password: "YourPassword",
            database: "sampleDB"
     };

var connection = new Connection (config);

connection.on('connect', function(err){
    console.log(err);
    if(err!=null){
         console.log("not connected");
    }
    else{  
          console.log("Connected")
          connection.close();
    };
});

I hope this will help someone having the same issue. 我希望这可以帮助遇到同样问题的人。

It may be too late to answer, but this recently happened with me and it drove me crazy!!!现在回答可能为时已晚,但最近发生在我身上的这件事让我发疯了!!! I was trying to connect my db to express and I was working with windows authentication mode.我试图将我的数据库连接到 express 并且我正在使用 Windows 身份验证模式。 For two long days I kept googling and refreshing until I got this article: https://www.liquidweb.com/kb/troubleshooting-microsoft-sql-server-error-18456-login-failed-user/整整两天我一直在谷歌搜索和刷新,直到我得到这篇文章: https : //www.liquidweb.com/kb/troubleshooting-microsoft-sql-server-error-18456-login-failed-user/
So in a nutshell;简而言之;
First I installed the msnodesqlv8 driver for windows authentication, then in the my server on ssms, I right clicked on my server then in properties and then security, I enabled sql server and windows authentication mode, then in the object explorer, clicked on the plus next to the server, then security, then logins.首先,我安装了用于 Windows 身份验证的msnodesqlv8驱动程序,然后在 ssms 上的我的服务器中,我右键单击我的服务器,然后在属性和安全性中,我启用了 sql server 和 windows 身份验证模式,然后在对象资源管理器中,单击加号在服务器旁边,然后是安全,然后是登录。 There I saw sa with a cross next to it.在那里我看到旁边有一个十字架的sa In it's properties, I changed my password to something easier to remember (optional), then in the status, set the login to enable.在它的属性中,我将密码更改为更容易记住的密码(可选),然后在状态中,将登录设置为启用。 PHEW!呸!
Now my config code: <br / >现在我的配置代码:<br />

const config = {
user: 'sa',
password: '123',
driver: "msnodesqlv8",
server: 'UZAIR-S_PC\\SQLEXPRESS',
database: 'LearningExpressJS',
options: {
    trustedconnection: true,
    enableArithAbort : true, 
    trustServerCertificate: true,
    instancename : 'SQLEXPRESS'
},
port: 58018

} }

This works finally !!!!!!!!!!!!!!!这终于奏效了!!!!!!!!!!!!!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM