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?

4 Answers 4

67

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. Check documentation for the corresponding configuration parameters, ex storage.dbPath, processManagement.pidFilePath, net.bindIp, etc

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

Sign up to request clarification or add additional context in comments.

4 Comments

--logpath and --pidfilepath are cli options
@BryanGrace There were lot's of changes in the documentation since 2015. However, the point is that you should use the corresponding parameters as per the documentation. Exact names can, of course, be different. I have updated the answer accordingly.
@kasur oh for sure, if you're trying to build a p2p network that uses mongo as the database though, and need to start like 1000+ databases in order to test the network, might be easier to use the cli though.
57

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

4 Comments

Please check @kasur's answer and edit your question with a warning
I understand your point but my problem is somewhat different in my server has 2.4 version Mongo and my system require more than 3 MongoDB version then how do I go?
But how do i know which mongo shell to start? any specific command to identify the process?
@AnkurSoni mongo --port 27018
39

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

4 Comments

You should really include pidfilepath too
Sorry. May bad. It's only needed for forking i.e the deamon wont create a pidFile otherwise.
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 , 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.
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.

6 Comments

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?
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.
Ok, understood. And is it the one install of Mongo or 2 seperate installs?
you only need one install of mongodb distribution. You will simply be starting mongod process twice.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.