【发布时间】:2021-09-12 15:09:17
【问题描述】:
我目前正在使用 nodeJS(无猫鼬)测试 MongoDB(4.4) 驱动程序并尝试连接到 localhost:27107。下面的代码几乎是从官方文档测试代码中复制/粘贴的。 MongoDB 很好地在后面运行。 但是,在我的命令行上,我收到如下所示的错误消息。谁能帮我解决这个问题?
错误信息:
MongoServerSelectionError: 连接 ECONNREFUSED 127.0.0.1:27107 在 Timeout._onTimeout (/Users/miya/Desktop/FruitsProject/node_modules/mongodb/lib/core/sdam/topology.js:438:30) 在 listOnTimeout (internal/timers.js:557:17) 在 processTimers (internal/timers.js:500:7) {原因:TopologyDescription { 类型:“单”, 设置名称:空, 最大设置版本:空, maxElectionId:空, 服务器:Map(1) { 'localhost:27107' => [ServerDescription] }, 陈旧:虚假, 兼容:真实, 兼容性错误:空, 逻辑会话超时分钟:空, heartbeatFrequencyMS: 10000, 本地阈值MS:15, commonWireVersion: null } }
我在 app.js 中的代码如下所示;
const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"mongodb://localhost:27107";
// Create a new MongoClient
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("Connected successfully to server");
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
const dbName = "fruitDB";
提前致谢!
【问题讨论】:
-
你的 mongoDB 服务器在运行吗?
-
@ApoorvaChikara 我猜我的社区服务器在后面运行..