Mongo, trying to run reconfig

28 views
Skip to first unread message

David Montgomery

unread,
Nov 17, 2012, 8:55:05 AM11/17/12
to mongod...@googlegroups.com
So..per the py mongo docs..I ran conf to get the current configuration the ran reconfig Trying to dup what I can do in the shell.  Did not work per the below.

c = Connection("mongo.test.com:27017",replicaSet='ffff')
rep_status = c.admin.command("replSetGetStatus")
pprint(rep_status)
cfg = c.conf
print cfg
c.reconfig(cfg)


Traceback (most recent call last):
  File "/home/ubuntu/workspace/htDevOps/ht_remote_services/mongo_test.py", line 14, in <module>
    c.reconfig(cfg)
  File "/usr/local/lib/python2.7/dist-packages/pymongo-2.3-py2.7-linux-x86_64.egg/pymongo/database.py", line 769, in __call__
    self.__name, self.__connection.__class__.__name__))
TypeError: 'Database' object is not callable. If you meant to call the 'reconfig' method on a 'Connection' object it is failing because no such method exists.

Bernie Hackett

unread,
Nov 17, 2012, 11:27:58 AM11/17/12
to mongod...@googlegroups.com
I think you're confusing PyMongo with the mongo javascript shell. See my response to your last question for how to get the replica set configuration using PyMongo. PyMongo's Connection class does not have a reconfig method.

To see how rs.reconfig works in the shell:

repl0: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);
}

So you want to run the command "replSetReconfig" using PyMongo's Database.command method.

Here's a full example:

First, get the replica set config:

>>> import pymongo
>>> c = pymongo.Connection()
>>> conf = c.local.system.replset.find_one()
>>> conf
{u'_id': u'repl0', u'version': 3, u'members': [{u'host': u'devbox:27017', u'_id': 0}, {u'host': u'devbox:27018', u'_id': 1}, {u'host': u'devbox:27019', u'_id': 2}]}

Next delete the member you no longer want:

>>> del conf['members'][2]
>>> conf
{u'_id': u'repl0', u'version': 3, u'members': [{u'host': u'devbox:27017', u'_id': 0}, {u'host': u'devbox:27018', u'_id': 1}]}

Bump the config version:

>>> conf['version'] += 1
>>> conf
{u'_id': u'repl0', u'version': 4, u'members': [{u'host': u'devbox:27017', u'_id': 0}, {u'host': u'devbox:27018', u'_id': 1}]}

Run replSetReconfig:

>>> c.admin.command({'replSetReconfig': conf})

There will be a traceback with AutoReconnect here since 'replSetReconfig' causes the primary to temporarily close connections...

Now check that everything worked as expected.

>>> c.local.system.replset.find_one()
{u'_id': u'repl0', u'version': 4, u'members': [{u'host': u'devbox:27017', u'_id': 0}, {u'host': u'devbox:27018', u'_id': 1}]}

This is how you run rs.status() in python:

>>> c.admin.command('replSetGetStatus')
{u'date': datetime.datetime(2012, 11, 17, 16, 20, 8), u'myState': 1, u'set': u'repl0', u'ok': 1.0, u'members': [{u'uptime': 37661, u'optime': Timestamp(1353169155, 1), u'name': u'devbox:27017', u'self': True, u'optimeDate': datetime.datetime(2012, 11, 17, 16, 19, 15), u'state': 1, u'health': 1.0, u'stateStr': u'PRIMARY', u'_id': 0}, {u'uptime': 53, u'optime': Timestamp(1353169155, 1), u'name': u'devbox:27018', u'pingMs': 0, u'optimeDate': datetime.datetime(2012, 11, 17, 16, 19, 15), u'state': 2, u'health': 1.0, u'lastHeartbeatMessage': u'syncing to: devbox:27017', u'stateStr': u'SECONDARY', u'lastHeartbeatRecv': datetime.datetime(1970, 1, 1, 0, 0), u'_id': 1, u'lastHeartbeat': datetime.datetime(2012, 11, 17, 16, 20, 7)}]}





--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Reply all
Reply to author
Forward
0 new messages