A2oz

How do I create an admin user in MongoDB?

Published in Database Administration 1 min read

You can create an admin user in MongoDB using the db.createUser() method.

Here's how:

  1. Connect to the admin database:
    use admin
  2. Create the user using db.createUser():
    db.createUser({
      user: "your_username",
      pwd: "your_password",
      roles: [ { role: "root", db: "admin" } ]
    })

Example:

use admin
db.createUser({
  user: "adminuser",
  pwd: "password123",
  roles: [ { role: "root", db: "admin" } ]
})

This creates an admin user named adminuser with the password password123. The roles array specifies the user's privileges. In this case, the root role grants full access to the admin database.

Important Note:

  • Replace your_username and your_password with your desired credentials.
  • This method provides the highest level of access. For more granular control, explore other available roles.

Related Articles