How to do user authentication in MongoDB, Prevent others to access MongoDB ?

Apr 21st, 2016

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.

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

Comments are closed.

Let's Discuss Your Project

Get free consultation and let us know your project idea to turn
it into an amazing digital product.

Let’s talk

NEWS & BLOG

Related Blogs

Digital Transformations Re-Engineering for Success for the 21st Century

Big Data Apr 25th, 2017

Digital Transformations Re-Engineering for Success for ...

Read more
Auto Backup MongoDB Database with NodeJS on server

Big Data Jan 13th, 2017

Auto Backup MongoDB Database with NodeJS on server...

Read more
8 Simple steps to install MongoDB with authentication on EC2 AMI Linux

Big Data Jan 10th, 2017

8 Simple steps to install MongoDB with authentication o...

Read more

INQUIRY

Let's get in touch

UNITED STATES

31236 Meadowview Square,
Delmar, DE 19940, USA

Sales: +1 667 771 6758

UNITED KINGDOM

13 Layton Road, Hounslow,
London, TW3 1YJ

Sales: +44 7404 607567

INDIA

2nd Floor, Sun Avenue One, Bhudarpura, Ayojan Nagar, Nr. Shyamal Cross Road, Ahmedabad, Gujarat-380006

Sales: +91 635-261-6164

For Project Inquiries

biolah

depo 25 bonus 25 to 5x

depo 25 bonus 25

depo 25 bonus 25

mndrmndr.com

bonusdeposit.net

https://www.greentourstanzania.com/wp-includes/customize/

https://temp1.novotest.biz/id/

depo 25 bonus 25

https://sumberjo-blitar.desa.id/images

https://sumberjo-blitar.desa.id/data

depo 25 bonus 25 to 5x

depo 25 bonus 25

https://www.greentourstanzania.com/wp-includes/js/product/

https://smpabbs.sch.id/gacor/100/

https://smpabbs.sch.id/gacor/bonus/

deposit 25 bonus 25

depo 25 bonus 25

bonus new member 100

https://ppdb.smk-kosgoro.sch.id/data/depo 25 bonus 25https://jesus.nouvellevie.com/wp-includes/images/Getoko.iddepo 25 bonus 25https://bonus-baru.s3.ap-southeast-1.amazonaws.com/link-daftar-slot-gacor.htmlhttps://bonus-baru.s3.ap-southeast-1.amazonaws.com/scatter-pink-paling-gacor.htmlhttps://worldlisteningproject.org/wp-includes/depo25bonus25/bonus new member 100depo 25 bonus 25