Mongonetworkerror failed to connect to server localhost

I have installed mongodb version 4.2.3 but I anycodings_npm am unable to connect my cloned project. anycodings_npm showing below error

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection timed out]

But I am able to connect database with my anycodings_npm local project and also checked by performing anycodings_npm transactions. mongo commmand output shown anycodings_npm below

MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a6f6b036-ad62-4e38-b0d2-50bfda540016") }
MongoDB server version: 4.4.3
---
The server generated these startup warnings when booting: 
        2021-01-12T12:43:25.733+05:30: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-01-12T12:43:31.420+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 

My mongodb connection code is below

// Connect to mongodb
var connect = function () {
  var options = {
    keepAlive: 1,
    autoReconnect: true,
    useNewUrlParser: true,
    useUnifiedTopology:true,
    serverSelectionTimeoutMS:60000,
    socketTimeoutMS:60000
  };
  mongoose.connect(config.db, options);
};

connect();
mongoose.connection.on("error", console.log);
mongoose.connection.on("disconnected", connect);

I have also tried by increasing timeout but anycodings_npm no resolution.

32

Answers 1 : of MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connection timed out]

I was initially facing this issue maybe anycodings_npm due to the slow system. I struggled with anycodings_npm this issue full day long. But my anycodings_npm resolved when I restarted my system. So anycodings_npm finally restarting the system as well as anycodings_npm the mongod service resolved my issue.

0

2022-09-17T08:49:24+00:00 2022-09-17T08:49:24+00:00Answer Link

mRahman

description¶

Got this error when nmp start:

npm start

>  start
> node app.js

(node:2330) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:2330) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
/Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/topologies/server.js:438
          new MongoNetworkError(
          ^

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (/Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (node:events:378:20)
    at /Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/connection/pool.js:995:11
    at /Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/connection/connect.js:280:5)
    at Socket.<anonymous> (/Users/maling/Desktop/MGSpecs/binary-sever/node_modules/mongodb/lib/core/connection/connect.js:310:7)
    at Object.onceWrapper (node:events:485:26)
    at Socket.emit (node:events:378:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:81:21)
npm ERR! code 1
npm ERR! path /Users/maling/Desktop/MGSpecs/binary-sever
npm ERR! command failed
npm ERR! command sh -c node app.js

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/maling/.npm/_logs/2021-08-23T07_06_42_476Z-debug.log

solution¶

Start mongod and specify the database path

sudo mongod --dbpath=/usr/local/data/db