0 votes
1 view
ago in Web Technology by (28.7k points)

My MongoDB is hosted on compose.io and is called ScroungeBA. I try to create a user with some built-in roles which by the documentary only work in the admin database:

MongoDB provides all other built-in roles only on the admin database

So my question: What is that admin DB about? Is it the standard DB which always exists?

Furthermore, I have trouble with (using MongoDB shell version: 3.0.5):

$ use admin

switched to DB admin

$ db.auth("user", "secret")

Error: 18 Authentication failed.

I guess my user does exist in the ScroungeBA DB but not in the admin DB? How can I create a user in the admin DB since

db.createUser({user:"hello", pwd:"world", roles:[{role: "userAdmin", db: "admin"}]})

results in the error:

Error: couldn't add user: not authorized on admin to execute command { createUser: "hello", pwd: "xxx", roles: [ { role: "userAdmin", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } }

at Error (<anonymous>)

at DB.createUser (src/mongo/shell/db.js:1101:11)

at (shell):1:4 at src/mongo/shell/db.js:1101

1 Answer

0 votes
ago by (63.1k points)

For creating a user in the admin database, you mainly have to tentatively disable auth on your MongoDB instance.

After that, you can connect to your MongoDB shell and generate a user in the admin database.

Provide the user with the role userAdminAnyDatabase instead of just useAdmin as a user with the role userAdminAnyDatabase can manage the users of all the databases.

use admin

db.createUser({ user:"admin1", pwd: "[email protected]", roles: [{role: "userAdminAnyDatabase", db: "admin1"}] })

After that enable auth again and restart the service.

Related questions

0 votes
2 answers
asked Oct 17, 2019 in Web Technology by Sammy (47.8k points)
0 votes
2 answers
asked Sep 9, 2019 in SQL by Sammy (47.8k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !