61

I am working with Mongo DB and I am a newbie to it. I am about to install it on a server specifically for Mongo.

I would like to create 2 instances of it - 1 to support a QA environment, the other to support a Staging Environment.

I am more familiar with SQL Server where I can create multiple instances.

Is it possible to do the same with Mongo DB and if so, how?

amateur
  • 43,371
  • 65
  • 192
  • 320

4 Answers4

68

The aforementioned answer is not a recommended way to run multiple instances (especially when the servers might be running at the same time) as it will lead to usage of the same config parameters like for example logpath and pidfilepath which in most cases is not what you want.

Please, consider creating dedicated mongod configuration files like mongod-QA.conf and mongod-STAGE.conf. In these files you may want to provide dbpath, logpath folders, bind_ip, port and pidfilepath specific to each mongod instance and that will not affect each other.

After these steps you are good to trigger two instances as follows

mongod --config <path-to>/mongod-QA.conf
mongod --config <path-to>/mongod-STAGE.conf

You can find more details on mongodb docs page

Community
  • 1
  • 1
kasur
  • 1,542
  • 15
  • 15
  • 4
    Mongo docs about [Run Multiple Database Instances](http://docs.mongodb.org/manual/administration/configuration/#run-multiple-database-instances-on-the-same-system) – Jon May 11 '15 at 01:19
55

You just need to create another folder(ex: mongodb2) dbpath for the second instance, and run it in a different port(ex: 27018)

 mongod --dbpath /usr/local/var/mongodb2 --port 27018
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Ilake Chang
  • 1,542
  • 1
  • 14
  • 19
40

Here is how I start 4 mongod's on the same pc to emulate production environment in development environment.

To start mongod you should use separate config for each mongod. Take 4 configs and start mongods using them:

start C:\mongodb\bin\mongod.exe --config C:\net2\dev1-pc\configs\mongod-primary1.cfg 
start C:\mongodb\bin\mongod.exe --config C:\net2\dev1-pc\configs\mongod-secondary1.cfg --rest
start C:\mongodb\bin\mongod.exe --config C:\net2\dev1-pc\configs\mongod-secondary2.cfg
start C:\mongodb\bin\mongod.exe --config C:\net2\dev1-pc\configs\mongod-secondary3.cfg

Configs look like this:

mongod-primary1.cfg file contents

systemLog:
    destination: file
    path: c:\net2\primary1-pc\data\log\mongod.log
storage:
    dbPath: c:\net2\primary1-pc\data\db
net:
    port: 27018
replication:
    replSetName: repl1

mongod-secondary1.cfg file contents

systemLog:
    destination: file
    path: c:\net2\secondary1-pc\data\log\mongod.log
storage:
    dbPath: c:\net2\secondary1-pc\data\db
net:
    port: 27019
replication:
    replSetName: repl1

mongod-secondary2.cfg file contents

systemLog:
    destination: file
    path: c:\net2\secondary2-pc\data\log\mongod.log
storage:
    dbPath: c:\net2\secondary2-pc\data\db
net:
    port: 27020
replication:
    replSetName: repl1

mongod-secondary3.cfg file contents

systemLog:
    destination: file
    path: c:\net2\secondary3-pc\data\log\mongod.log
storage:
    dbPath: c:\net2\secondary3-pc\data\db
net:
    port: 27021
replication:
    replSetName: repl1
  • You should really include [pidfilepath](https://docs.mongodb.com/manual/reference/configuration-options/#processManagement.pidFilePath) too – Karl Pokus May 19 '17 at 20:27
  • Sorry. May bad. It's only needed for forking i.e the deamon wont create a pidFile otherwise. – Karl Pokus May 22 '17 at 11:00
  • I have followed these steps, when i try to save some values in 27018 instance its throwing write error like ` > db.product.save ( { a: 1 } ) WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })` – Johnykutty Jul 06 '17 at 07:19
  • 1
    @Johnykutty , above configurations are of replication servers and replication servers supposed to be for read only. you can do write operations in a master server/main server.Only master writes on repl servers. Remove the replication part in the config and that server will run as another separate mongodb master instances. – diveinsky Jun 06 '18 at 05:49
11

It's possible - you would give each one its own port to listen on, and its own --dbpath directory to put its files in, but I wouldn't recommend this because they will both be competing for the same resources - RAM, i/o bandwidth, etc.

If you have multiple disks on this server you can place their data files on separate devices but you're still risking your QA instance reducing availability of the production instances, possibly at the worst possible time.

I would put QA instance on a random machine that's doing something unimportant before I would colocate it with my production instance.

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • Asya, thank you for this feedback - it is most helpful. The environments that share the one server are both testing environments, so I am not concerned regarding their competing for resources. However, saying that, I would like the best setup. I can split the disk in to multiple partitions. How can this be setup with mongo, connection strings etc to it? – amateur Mar 11 '13 at 20:02
  • you would just put each mongo's dbpath directory on a different partition. Then since each has a different port you connect to hostname:port - so you use different ports depending on which you are connecting to. – Asya Kamsky Mar 11 '13 at 22:39
  • Ok, understood. And is it the one install of Mongo or 2 seperate installs? – amateur Mar 11 '13 at 22:52
  • 3
    you only need one install of mongodb distribution. You will simply be starting mongod process twice. – Asya Kamsky Mar 11 '13 at 23:23
  • Wouldn't multiple partitions on the same disk be between useless and harmful for performance, though? I think the most important factor here is available RAM so that caches don't suffocate. – Camilo Martin Aug 11 '14 at 08:17
  • while RAM is important, disk IOPs (or lack of availability of such) tends to be one of the most common limiting factors (even insufficient RAM manifests as insufficient IOPs due to excessive page faulting). So it depends on what the limiting factor is - increasing anything other than what is the current bottleneck will be useless (until the actual bottleneck is addressed anyway) – Asya Kamsky Aug 11 '14 at 14:10