我在AWS EC2 Linux机器上运行MongoDB服务器v6.0.4,它是公共可访问的。
- 使用MongoDB:6.0.4使用Mongosh:1.8.0版 *
/etc/监视配置文件
# network interfaces
net:
port: 27017
ipv6: true
bindIp: ::,0.0.0.0 # Tried with public DNS and IP as well here.
security:
authorization: enabled
keyFile: /etc/mongodkeyfile.key
#operationProfiling:
replication:
replSetName: rs0
能够通过mongosh在同一台计算机上连接来执行所有DB操作,
当我从Mac机器ping时,EC2机器响应。
而当我尝试通过Nodejs应用程序连接时,出现以下错误
MongoServerSelectionError: getaddrinfo ENOTFOUND ip-xx-x-xx-xxx.ap-south-1.compute.internal
at Timeout._onTimeout (/Users/durgalovababupadala/myprojects/continuum-electrolite/ev-server/node_modules/mongodb/lib/sdam/topology.js:293:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
这是我的示例代码,
const { MongoClient } = require("mongodb");
// Replace the uri string with your connection string.
const uri = "mongodb://<user>:<pwd>@<EC2-PublicIP>:27017/<dbname>?replicaSet=rs0&useUnifiedTopology=true";
const client = new MongoClient(uri);
async function run() {
try {
const database = client.db('test');
console.log(database);
const myColl = database.collection("pizzas");
const doc = { name: "Neapolitan pizza", shape: "round" };
const result = await myColl.insertOne(doc);
console.log(
`A document was inserted with the _id: ${result.insertedId}`,
);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
先谢谢你,
**更新:**在同一台EC2机器上使用MongoDB连接执行简单节点应用程序,并且运行正常。
1条答案
按热度按时间r6l8ljro1#
错误消息为
getaddrinfo ENOTFOUND ip-xx-x-xx-xxx.ap-south-1.compute.internal
。注意结尾处的 .internal。如果您不在EC2网络内部,则无法解决此问题。当连接到副本集时,连接字符串中的主机名和IP地址是种子列表。驱动程序将尝试依次连接到种子列表中的每个主机,一旦获得连接,将运行isMaster。
isMaster命令将返回在配置文档中输入的副本集中的成员hostname:port的列表。然后客户端断开原始连接,并连接到发现的节点。
在您的情况下,将发生以下情况:
要解决这个问题,请重建或重新配置副本集,确保每个成员中
host
字段的值集是一个在EC2网络内外都可以解析的主机名。