Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple instances of Mongo DB on same server

Tags:

mongodb

People also ask

Is database in MongoDB has multiple?

A database is a container for collections. Each database gets its own set of files on the host file system. A single MongoDB server typically has multiple databases.

How many databases can MongoDB handle?

By default, you can run some 12,000 collections in a single instance of MongoDB( that is, if each collection also has 1 index).

What is a MongoDB replica set?

In simple terms, MongoDB replication is the process of creating a copy of the same data set in more than one MongoDB server. This can be achieved by using a Replica Set. A replica set is a group of MongoDB instances that maintain the same data set and pertain to any mongod process.

What is a MongoDB instance?

MongoDB is a general-purpose, NoSQL database that provides support for JSON-styled, document-oriented storage systems. Its flexible data model enables you to store data of any structure, and it provides full index support, sharding, and replication.


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


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

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

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.