Search code examples
javascriptnode.jsmongodbmongoosemongodb-compass

Can't connect to my MongoDB DataBase, getting this error "Server Selection Timed Out After 3000ms" on Mongodb Compass


This is the error that I is being shown on my terminal.

{
message: 'connect ECONNREFUSED 127.0.0.1:27017',
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

My Code for connecting to DataBase in this:-

const mongoose = require("mongoose");

mongoose
  .connect("mongodb://localhost:27017/playground", {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log("database connected");
  })
  .catch(err => {
    console.log("Could not connect", err);
  });

Everything was working fine until I shut down my laptop. I don't know why the Mongodb compass is showing me this error "Server selection timed out after 30000 ms", I am working on the localhost, only


Solution

  • You said "...everything was working until you shut down your computer...", it's most likely the mongod process exited on shut down. Can you try restarting the mongod process and connect again, details on how to start the process are here For Windows and For Mac.

    To avoid having issues like this again when you restart or shut down your system, consider starting the mongod process as a service, how you do that is dependant on the OS you are using, however, you can find complete set of instructions on the installation page. For example, if you are using a Mac, instructions are here.