Replica sets setup using Python

559 views
Skip to first unread message

Om

unread,
Jul 27, 2011, 7:53:34 PM7/27/11
to mongodb-user
Hi,

I know replica sets can be setup using commands like (from
http://api.mongodb.org/python/current/examples/replica_set.html):

mongod --replSet foo/morton.local:27018,morton.local:27019 --rest

However, is there any way to do this using PyMongo? The page explains
how to initialize the replica set etc however it's not clear whether
the initial setups can be done via Python scripts.

Thanks for your time and help.

Bernie Hackett

unread,
Jul 27, 2011, 9:22:30 PM7/27/11
to mongodb-user
You can use the command interface in pymongo to do this. Here's a
simple example with two hosts:

Start up two hosts on your local machine:
mongod --dbpath ~/data/replica/repl0/db0 --replSet repl0
mongod --dbpath ~/data/replica/repl0/db1 --replSet repl0 --port 27018

Then in the python shell:

>>> import pymongo
>>> c = pymongo.Connection()
>>> conf = {'_id': 'repl0', 'members': [{'_id': 0, 'host': 'localhost:27017'}, {'_id': 1, 'host': 'localhost:27018'}]}
>>> c.admin.command('replSetInitiate', conf)
{u'info': u'Config now saved locally. Should come online in about a
minute.', u'ok': 1.0}

That's all there is to it.

Note: this config is just a simple example. In a production deployment
you would want at least 3 active data nodes or 2 active data nodes and
an arbiter.

On Jul 27, 11:53 pm, Om <omkar.ti...@gmail.com> wrote:
> Hi,
>
>  I know replica sets can be setup using commands like (fromhttp://api.mongodb.org/python/current/examples/replica_set.html):

Om

unread,
Jul 27, 2011, 9:32:37 PM7/27/11
to mongodb-user
Bernie,

thanks. Where can I find comprehensive documentation on this topic?
The online description seems rather sparse. Also, is there a way to do
the mongod commands (mongod --dbpath ~/data/replica/repl0/db0 --
replSet repl0) through Python?

Bernie Hackett

unread,
Jul 27, 2011, 9:54:31 PM7/27/11
to mongodb-user
> Where can I find comprehensive documentation on this topic?

This is about it in terms of initializing a replica set using pymongo.
I'll add this example to the API docs.

There is extensive documentation on replica sets here:

http://www.mongodb.org/display/DOCS/Replica+Sets

> Also, is there a way to do the mongod commands (mongod --dbpath ~/data/replica/repl0/db0 --replSet repl0) through Python?

You want to read the subprocess docs for Python itself here:

http://docs.python.org/library/subprocess.html

Your code would probably look **something like** this:

from subprocess import PIPE, Popen
Popen(<your shell command to start mongod>, stdout=PIPE, stderr=PIPE,
shell=True)

Om

unread,
Jul 28, 2011, 1:12:13 AM7/28/11
to mongodb-user
Bernie,

thanks for the help. I'm trying to use Python to control all the
replica set life cycle. Are there any limitations I should be aware
of?

Bernie Hackett

unread,
Jul 28, 2011, 7:34:59 PM7/28/11
to mongodb-user
I can't think of anything specific. What do you mean in terms of life-
cycle?

Om

unread,
Jul 28, 2011, 9:18:37 PM7/28/11
to mongodb-user
I meant things like creating the rs, adding members, removing members
etc. I'm planning to do them all using Python scripts. My goal is to
control these entire functions through Python.

Bernie Hackett

unread,
Jul 28, 2011, 9:33:30 PM7/28/11
to mongodb-user
The python driver doesn't provide helper methods to do a lot of these
things but most of them can be accomplished using db.command.

For example in the mongo shell there is a command rs.stepDown which
tells the mongod you are currently connected to that it should no
longer act as the primary. You can see what it does by running
"rs.stepDown" with no parenthesis in the shell. That will print out
the javascript code for the function. In this case you see that all
the function does is run the command "replSetStepDown". To do that in
python you would connect to that specific mongod and run
connection.admin.command("replSetStepDown"). Other rs functions can be
handled in pymongo in a similar fashion.

Om

unread,
Jul 28, 2011, 9:40:45 PM7/28/11
to mongodb-user
Thanks a lot for the help Bernie :D
Reply all
Reply to author
Forward
0 new messages