[英]Hi, How do I run the equivalent using pymongo? cfg = rs.conf() db.printSlaveReplicationInfo()
1>如何使用pymongo运行等效项?
一>
cfg = rs.conf()
b>
db.printSlaveReplicationInfo()
2>使用PyMongo,如何在创建的集群中获取其他副本集CLI输出的详细信息。 (注意:我已经成功创建了集群。只需要在primary中编写一个python脚本来检查集群内所有副本集中rs.conf()
和db.printSlaveReplicationInfo()
的输出并解析输出。)
在这方面的任何帮助都是极大的。
副本集配置存储在名为“ rs.conf()
”的集合中的“本地”数据库中,因此当db为local
或其等效的find_one()
db.system.replset.findOne()
时, rs.conf()
的等效项为rs.conf()
。 find_one()
。
db.printSlaveReplicationInfo()涉及更多,但是您也可以在local
数据库中获取所有这些信息。
通过管理数据库命令replSetGetStatus
可以更轻松地获取它,该命令返回包含副本集每个成员的oplog信息以及其他详细信息的文档。 Python MongoDB驱动程序pymongo
提供了一种运行命令的方法 ,因此您可以针对admin
数据库运行该方法,并解析输出以获取有关副本集每个成员相对于主要副本的位置的信息。
我基本上会给您一个提示,而不是直接回答,因为完整的答案是简单地编写代码。 但是您可能不知道可以在shell中执行以下简单操作:
> db.printSlaveReplicationInfo
function () {
var startOptimeDate = null;
function getReplLag(st) {
assert( startOptimeDate , "how could this be null (getReplLag startOptimeDate)" );
print("\tsyncedTo: " + st.toString() );
var ago = (startOptimeDate-st)/1000;
var hrs = Math.round(ago/36)/100;
print("\t" + Math.round(ago) + " secs (" + hrs + " hrs) behind the primary ");
};
function getMaster(members) {
var found;
members.forEach(function(row) {
if (row.self) {
found = row;
return false;
}
});
if (found) {
return found;
}
};
function g(x) {
assert( x , "how could this be null (printSlaveReplicationInfo gx)" )
print("source: " + x.host);
if ( x.syncedTo ){
var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );
getReplLag(st);
}
else {
print( "\tdoing initial sync" );
}
};
function r(x) {
assert( x , "how could this be null (printSlaveReplicationInfo rx)" );
if ( x.state == 1 || x.state == 7 ) { // ignore primaries (1) and arbiters (7)
return;
}
print("source: " + x.name);
if ( x.optime ) {
getReplLag(x.optimeDate);
}
else {
print( "\tno replication info, yet. State: " + x.stateStr );
}
};
var L = this.getSiblingDB("local");
if (L.system.replset.count() != 0) {
var status = this.adminCommand({'replSetGetStatus' : 1});
startOptimeDate = getMaster(status.members).optimeDate;
status.members.forEach(r);
}
else if( L.sources.count() != 0 ) {
startOptimeDate = new Date();
L.sources.find().forEach(g);
}
else {
print("local.sources is empty; is this db a --slave?");
return;
}
}
我喜欢REPL的功能 ,就像python自己的著名REPL一样,您可以仅转储已实现函数的功能。
Simples。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.