在运行“nodemon index.js”后,我收到此错误,说明我的应用程序无法连接到 mongo:
{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017]
at Pool.<anonymous> (/Users/Dakota/Projects/2018-Udemy-Bootcamp/node-mongo-project/todos_api/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/Dakota/Projects/2018-Udemy-Bootcamp/node-mongo-project/todos_api/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/Dakota/Projects/2018-Udemy-Bootcamp/node-mongo-project/todos_api/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
我的代码如下:
let mongoose = require('mongoose');
let dbUrl = 'mongodb://localhost/todo-api';
mongoose.set('debug', true);
mongoose.Promise = Promise;
mongoose.connect(dbUrl, { useNewUrlParser: true }).then(() => {
console.log("Database connection is active");
}).catch(err => {
console.log(err);
});
module.exports.Todo = require('./todo');
我已经在终端中启动了 mongod。
这个问题发生在我家里的网络上,但如果我去街上的咖啡店,它就会连接。我已经阅读了一些有关导致问题的防火墙和 IP 地址的内容,并已尽我所知尝试调整这两件事,但我似乎仍然无法解决这个问题。
关于如何解决这个问题有什么见解吗?
最佳答案
首先,检查您的mongodb服务是否正常运行。
您还可以将 localhost 替换为 127.0.0.1,如下所示:
// connect to database
mongoose.connect('mongodb://127.0.0.1/todo-api', { useNewUrlParser: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', function() {
console.log(`MongoDB connected!`);
});
编辑:我发现this形成 Mongoose 文档:
MongoDB replica sets rely on being able to reliably figure out the domain name for each member. On Linux and OSX, the MongoDB server uses the output of the
hostname
command to figure out the domain name to report to the replica set. This can cause confusing errors if you're connecting to a remote MongoDB replica set running on a machine that reports itshostname
aslocalhost
:
// Can get this error even if your connection string doesn't include
// `localhost` if `rs.conf()` reports that one replica set member has
// `localhost` as its host name.
failed to connect to server [localhost:27017] on first connect
If you're experiencing a similar error, connect to the replica set using the mongo shell and run the
rs.conf()
command to check the host names of each replica set member. Follow this page's instructions to change a replica set member's host name.
关于javascript - Mongodb连接服务器失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53756622/