Mongoose: How to specify `--authenticationDatabase` in mongoose?

Created on 23 Feb 2016  ·  6Comments  ·  Source: Automattic/mongoose

Works in the shell:

$ mongo -u monguser -p mongpass 192.168.2.2/ps --authenticationDatabase=admin
MongoDB shell version: 3.2.3
connecting to: 192.168.2.2/ps
Server has startup warnings: 
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] 
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] 
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-23T01:59:10.522+0000 I CONTROL  [initandlisten] 
> db.foo.insert({can:'haz'})
WriteResult({ "nInserted" : 1 })

Fails in mongoose:

'use strict';

const mongoose = require('mongoose');

const uri = 'mongodb://monguser:[email protected]:27017/ps'

console.log('mongoose.connect =', mongoose.connect(uri, (err) => {
    err && console.log('err =', err);
}));
// For `mongoose.connect` output, see: https://gist.github.com/SamuelMarks/dedd863742f90cf2f333
err = { [MongoError: Authentication failed.]
  name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.' }

So it's save to assume that authenticationDatabase needs to be set somehow, maybe in the config parameter. _What's the syntax?_

Most helpful comment

Easiest way is to specify authSource in the uri

const uri = 'mongodb://monguser:[email protected]:27017/ps?authSource=admin';

All 6 comments

Easiest way is to specify authSource in the uri

const uri = 'mongodb://monguser:[email protected]:27017/ps?authSource=admin';

Thanks

@vkarpov15 you saved my day. Thanks a lot.

Anybody know how to specify authSource when using mongodump command? :]

@ORESoftware mongodump calls it authenticationDatabase: https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption-authenticationdatabase

Was this page helpful?
0 / 5 - 0 ratings