每次尝试连接到服务器以访问数据库中的文档时,它都会返回连接ETIMEDOUT错误。
我拥有运行Mongo 4.0的服务器,已经运行mongod并保持运行状态,集合中有数据。
let MongoClient = require('mongodb').MongoClient;
let uri = "mongodb://notmyrealusername:notmyrealpassword@thomas.misson.info/tom-portfolio";
MongoClient.connect(uri, { useNewUrlParser: true }, async function(err, db) {
try{
if (err) throw err;
let res = await db.collection("projects").find().count()
console.log(res);
db.close();
}
catch(err)
{
console.log(err);
}
});
错误消息:
{ MongoNetworkError: connect ETIMEDOUT 94.195.191.104:27017
at Socket.err (H:\Documents\Personal projects\node_modules\mongodb-core\lib\connection\connect.js:287:16)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
我已经使用netsh advfirewall firewall add rule name="Open mongod port 27017" dir=in action=allow protocol=TCP localport=27017
现在已经向mongod.cfg文件添加了新接口。
# network interfaces net:
port: 27017
bindIp: 127.0.0.1, 94.195.191.104