mongodb连接报错
来源:13-13 nodejs连接mongodb
神话2005
2023-04-24
const { MongoClient } = require(‘mongodb’)
const url = 'mongodb://localhost:27017’
const client = new MongoClient(url)
const dbName = ‘myblog’
async function main() {
// Use connect method to connect to the server
await client.connect();
console.log(‘Connected successfully to server’);
const db = client.db(dbName);
const collection = db.collection(‘documents’);
// the following code examples can be pasted here…
return ‘done.’;
}
main()
.then((res) => {
console.log(res,‘res------successfully’)
})
.catch((err) => {
console.error(err, ‘console.error--------------------’)
})
.finally(() => client.close());
连接报错
报错信息
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (E:\myCode\nodejs-myblog\code-hu\mongodb-test\node_modules\mongodb\lib\sdam\topology.js:277:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: ‘Unknown’,
servers: Map(1) { ‘localhost:27017’ => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
} console.error--------------------
1回答
-
双越
2023-04-24
你使用命令行连接呢?能成功吗
032023-04-25
相似问题