Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k 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 (107k 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: "Qwerty12345@", roles: [{role: "userAdminAnyDatabase", db: "databaseName"}] })

After performing the above syntax, just enable the auth again and restart the service.

MongoDB is an important part of full-stack development. Enroll in this Full Stack Developer Training to start your journey in this field.

Related questions

0 votes
2 answers
asked Oct 17, 2019 in Web Technology by Sammy (47.6k points)
0 votes
2 answers
asked Sep 9, 2019 in SQL by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...