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
2. Then create the user and password:
db.createUser({user:’root’,pwd:’root1234’,roles:[‘root’]})
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
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
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‘);
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...‘);
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