0

我在 ec2 上有一个死节点。如何使用 pymongo 从代表集中删除死的次要?

{u'date': datetime.datetime(2012, 11, 16, 11, 53, 53),
 u'members': [{u'_id': 0,
               u'health': 1.0,
               u'name': u'xxxxxxxxx:27017',
               u'optime': Timestamp(1352810702, 1),
               u'optimeDate': datetime.datetime(2012, 11, 13, 12, 45, 2),
               u'self': True,
               u'state': 1,
               u'stateStr': u'PRIMARY',
               u'uptime': 507560},
              {u'_id': 1,
               u'health': 1.0,
               u'lastHeartbeat': datetime.datetime(2012, 11, 16, 11, 53, 51),
               u'name': u'ffffffffff:27017',
               u'optime': Timestamp(1352810702, 1),
               u'optimeDate': datetime.datetime(2012, 11, 13, 12, 45, 2),
               u'pingMs': 1,
               u'state': 2,
               u'stateStr': u'SECONDARY',
               u'uptime': 163187},
              {u'_id': 2,
               u'errmsg': u'socket exception [CONNECT_ERROR] for jjjjjjjj:27017',
               u'health': 0.0,
               u'lastHeartbeat': datetime.datetime(2012, 11, 16, 11, 53, 14),
               u'name': u'jjjjjjjjjj:27017',
               u'optime': Timestamp(1352810702, 1),
               u'optimeDate': datetime.datetime(2012, 11, 13, 12, 45, 2),
               u'pingMs': 0,
               u'state': 8,
               u'stateStr': u'(not reachable/healthy)',
               u'uptime': 0}],
 u'myState': 1,
 u'ok': 1.0,
 u'set': u'heythat'}
4

1 回答 1

0

您可以从集合中获取副本集配置local.system.replset

>>> from pymongo import Connection
>>> c = Connection()
>>> conf = c['local']['system.replset'].find_one()

如果您在 mongo shell 中检查 rs.reconfig 包装器所做的事情,您会看到它还增加了版本,然后运行了一个特殊replSetReconfig命令。

default:PRIMARY> rs.reconfig
function (cfg, options) {
    cfg.version = rs.conf().version + 1;
    cmd = {replSetReconfig:cfg};
    for (var i in options) {
        cmd[i] = options[i];
    }
    return this._runCmd(cmd);
}

只需在 Python 中做同样的事情。修改conf['members']文档(例如删除死节点)并更新版本(添加 1)。然后replSetReconfig使用更新的conf字典作为参数对 admin 数据库运行命令。

>>> # change conf['members'] accordingly
>>> conf['version'] += 1
>>> c['admin'].command({'replSetReconfig':conf})
于 2012-11-19T03:32:06.760 回答