How to do user authentication in MongoDB, Prevent others to access MongoDB ? is an article sent to you by the InApps editorial team. Hope readers will have more useful knowledge at www.inapps.net


You are viewing the article: How to do user authentication in MongoDB, Prevent others to access MongoDB ?

MongoDB, a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.

Here I am going to explain how to apply user-name and password(authenticate) to mongoDB’s database.

#1. Open shell command. Fire this command

command – mongo
output-
MongoDB shell version: 3.0.4
connecting to: test

#2.

command – show dbs;
output-
admin 0.078GB
local 0.078GB
auth_db 0.078GB

#3.

command – use auth_db;
output-
switched to auth_db

Here, we have selected auth_db Database for apply authentication.

#4. Fire query :

db.createUser({user:”YOUR_USERNAME”, pwd:”YOUR_PWD”,roles:[“readWrite”,”dbAdmin”]})

We have created User of database ‘auth_db’. You can set your user-name and password to above query.

Make sure authentication is true. You can set authentication = true in MongoDB.conf file.

  • Option 1: In mongod.conf, Remove comment and make it true.
    auth=true;- restart mongoDB – sudo service mongod restart
  • Option 2 : command to run mongodb with auth true :- mongod -auth

#5. In database config files in Nodejs

var mongoose = require('mongoose');
var uri = 'mongodb://localhost:27017/auth_db';
var options = {
user: 'YOUR_USERNAME',
pass: 'YOUR_PWD'
}
mongoose.connect(uri, options, function(err, result){
console.log('err -->' + JSON.stringify(err));
});

Without user-name and password, would not able to perform an operation with the database. If you don’t want to use authentication to database just make auth=true is commented in MongoDB.conf file or start mongo without -auth i.e mongo . Please do not forget restart MongoDB.

Read More:   Update What can Make Your Application Fail to Mark Presence in 2019

You can drop a comment for any questions or feedback. We Will get back to you soon.




Follow this to make sure you’ve got How to do user authentication in MongoDB, Prevent others to access MongoDB ?. Save and share with those around you these extras.
To learn more about ECOMMERCE DEVELOPMENT

Contact us:
www.inapps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...