Nodejs Connect to MongoDB database via account password

Source: Internet
Author: User
Tags auth

Transfer from 77435804#commentbox
1. Create an administrator
First open the MONGO service, then switch the admin database, at first there is no this database.

use admin
    • 1

2. Then create the user and password:

db.createUser({user:’root’,pwd:’root1234’,roles:[‘root’]})
    • 1

3. After you create the administrator account and password, and then run the MONGO service, add the –auth parameter:

mongod --dbpath "F:mongodb\data\db" --logpath "F:\mongodb\data\log\MongoDB.log" --auth
    • 1

4. If you log in directly to the database, you want to view the data, you can not see, you need to use the following command to log on successfully, if printed out a ' 1 ', indicating the success of the login:

> db.auth(‘root‘,‘root1234‘)1
    • 1
    • 2

To this step, MongoDB set the account password has been completed, and then I need to use Nodejs to connect the database, I use the Mongoose this library, he provided the schema, model and document object, it is very convenient to use. But when I follow the official website example to write the discovery is not successful, as follows:

var mongoose=require("mongoose")mongoose.connect(‘mongodb://root:[email protected]:27017/test‘);
    • 1
    • 2
    • 3

has always been an error, saying that verification failed

After looking at a lot of information is not found, but only to study the official API, in the official API to see:

mongoose.connect(‘mongodb://username:[email protected]:port/database?options...‘);
    • 1

This is the official website Connection example, there is an options parameter, I doubt whether there is a need to write some parameters, but in the actual example did not see the following to write any parameters, and then find the official website on the options of the description, as follows:

See the first parameter Authsource, his description is: If The database authentication are dependent on another databaseName. What he means is that if the database authentication needs to depend on another database name. I think, our database account password is not stored in the admin database, and we now need to connect to another database, you can try to add this parameter to see, is authsource=admin. Sure enough, plus this parameter, you can successfully connect to the database, it seems that the official website documents, encounter problems must first look at the official documentation, many problems may be solved.
The above is the Nodejs connection set up the account password of the MongoDB database of some pits, on the internet did not see the solution, I hope you see some enlightenment.

Nodejs Connect to MongoDB database via account password

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.