简体   繁体   中英

Nodejs mongoClient.connect fails due to username/password setup in mongo

When i try to connect like this it fails when i use database as eventsstore

mongoClient.connect

MongoClient.connect('mongodb://tom:pass@localhost:27017/eventsstore',function(err,db){ if(err){ console.log('NOT CONNECTED') } console.log('CONNECTED') var collection = db.collection('events').insert({mydata:123}, function(err, res){}) })

Note : Also database eventsstore is created by admin login

It Passes/connects successfully when i use database as admin

MongoClient.connect('mongodb://tom:pass@localhost:27017/admin',function(err,db){ if(err){ console.log('NOT CONNECTED') } console.log('CONNECTED') var collection = db.collection('events').insert({mydata:123}, function(err, res){}) })

this is how my mongo user tom SUPERUSER permission look in mongo

db.getUser('tom')

[ { "role" : "readWrite", "db" : "admin" }, { "role" : "userAdmin", "db" : "admin" }, { "role" : "dbOwner", "db" : "admin" }, { "role" : "dbAdminAnyDatabase", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" } ]

You need to provide authentication db

var opts = {db: {authSource: 'admin'}}

mongo.connect('mongodb://tom:pass@localhost:27017/admin', opts, function(err, db) {

}

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