mongodb Mongo服务器选择错误:连接电子连接拒绝::1:27017

9gm1akwq  于 2022-11-03  发布在  Go
关注(0)|答案(3)|浏览(140)

我正在使用mongo 5.0.1和节点17.2.0这是我的代码如果我连接想要连接atlas与此代码它运行成功,但当我尝试连接与本地数据库它给出这个错误.

const { MongoClient } = require("mongodb");

async function main(){
    const uri = "mongodb://localhost:27017";

    const client = new MongoClient(uri);

    try{
        await client.connect();
        await listDatabases(client);
    } catch (e){
        console.error(e);
    } finally {
        await client.close();
    }
}
main().catch(console.error);

async function listDatabases(client) {
    databasesList = await client.db().admin().listDatabases();

    console.log("Databases:");
    databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};

这是我得到的错误。

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (D:\web development\nodeDemo\node_modules\mongodb\lib\sdam\topology.js:330:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}
i7uaboj4

i7uaboj41#

使用此作为URI

const uri = "mongodb://127.0.0.1:27017";
8oomwypt

8oomwypt2#

我通过在mongodb.config中启用IPv6解决了这个问题
更多信息请访问https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-net.ipv6

kkbh8khc

kkbh8khc3#

请使用此网址“mongodb://127.0.0.1:27017”
而不是“mongodb://本地主机:27017”

相关问题