0

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?

Community
  • 1
  • 1
CaribouCode
  • 13,998
  • 28
  • 102
  • 174

1 Answers1

1

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
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49