The following are the steps to set the user as admin:
First, you have to begin MongoDB without any access control, for that refer to the following code:
mongod --port 27017 --dbpath /data/db1
Then you have to connect to the instance, refer the following code:
mongo --port 27017
After that, you can create a user administrator.
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Then just restart the MongoDB instance this time with the access control:
mongod --auth --port 27017 --dbpath /data/db1
Then, at last, validate as the user administrator.
Learn about full stack web development by signing up for this professional Full Stack Training offered by Intellipaat..
mongo --port 27017 -u "myUserAdmin" -p "abc123" \
--authenticationDatabase "admin"