![](/img/trans.png)
[英]REST API with Express, MongoDB and Mongoose in node.js in Ubuntu. MongoError: failed to connect to server [127.0.0.1:27017] on first connect
[英]Node.js connect to MongoDB throw an error :MongoError: failed to connect to server [127.0.0.1:27017] on first connect
我正在嘗試使用node.js與Windows系統上的mongodb連接。 但是,這使我出錯:
C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\lib\mongo_client.js:225
throw err
^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:260:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:162:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
這是我的代碼:
"use strict";
const MongoClient = require('mongodb').MongoClient;
const dbUrl = 'mongodb://127.0.0.1:27017/cs602';
MongoClient.connect(dbUrl, (err, db) => {
if (err)
{throw err}else {
console.log('Successfully connected to',
db.s.databaseName);
}
// Do database operations
db.close();
});
我是node.js和mongodb的新手,有人可以幫助我嗎? 提前非常感謝您!
此錯誤可能是由MongoDB的綁定IP設置引起的。 您可以檢查MongoDB的配置文件。 但是事實是我不知道它位於Windows系統中。
就我而言,綁定IP設置為本地主機地址,如下所示:
# /usr/local/etc/mongod.conf
net:
bindIp: 127.0.0.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.