linuxmongodbarch

facing error while connecting mongodb with mongoose


Hi I am in arch based garuda linux and I am facing MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 error while connecting my node app to mongodb.

The error is

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/connection.js:797:32)
    at /home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:341:10
    at /home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:1167:10)
    at Mongoose.connect (/home/linux/Desktop/mongo tutorial/node_modules/mongoose/lib/index.js:340:20)
    at Object.<anonymous> (/home/linux/Desktop/mongo tutorial/index.js:2:10)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149: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: 19125836,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (/home/linux/Desktop/mongo tutorial/node_modules/mongodb/lib/cmap/connect.js:293:20)
            at Socket.<anonymous> (/home/linux/Desktop/mongo tutorial/node_modules/mongodb/lib/cmap/connect.js:267:22)
            at Object.onceWrapper (node:events:510:26)
            at Socket.emit (node:events:390:28)
            at emitErrorNT (node:internal/streams/destroy:164:8)
            at emitErrorCloseNT (node:internal/streams/destroy:129:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21)
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined   } }

Node.js v17.3.0

My mongodb compass screenschot enter image description here


Solution

  • The problem was not with the code it was the version. Instead of using

    mongoose.connect("mongodb://localhost/test")
    

    I used

    mongoose.connect("mongodb://127.0.0.1/test")
    

    it worked.

    reference: https://www.mongodb.com/community/forums/t/mongooseserverselectionerror-connect-econnrefused-127-0-0-1-27017/123421