CaribouCode
CaribouCode

Reputation: 14398

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?

Upvotes: 0

Views: 1240

Answers (1)

itzMEonTV
itzMEonTV

Reputation: 20339

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

Upvotes: 2

Related Questions