简体   繁体   English

无法使用node.js向MongoDB进行身份验证

[英]Failing to authenticate with MongoDB using node.js

I've been attempting to add authentication to my mongo database. 我一直在尝试向我的mongo数据库添加身份验证。

So following various tutorials and solving some problems (my server was 2.4 and needed to be upgraded), I was finally able to add a user 因此,按照各种教程并解决了一些问题(我的服务器是2.4,需要升级),我终于能够添加一个用户

I run the server in one shell 我在一个外壳中运行服务器

mongod --dbpath ./

then in another I run 然后在另一个我跑

mongo
use admin
db.createUser({
     user:"user",
     pwd:"pass",
     roles: [{role:"userAdminAnyDatabase", db:"admin"}]
})

I get a success message, so then I quit out of both the server and the shell 我收到一条成功消息,因此我退出了服务器和外壳程序

In another shell I run 在另一个外壳中,我运行

mongod --dbpath ./ --auth

Then I run 然后我跑

node server.js

Within the server.js file, there is the following code 在server.js文件中,有以下代码

mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/db', function(err, db) {
    if(err) throw err;  
    db.authenticate("user", "pass", function(err, res) {
        if(err) throw err;      
        // code here
    });
});

But this throws a mongo error, that authentication failed 但这会引发mongo错误,即身份验证失败

Am I trying to authenticate incorrectly, or doing something else wrong? 我是在尝试认证不正确,还是在做其他错误的事情? I'm not understanding my mistake. 我不明白我的错误。

Here is the error 这是错误

C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\connection\base.js:245
    throw message;      
          ^
MongoError: auth failed
    at Object.toError (C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\utils.js:110:11)
    at C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\db.js:1128:31
    at C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\db.js:1843:9
    at Server.Base._callHandler (C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\connection\base.js:445:41)
    at C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\connection\server.js:468:18
    at MongoReply.parseBody (C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\responses\mongo_reply.js:68:5)
    at null.<anonymous> (C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\connection\server.js:426:20)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (C:\Users\Corey Byrne\Documents\DocuBuilder\Email Project\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:201:13)
    at EventEmitter.emit (events.js:98:17)

看来您是在名为admin的数据库中创建用户,然后尝试登录名为db

It fails when you pass in the db to connect to in the url. 当您传递db以连接到URL时,它将失败。 Looks like the authentication is done against the same db. 看起来认证是针对同一数据库完成的。 Instead you should do: 相反,您应该这样做:

    const client = await new Promise((res, rej) =>
      MongoClient.connect(
        K2_MONGODB_URL,
        {
          auth: {
            authdb: 'admin'
          },
          useNewUrlParser: true 
        },
        (err, client) => (err ? rej(err) : res(client))
      )
    )

where K2_MONGODB_URL=mongodb://user:password@localhost:27017 其中K2_MONGODB_URL=mongodb://user:password@localhost:27017

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

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