When i try to connect like this it fails when i use database as eventsstore
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.