MongoDB "root" user MongoDB "root" user mongodb mongodb

MongoDB "root" user


The best superuser role would be the root.The Syntax is:

use admindb.createUser({    user: "root",    pwd: "password",    roles: [ "root" ]})

For more details look at built-in roles.

Hope this helps !!!


While out of the box, MongoDb has no authentication, you can create the equivalent of a root/superuser by using the "any" roles to a specific user to the admin database.

Something like this:

use admindb.addUser( { user: "<username>",          pwd: "<password>",          roles: [ "userAdminAnyDatabase",                   "dbAdminAnyDatabase",                   "readWriteAnyDatabase"] } )

Update for 2.6+

While there is a new root user in 2.6, you may find that it doesn't meet your needs, as it still has a few limitations:

Provides access to the operations and all the resources of the readWriteAnyDatabase, dbAdminAnyDatabase, userAdminAnyDatabase and clusterAdmin roles combined.

root does not include any access to collections that begin with the system. prefix.

Update for 3.0+

Use db.createUser as db.addUser was removed.

Update for 3.0.7+

root no longer has the limitations stated above.

The root has the validate privilege action on system. collections. Previously, root does not include any access to collections that begin with the system. prefix other than system.indexes and system.namespaces.


Mongodb user management:

roles list:

readreadWritedbAdminuserAdminclusterAdminreadAnyDatabasereadWriteAnyDatabaseuserAdminAnyDatabasedbAdminAnyDatabase

create user:

db.createUser(user, writeConcern)db.createUser({ user: "user",  pwd: "pass",  roles: [    { role: "read", db: "database" }   ]})

update user:

db.updateUser("user",{  roles: [    { role: "readWrite", db: "database" }   ]})

drop user:

db.removeUser("user")

or

db.dropUser("user")

view users:

db.getUsers();

more information: https://docs.mongodb.com/manual/reference/security/#read