1

I have databases A, B, C, in addition to system's admin

I want to add a user that has readWrite access to A, B, C.

// attempt 1
use admin
db.createUser({ user: "Bob", pwd: "1234", roles: [
    { role: "readWriteAnyDatabase", db: "admin" }
] })

.

// attempt 2
use admin
db.createUser({ user: "Bob", pwd: "1234", roles: [
    { role: "readWrite", db: "A" },
    { role: "readWrite", db: "B" },
    { role: "readWrite", db: "C" }
] })

For these attempts, the user is stored in db admin. So how can I authenticate using admin but perform actions in one of A, B, C?

var uri = 'mongodb://localhost/A';
var opts = {
    user: 'Bob',
    pass: '1234',
    auth: { authDb: 'admin' }
};
mongoose.connect(uri, opts); // auth fails

Is my only solution to separately add a user on each of A, B, C ? Ex.

use A
db.createUser({...})
use B
db.createUser({...})
use C
db.createUser({...})

Seems foolish.

Kat
  • 473
  • 5
  • 17
  • Very late, but it's possible your auth was failing because `authDb` should be `authdb`. I tried `authDb` and got auth failed, then `authdb` and it worked. – Nick Spacek Dec 15 '15 at 12:15

2 Answers2

5

Easiest way is to specify authSource in the uri

const uri = 'mongodb://monguser:mongpass@192.168.2.2:27017/ps?authSource=admin';
Developerium
  • 7,155
  • 5
  • 36
  • 56
0

It seems you have to use one Mongoose connection per database. Have a look to Mongoose and multiple database in single node.js project .

Community
  • 1
  • 1
Niabb
  • 139
  • 5