0

I am using mongodb on ubuntu 14.04 and I ran a command to add a user to a database and set readWrite permissions to it by the following command -

mongo

and then use utthan-dev and then do

db.createUser(
    {
      user: "hladdha",
      pwd: "Hladdha@31",
      roles: [
         { role: "readWrite", db: "utthaan-dev" }
      ]
    }
)

Now, I am connecting in my express app by the following connection string -

mongoose.connect('mongodb://hladdha:Hladdha@Demo@localhost/utthaan-dev');
mongoose.connection.on('error', function(err) {
    console.error('MongoDB connection error: ' + err);
    process.exit(-1);
    }
);

And now I am getting the following error in node console -

MongoDB connection error: MongoError: getaddrinfo ENOTFOUND

How should I connect the mongoose to mongodb correctly no ?

Harshit Laddha
  • 2,044
  • 8
  • 34
  • 64

1 Answers1

0

Step 1. Open mongodb comandline Goto server location and write cmd 1.C:\Program Files\MongoDB\Server\5.0\bin 2. Mongo.exe

Step 2 Switch to user administrator

  1. Write "use admin" command in cmd

Step 3. Create User with below command 1.db.createUser({ user: "UserName", pwd: "Password", roles: [ { role: "readWrite", db: "DatabaseName" } ] }) 2.Then disconnect from the mongo shell (Ctrl+D)

Step 4. Add below line in mongod.cfg file below #security line security: authorization: "enabled"

Step 5. Restart MongoDbServer