Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I am working with the same localhost string on local and production. My connection string is as follows:

mongodb://localhost/mydb

I want to know the username and the password so that I can secure it.

1 Answer

0 votes
by (107k points)
edited by

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"

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...