0

我正在尝试使用 python 脚本降级 MongoDB 主节点。我在输出中看到以下内容。有没有办法可以将退出代码标记为 OK。

代码:

 if 'primary' in isMaster:
        primary =(isMaster['primary']).split(':')[0]
        conn = pymongo.MongoClient('mongodb://'+ primary +':10000 replicaSet=test-ipe1')
        stepdown = conn.admin.command("replSetStepDown",100)
        if 'ismaster' in isMaster == 'true':
            print("I am still the primary")
        else:
            print("I am no longer the primary")
    else:
        primary = "No primary currently elected."

追溯:

Traceback (most recent call last):

File "./repldown.py", line 39, in <module>
    stepdown = conn.admin.command("replSetStepDown",60)

 File "/usr/local/lib64/python2.6/site-packages/pymongo/database.py", line 391, in command
    result = self["$cmd"].find_one(command, **extra_opts)

File "/usr/local/lib64/python2.6/site-packages/pymongo/collection.py", line 604, in find_one
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1):

File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 904, in next
    if len(self.__data) or self._refresh():

File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 848, in _refresh
    self.__uuid_subtype))

 File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 782, in __send_message
    res = client._send_message_with_response(message, **kwargs)

File "/usr/local/lib64/python2.6/site-packages/pymongo/mongo_client.py", line 1051, in _send_message_with_response

raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: connection closed
4

1 回答 1

2

是的:

from pymongo.errors import AutoReconnect

try:
    stepdown = conn.admin.command("replSetStepDown",100)
except AutoReconnect:
    pass

主节点在降级时故意立即关闭所有连接,因此该命令会引发异常。忽略它并继续。

于 2016-10-26T13:57:50.747 回答