简体   繁体   中英

Failing to authenticate with MongoDB using node.js

I've been attempting to add authentication to my mongo database.

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

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

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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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