检查 Atlas 中的 mongo db 是否可访问的最佳方法是什么?



我想在Azure中监视我所依赖的外部服务,以检查它们是否已启动并运行,我如何检查Atlas上的mongo实例是否正在工作?

我发现的一种方法是ping数据库碎片,如果它们是up,它们似乎回复200,这是最好的方法吗?我还注意到,我不能ping碎片共享实例,是真的吗?(尽管我不需要检查共享实例,但我只是想知道)

您可以使用这样的脚本:

const map = db.adminCommand("getShardMap").map;
for (let aShard in map) {
const uri = map[aShard].split('/');
try {
replicaSet = Mongo(`mongodb://username:password@${uri[1]}/admin?authSource=admin&replicaSet=${uri[0]}`).getDB('admin');
let hello = replicaSet.hello();
if (hello.isWritablePrimary) {
print(`${aShard} is operational`)
} else {
print(`${aShard} has problems`)
}
} catch (err) {
print(`${aShard} has problems: ${err.message}`)
}
}

根据您的需求,您还可以使用replicaSet.adminCommand({ replSetGetStatus: 1 })而不是replicaSet .hello()或计算db.hello()的其他属性

如果您想逐个检查主机,您也可以使用db.adminCommand("getShardMap").hosts

如果您只想检查整个数据库,请查看MongoDB的简单HTTP/TCP健康检查

相关内容

最新更新