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"

Related questions

0 votes
1 answer
asked Jan 28, 2020 in Web Technology by ashely (50.2k points)
0 votes
2 answers
asked Sep 4, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
asked Feb 14, 2020 in Web Technology by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...