Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to be admin to create a new user in MongoDB?

Tags:

mongodb

I want to create a new user in MongoDB. Do I do that by logging in as admin (use admin) and then using the command add user to create new user?

like image 620
user626206 Avatar asked Feb 21 '11 09:02

user626206


1 Answers

If no users created you can create new user without any authentification, but if you have created admin user for specific database you should authentifcate, and then perform any operation.

Documentation:

If no users are configured in admin.system.users, one may access the database from the localhost interface without authenticating. Thus, from the server running the database (and thus on localhost), run the database shell and configure an administrative user:

 $ ./mongo
 > use admin
 > db.addUser("theadmin", "anadminpassword")

We now have a user created for database admin. Note that if we have not previously authenticated, we now must if we wish to perform further operations, as there is a user in admin.system.users.

 > db.auth("theadmin", "anadminpassword")

We can view existing users for the database with the command:

 > db.system.users.find()

Now, let's configure a "regular" user for another database.

 > use projectx
 > db.addUser("joe", "passwordForJoe")
like image 127
Andrew Orsich Avatar answered Oct 08 '22 00:10

Andrew Orsich