Abdullah Ch
Abdullah Ch

Reputation: 101

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

Upvotes: 3

Views: 31532

Answers (8)

Nipun Chathuranga
Nipun Chathuranga

Reputation: 71

I have faced the similar issue, my node version is 18.17.0 by simply changing the connection string to

from mongodb://localhost:27017/ to this mongodb://127.0.0.1:27017/

solve my issue

Upvotes: 3

Sathwik Reddy
Sathwik Reddy

Reputation: 11

By changing the localhost:27017 to 127.0.01:27017 works fine without any issues.

   const mongoose=require('mongoose')
   mongoose.connect('mongodb://localhost:27017/your-datbase-name',
   {
   useNewUrlParser: true,
   useUnifiedTopology: true
   }).then(()=>{
   console.log("connected")
   }).catch((e)=>{
   console.log('error occured')
   console.log(e)
   })

final code looks like

   const mongoose=require('mongoose')
   mongoose.connect('mongodb://127.0.0.1:27017/your-datbase- 
   name',
   {
   useNewUrlParser: true,
   useUnifiedTopology: true
   }).then(()=>{
   console.log("connected")
   }).catch((e)=>{
   console.log('error occured')
   console.log(e)
   })

Upvotes: 1

Srinivas k
Srinivas k

Reputation: 161

If you have recently updated your Node.js runtime environment to version v18.12.1, you might notice that the MongoDB URL that you were using to connect to your local MongoDB instance has changed from 'mongodb://localhost:27017' to 'mongodb://127.0.0.1:27017'. This change is because using "localhost" can sometimes lead to issues with certain network configurations. By using the IP address "127.0.0.1" instead of "localhost", you can avoid these issues and ensure that your application can connect to MongoDB reliably. Therefore, it's generally recommended to use IP addresses rather than hostnames when connecting to MongoDB to avoid issues with DNS resolution and network configuration.

Upvotes: 14

Shajmil VJ
Shajmil VJ

Reputation: 11

Try with 127.0.0.1:27017 rather than localhost:27017 . it really works

Upvotes: 1

Oluwaseun Adesina
Oluwaseun Adesina

Reputation: 43

I tried starting MongoDB from services, but it does not start so i run mongod from the terminal to start MongoDB and change the localhost section of my connection string to 127.0.0.1.

Upvotes: 3

Erisan Olasheni
Erisan Olasheni

Reputation: 2905

Extending @Maduekew's answers, on Windows, click on services to filter out services only, then search for MongoDB, if the status column says Stopped, right-click and then select Start.

enter image description here

Upvotes: 4

Chukwuemeka Maduekwe
Chukwuemeka Maduekwe

Reputation: 8526

I ran mongo and mongod in terminal it didn't work.

solution for windows but should be similar to mac

on windows open task manager || Ctrl + shift + ESC > select service tab > search for mongo db > right click > start service

Upvotes: 0

Tunmise Ogunniyi
Tunmise Ogunniyi

Reputation: 2573

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.

Upvotes: 7

Related Questions