我正在使用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
}
}
3条答案
按热度按时间i7uaboj41#
使用此作为URI
8oomwypt2#
我通过在
mongodb.config
中启用IPv6解决了这个问题更多信息请访问https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-net.ipv6
kkbh8khc3#
请使用此网址“mongodb://127.0.0.1:27017”
而不是“mongodb://本地主机:27017”