You can create an admin user in MongoDB using the db.createUser()
method.
Here's how:
- Connect to the
admin
database:use admin
- 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
andyour_password
with your desired credentials. - This method provides the highest level of access. For more granular control, explore other available roles.