Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I want to set up user name & password authentication for my MongoDB instance so that any remote access will ask for the user name & password. I tried the tutorial from the MongoDB site and did the following:

use admin 

db.addUser('theadmin', '12345'); 

db.auth('theadmin','12345');

After that, I exited and ran mongo again. And I don't need the password to access it. Even if I connect to the database remotely, I am not prompted for username & password.

2 Answers

0 votes
by (106k points)
To secure MongoDB with username and password you need to start mongod with the --auth option after setting up the user.

You need to start MongoDB without access control:-

mongod --dbpath /data/db

Then connect to the instance:-

mongo

After that create the user:-

use some_db 

db.createUser( 

   { 

    user: "myNormalUser", 

    pwd: "xyz123",

    roles: [ { role: "readWrite", db: "some_db" }, 

             { role: "read", db: "some_other_db" } ]

    } 

)

0 votes
by (107k points)

  1. First start MongoDB without access control.

  2. Then connect to the instance.

  3. After that create the user.

  4. Then stop the MongoDB instance and start it again with access control.

  5. And at last, connect and authenticate as the user.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 22, 2020 in SQL by Appu (6.1k points)
0 votes
2 answers
asked Sep 13, 2019 in SQL by Sammy (47.6k points)
+2 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...