admin管理员组

文章数量:1200820

I've been trying for over 2 hours now trying to figure out what's wrong with this database. I've tried everything. From reinstalling the server, restarting the processes, rebooting and so much more. It keeps giving me this error when trying to connect:

const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
    at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
    at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
    at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
    at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
    at D:\TheShed\MX_\app.js:195:37
    at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
  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: 1421094,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
            at Socket.<anonymous> (D:\TheShed\MX_\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
  }
}

This error wont resolve no matter what I do. The MongoDB server is running, I've checked by doing >show dbs and it lists them perfectly fine. Also, C:/data/db exists and its fine too. What do I do? Here's my connection code:

(async () => {
    await require('./other/DB/mong')();
    console.log("Connected to Database.");
})();

and here's ./other/DB/mong:

var mongoose = require("mongoose");

module.exports = async () => {
    await mongoose.connect('mongodb://localhost:27017/MX', {
        keepAlive: true,
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    return mongoose;
}

I've been trying for over 2 hours now trying to figure out what's wrong with this database. I've tried everything. From reinstalling the server, restarting the processes, rebooting and so much more. It keeps giving me this error when trying to connect:

const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
    at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
    at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
    at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
    at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
    at D:\TheShed\MX_\app.js:195:37
    at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
  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: 1421094,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
            at Socket.<anonymous> (D:\TheShed\MX_\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
  }
}

This error wont resolve no matter what I do. The MongoDB server is running, I've checked by doing >show dbs and it lists them perfectly fine. Also, C:/data/db exists and its fine too. What do I do? Here's my connection code:

(async () => {
    await require('./other/DB/mong')();
    console.log("Connected to Database.");
})();

and here's ./other/DB/mong:

var mongoose = require("mongoose");

module.exports = async () => {
    await mongoose.connect('mongodb://localhost:27017/MX', {
        keepAlive: true,
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    return mongoose;
}
Share Improve this question asked Oct 29, 2021 at 3:01 MlemixMlemix 751 gold badge3 silver badges6 bronze badges 2
  • What happens when you replace localhost with 127.0.0.1 ? – Alex028502 Commented Oct 29, 2021 at 3:28
  • 1 Does this answer your question? Can't connect to MongoDB 6.0 Server locally using Nodejs driver – Wernfried Domscheit Commented May 24, 2023 at 13:26
Add a comment  | 

5 Answers 5

Reset to default 17

If you are using latest nodejs (v17.x) , update the mongodb url from localhost to 127.0.0.1.

MongoDB does not bind to localhost on ipv6 by default.

If you want it to listen on ::1 you will need to enable net.ipv6 and either enable net.bindIpAll or explicitly list the loopback address in net.bindIp

Updated my mongodb url from 'mongodb://localhost:27017/student' to 'mongodb://127.0.0.1:27017/student' and it worked fine for me

You need to add family:4 into the connect method.

const options = {

    useNewUrlParser: true,
    useUnifiedTopology: true,
    serverSelectionTimeoutMS: 5000,
    autoIndex: false, // Don't build indexes
    maxPoolSize: 10, // Maintain up to 10 socket connections
    serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
    socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
    family: 4 // Use IPv4, skip trying IPv6
}

本文标签: javascriptMongooseServerSelectionError connect ECONNREFUSED 127017 wont get fixedStack Overflow