0 votes
1 view
in Web Technology by (32.3k 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 (71.3k 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.

mongo --port 27017 -u "myUserAdmin" -p "abc123" \

  --authenticationDatabase "admin"

Related questions

0 votes
1 answer
0 votes
2 answers
asked Sep 4, 2019 in SQL by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 14 in Web Technology by ashely (32.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...