12

I'm not talking about sharding. We had a testing server (linux) and there is already one mongo which belongs to another project/subteam. Is it possible to run multiple, isolated instances of mongodb on one machine? How can I do it?

2 Answers 2

15

Yes, you can do this by specifying different port numbers and data directories for the other instances of mongod, and then specifying the new port number in the client.

For example:

./mongod --dbpath /foo/bar/otherpath --port some_other_port

You can also change the shard server and config server port numbers if you need to.

4
  • 1
    Is it possible to specify this within mongodb.conf? Or would I have to create a script to do that? (on *nix)
    – Justin
    Aug 23, 2011 at 22:22
  • @Justin, you can certainly specify these options within a config file - see mongodb.org/display/DOCS/File+Based+Configuration. But then you still need to pass different config files to the different instances.
    – dsolimano
    Aug 24, 2011 at 1:10
  • Thanks @dsolimano, I've got two scripts in rc.d for two instances of Mongo. I've specified different paths for the db, pid, logging and given them different ports to listen on. It's also being forked, and when the second script runs it fails as mongo is already running (the first instance). So I guess my real question was (I I haven't had much luck finding this on the web) is can you have two forked instances running at the same time? Thanks
    – Justin
    Aug 24, 2011 at 14:25
  • @Justin, I think this probably rises to the level of a new question, perhaps you should ask it on this site? It sounds like there is something about the way you're starting Mongo that is causing a problem.
    – dsolimano
    Aug 24, 2011 at 21:09
3

The steps I've taken are:

  1. Copy conf file /etc/mongod.conf to mongod2.conf and also to mongod3.conf
  2. Edit conf files to have different ports and different database paths
  3. Copy init.d start script /etc/init.d/mongod to mongod2 and also to mongod3
  4. Copy binary mongod /usr/bin/mongod to /usr/bin/mongod2 and also to /usr/bin/mongod3
  5. Edit init.d start scripts and changed the following:

    CONFIGFILE="/etc/mongod2.conf" (mongod3.conf, respectively)

    ....

    mongod=${MONGOD-/usr/bin/mongod2} (mongod3, respectively)

Replaced /var/lock/subsys/mongod with /var/lock/subsys/mongod2 (mongod3, respectively) wherever I found it.

Resist the temptation to replace mongod.lock with mongod2.lock (or to mongod3.lock, respectively). They are in different folders (database folders are different) and won't conflict.

Now I can

service mongod start|stop|status

service mongod2 start|stop|status

service mongod3 start|stop|status

and also

mongo --port <port_number> 

for each mongo instance (remember the port settings from the conf files)

I am not aware of any side effect of renaming the mongod binary..

Hope this helps.

[Later edit] To start the instances automatically, just ln -s /etc/init.d/mongod2 /etc/rc.d/rc3.d/S86mongod and ln -s /etc/init.d/mongod3 /etc/rc.d/rc3.d/S87mongod :)

2
  • Ah Nice one! did exactly the same without much luck before because I didn't think we should also duplicate the mongod executable itself. All good now.
    – Bach
    Feb 11, 2013 at 8:13
  • In my case, using Ubuntu 16.01, there were no init.d scripts. Can you provide init.d script content?
    – Shubham A.
    May 16, 2017 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .