Question

I have create an admin user for my mongo installation as follow:

> use admin   
> db.addUser( { user: "test",
              pwd: "password",
              roles: [ "dbAdminAnyDatabse", 
                        otherDBRoles:
                       {
                         "otherTestDB": [ "readWrite" ]
                       }]
                 } )

When I try to connect to "otherTestDB" using user: "test" and pwd: "password" with robomongo or java driver i get a wrong authentication error.

where mistake?

Was it helpful?

Solution

You have created a userid for the admin db, so to use that userid you have to connect to the admin db, not the otherTestDB. The otherDBRoles document controls access to other dbs while connected to the admin db as that user. So with the addUser as you have specified it the following command fails because the user is for the admin db, not for the otherTestDB:

$ mongo otherTestDB --username test --password password --eval 'printjson(db.c.findOne())'
connecting to: otherTestDB
Thu Feb 27 10:45:20.722 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228

whereas the following command, which connects to the admin db and then uses otherTestDB via getSiblingDB, succeeds:

$ mongo admin --username test --password password --eval 'printjson(db.getSiblingDB("otherTestDB").c.findOne())'
connecting to: admin
{ "_id" : ObjectId("530f5d904dbd43cfb46aab5b"), "hello" : "world" }

If you want a user that can authenticate when connecting to the otherTestDB with that userid and password, you will need to separately add such a user to the otherTestDB.

Does this help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top