0 votes
1 view
in Web Technology by (42.1k 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
by (90.8k points)
edited by

You have to temporarily disable auth on your MongoDB instance in order to generate users in the admin database.

After disabling the auth on your MongoDB, you can connect to your MongoDB shell and then generate a user in the admin database.

You can provide the user with the role named 'userAdminAnyDatabase' instead of providing 'useAdmin'  because a user with the role userAdminAnyDatabase can able to manage the users of all kinds of the databases.

use databaseName

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

After performing the above syntax, just enable the 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)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !