Nodejs authorizing the connection to MongoDB

Source: Internet
Author: User
Tags assert auth install mongodb

After MongoDB is installed, there is no user connection authorization authentication

Direct input MONGO into interactive mode on the console

Show DBS Use Databasenameshow collections

None of these basic commands are going to be a problem.

############################################

Add user authorization Authentication to MONGODB-----MONGO into interactive mode

Use admin  # to switch to the admin database # displays theDataSet----------------  # View user data in System.users ----db.adduser ('name') ,'pwd'# Add an Administrator account

Then open vi/etc/mongodb.conf.

Find #auth=true and comment out

Then restart the MongoDB database service

Sudo/etc/init.d/mongodb restart

At this point, MongoDB's auth configuration is complete

#################################

Create a Nodejs app

Add a MongoDB Plugin

NPM Install Mongodb-save

Use

var Mongodb=require ('MongoDB'). Mongoclient, URL='Mongo://username:[email protected]:p ort/database?authmechanism=mongodb-cr&authsource=admin',      assert=require ('assert'); Mongodb.connect (Url,function (err,db) {assert. Equal (Err,null); var col=db.collection ('Collection-name');       Col.find ({}). ToArray (function (err,data) {console.log (data); })      })

Where authmechanism is the authorization mechanism this can be viewed through the above Db.system.users.find ()

Reference Address: HTTP://MONGODB.GITHUB.IO/NODE-MONGODB-NATIVE/2.2/

Nodejs authorizing the connection to MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.