MongoServerSelectionError: connect ECONNREFUSED ::1:27017 - worked fine 3 weeks ago but I worked on another program between

I recently started working on a script unrelated to the app I’ve been building and now I can’t get my app to connect to the database again. It’s been about three weeks since I was in the app and the db connection worked beforehand. I’m not totally sure how long MongoDB stays connected so bringing this up in case it’s relevant.

I’ve tried a bunch of things from stack overflow posts and youtube but can’t get my connection to work.

Connection string

const dbUrl = 'mongodb://localhost:27017/macro-tickets';

If I run mongosh in my terminal, I get a connection (below) and noticed that the db route string is different from what I have above…

    Current Mongosh Log ID:	64b17475ab3453969945af15
    Connecting to:		mongodb://127.0.0.1:27017/? directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
    Using MongoDB:		6.0.1
    Using Mongosh:		1.6.0

Attempted fixes

After noticing the db route is different in the terminal, I changed dbUrl to mongodb://127.0.0.1:27017/macro-tickets, mongodb://0.0.0.0:27017/macro-tickets, mongodb://127.0.0.1/macro-tickets, and mongodb://127.0.0.1/macro-tickets but none work.

I also ran sudo mongod in VSCode’s terminal but that didn’t help.

I included my full error’s below (sorry I know it’s a lot of text) for context but any ideas why I can’t get this thing to work?

Errors

Error from the client

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
     at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
     at listOnTimeout (node:internal/timers:564:17)
     at process.processTimers (node:internal/timers:507:7)`

Error in my VSCode

    MongoServerSelectionError: connect ECONNREFUSED ::1:27017
        at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
        at listOnTimeout (node:internal/timers:564:17)
        at process.processTimers (node:internal/timers:507:7)
    MongoServerSelectionError: connect ECONNREFUSED ::1:27017
        at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
        at listOnTimeout (node:internal/timers:564:17)
        at process.processTimers (node:internal/timers:507:7)
    connection error: MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
        at Connection.openUri (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/connection.js:796:32)
        at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:328:10
        at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
        at new Promise (<anonymous>)
        at promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
        at Mongoose._promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:1149:10)
        at Mongoose.connect (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:327:20)
        at Object.<anonymous> (/Users/myname/Downloads/mtapp/app.js:38:10)
        at Module._compile (node:internal/modules/cjs/loader:1218:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) {
      reason: TopologyDescription {
        type: 'Unknown',
        servers: Map(1) { 'localhost:27017' => [ServerDescription] },
        stale: false,
        compatible: true,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        logicalSessionTimeoutMinutes: undefined
      }
    }
    node:internal/process/promises:289
                triggerUncaughtException(err, true /* fromPromise */);
                ^

    MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
        at Connection.openUri (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/connection.js:796:32)
        at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:328:10
        at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
        at new Promise (<anonymous>)
        at promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
        at Mongoose._promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:1149:10)
        at Mongoose.connect (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:327:20)
        at Object.<anonymous> (/Users/myname/Downloads/mtapp/app.js:38:10)
        at Module._compile (node:internal/modules/cjs/loader:1218:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) {
      reason: TopologyDescription {
        type: 'Unknown',
        servers: Map(1) {
          'localhost:27017' => ServerDescription {
            _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
            address: 'localhost:27017',
            type: 'Unknown',
            hosts: [],
            passives: [],
            arbiters: [],
            tags: {},
            minWireVersion: 0,
            maxWireVersion: 0,
            roundTripTime: -1,
            lastUpdateTime: 166365218,
            lastWriteDate: 0,
            error: MongoNetworkError: connect ECONNREFUSED ::1:27017
                at connectionFailureError (/Users/myname/Downloads/mtapp/node_modules/mongodb/lib/cmap/connect.js:293:20)
                at Socket.<anonymous> (/Users/myname/Downloads/mtapp/node_modules/mongodb/lib/cmap/connect.js:267:22)
                at Object.onceWrapper (node:events:628:26)
                at Socket.emit (node:events:513:28)
                at emitErrorNT (node:internal/streams/destroy:151:8)
                at emitErrorCloseNT (node:internal/streams/destroy:116:3)
                at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
              }
            },
            stale: false,
            compatible: true,
            heartbeatFrequencyMS: 10000,
            localThresholdMS: 15,
            logicalSessionTimeoutMinutes: undefined
          }
        }

        Node.js v19.3.0
        [nodemon] app crashed - waiting for file changes before starting...

I think the issue has to do with mongodb not running but it’s throwing me off that it shows as running from the terminal.

Here are my last 20 lines of logs:


tail -n 20 $(brew --prefix)/var/log/mongodb/mongo.log
{"t":{"$date":"2023-07-14T11:14:46.106-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn4","msg":"client metadata","attr":{"remote":"127.0.0.1:61881","client":"conn4","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T11:14:56.941-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:61885","uuid":"7c9e0af1-1eaf-49ff-bc48-a2f262e8adaf","connectionId":5,"connectionCount":5}}
{"t":{"$date":"2023-07-14T11:14:56.994-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn5","msg":"client metadata","attr":{"remote":"127.0.0.1:61885","client":"conn5","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T12:10:26.836-05:00"},"s":"W",  "c":"NETWORK",  "id":4615610, "ctx":"conn1","msg":"Failed to check socket connectivity","attr":{"error":{"code":6,"codeName":"HostUnreachable","errmsg":"Connection closed by peer"}}}
{"t":{"$date":"2023-07-14T12:10:26.837-05:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn1","msg":"Interrupted operation as its client disconnected","attr":{"opId":344305}}
{"t":{"$date":"2023-07-14T12:10:27.099-05:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn1","msg":"Connection ended","attr":{"remote":"127.0.0.1:61878","uuid":"99630538-48de-46ea-abd0-556112ea0df4","connectionId":1,"connectionCount":4}}
{"t":{"$date":"2023-07-14T12:10:35.214-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:62668","uuid":"5c7dec68-063a-46bd-ad9a-6425fd459654","connectionId":6,"connectionCount":5}}
{"t":{"$date":"2023-07-14T12:10:35.221-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn6","msg":"client metadata","attr":{"remote":"127.0.0.1:62668","client":"conn6","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T12:17:44.199-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:63007","uuid":"cf6d1cf6-ef70-4004-9ee8-8e111a396924","connectionId":7,"connectionCount":6}}
{"t":{"$date":"2023-07-14T12:17:44.199-05:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:61879","uuid":"70450628-bfb7-4ea9-8523-29c5623259d9","connectionId":2,"connectionCount":5}}
{"t":{"$date":"2023-07-14T12:17:44.200-05:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn4","msg":"Connection ended","attr":{"remote":"127.0.0.1:61881","uuid":"20cb14c6-1e92-4ba8-b54d-b7c5696dc976","connectionId":4,"connectionCount":4}}
{"t":{"$date":"2023-07-14T12:17:44.200-05:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn3","msg":"Connection ended","attr":{"remote":"127.0.0.1:61880","uuid":"cdad86ba-69b8-426d-8776-6ad883172a4b","connectionId":3,"connectionCount":3}}
{"t":{"$date":"2023-07-14T12:17:44.203-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn7","msg":"client metadata","attr":{"remote":"127.0.0.1:63007","client":"conn7","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T12:17:50.949-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:63008","uuid":"efccaf7a-f563-4367-8e39-69397f8036cf","connectionId":8,"connectionCount":4}}
{"t":{"$date":"2023-07-14T12:17:50.949-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:63009","uuid":"168c560e-c671-45e9-a257-e5aff01f76ac","connectionId":9,"connectionCount":5}}
{"t":{"$date":"2023-07-14T12:17:50.953-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn8","msg":"client metadata","attr":{"remote":"127.0.0.1:63008","client":"conn8","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T12:17:50.955-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn9","msg":"client metadata","attr":{"remote":"127.0.0.1:63009","client":"conn9","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T12:17:50.958-05:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:63010","uuid":"f4f52d36-2ec4-4b7f-a37e-34649cdcf653","connectionId":10,"connectionCount":6}}
{"t":{"$date":"2023-07-14T12:17:50.960-05:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn10","msg":"client metadata","attr":{"remote":"127.0.0.1:63010","client":"conn10","doc":{"driver":{"name":"nodejs|mongosh","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.6.0"},"platform":"Node.js v16.18.0, LE (unified)","version":"4.10.0|1.6.0","application":{"name":"mongosh 1.6.0"}}}}
{"t":{"$date":"2023-07-14T13:17:06.880-05:00"},"s":"I",  "c":"INDEX",    "id":5479200, "ctx":"TTLMonitor","msg":"Deleted expired documents using index","attr":{"namespace":"macro-tickets.sessions","index":"expires_1","numDeleted":0,"durationMillis":162}}

This answer from stack plus a computer restart worked for me.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.