简体   繁体   中英

MongoDB connect with credentials

I've been using MongoDB for a little while now. I've always managed to connect without using a username and password (on 2 servers I setup Mongo on).

I now want to change this so username and password has to be used. So far, I created a user in the admin database with the userAdminAnyDatabase role. I can see the user is in there.

Next I try to make my connection and test it:

mongoose.connect("mongodb://username:password@localhost:8080/MEAN");

var conn = mongoose.connection;             

conn.on("error", function(err){
  console.log(err);
});

I've not put in my real username and password there of course. With this, I get the following error: [Error: connection closed]

What's going wrong here?

If you want to connect with credentials, start mongo mongo instance using --auth

sudo mongod --dbpath=/your/db/path --auth

or

in /etc/mongodb.conf , set

auth=true

Then restart service

sudo service mongodb restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM