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?

om-nom-nom
  • 225
  • 1
  • 2
  • 11

2 Answers2

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.

dsolimano
  • 1,320
  • 2
  • 14
  • 26
  • 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 '11 at 22:22
  • @Justin, you can certainly specify these options within a config file - see http://www.mongodb.org/display/DOCS/File+Based+Configuration. But then you still need to pass different config files to the different instances. – dsolimano Aug 24 '11 at 01: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 '11 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 '11 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 :)

om-nom-nom
  • 225
  • 1
  • 2
  • 11
aemilianvs
  • 31
  • 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 '13 at 08: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 '17 at 13:27