在Python中设置的副本中显示同步时间黑白主端口和辅助端口号

时间:2019-02-06 15:05:38

标签: python mongodb pymongo

rs.printSlaveReplicationInfo()

mongodb中的此命令提供了主副本集和辅助副本集之间的同步或延迟时间, 但是如何在python中做到这一点

1 个答案:

答案 0 :(得分:0)

from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo import MongoClient
client=MongoClient
("mongodb://localhost:27017,localhost:27018,localhost:27019/admin? 
replicaSet=xyz")
db = client['admin']
data = db.command({"replSetGetStatus": 1})
primary_optime = 0
secondary_optime = 0
for  key in data['members']:
    if key['stateStr'] == 'SECONDARY':
    #print(key['name'])
    secondary_optime = key['optimeDate']

    if key['stateStr'] == 'PRIMARY' :
     primary_optime = key['optimeDate']

print 'PrimaryOptime : '+str(primary_optime)
print 'SecondaryOptime : ' + str(secondary_optime)
seconds_lag = (primary_optime - secondary_optime ).total_seconds()
print 'secondary_lag : ' + str(seconds_lag)