DEV Community

Purvesh Panchal
Purvesh Panchal

Posted on

How to Run Two MongoDB Instances on the Same Server (Standalone and Replica Set)

Hey buddy! Tech can be tricky, but you’ve got this. Think of this as a tech chat over coffee...or my personal favorite chaay(tea). Let’s get to it!

If you're working with MongoDB, there might come a time when you need to run two MongoDB instances on the same server—one as a standalone instance and another as part of a replica set. This guide will walk you through setting up two MongoDB instances on different ports, using separate configurations, all on the same server. By the end of this tutorial, you'll have one standalone MongoDB instance and another configured as a replica set member.

Table of Contents

  • Introduction
  • Prerequisites
  • Step 1: Prepare Your Server for the Second MongoDB Instance
  • Step 2: Configure MongoDB to Run Two Instances on Different Ports
  • Step 3: Create Separate Configurations for the Second MongoDB Instance
  • Step 4: Start the Second MongoDB Instance
  • Step 5: Initialize the Replica Set
  • Step 6: Verify and Connect to Both MongoDB Instances
  • Conclusion

Introduction

Running multiple MongoDB instances on a single server can be useful for testing, staging environments, or setting up replica sets without additional hardware. But hey, it’s a little tricky! Don’t worry, though, we’ll go step-by-step and make sure everything runs smoothly. In this tutorial, you’ll learn how to:

  • Set up one MongoDB instance as standalone.
  • Set up a second MongoDB instance with a replica set.
  • Run both instances on different ports.
  • Manage data directories and log files for each instance.

Prerequisites

Before starting, just a couple of things to make sure:

  • You have root or sudo access to the server.
  • MongoDB is already installed and running as a standalone instance on your server.
  • You’ve got a basic understanding of MongoDB configuration and Linux commands.

Step 1: Prepare Your Server for the Second MongoDB Instance

Since you already have one MongoDB instance running, you need separate directories for the second MongoDB instance to store its data and logs.

Create a Separate Data Directory

sudo mkdir -p /var/lib/mongodb-replicaset
sudo chown -R mongodb:mongodb /var/lib/mongodb-replicaset
Enter fullscreen mode Exit fullscreen mode

Create a Separate Log Directory

sudo mkdir -p /var/log/mongodb-replicaset
sudo chown -R mongodb:mongodb /var/log/mongodb-replicaset
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure MongoDB to Run Two Instances on Different Ports

Since MongoDB instances can’t share the same port, you'll want your standalone MongoDB running on the default port (27017), and the second instance will run on a different port, like 27018.


Step 3: Create Separate Configurations for the Second MongoDB Instance

Now, you’ll need a new mongod.conf file for the second instance.

Copy the Default MongoDB Configuration File

sudo cp /etc/mongod.conf /etc/mongod-replicaset.conf
Enter fullscreen mode Exit fullscreen mode

Edit the Configuration for the Second Instance

Open the new config file /etc/mongod-replicaset.conf and tweak the following:

# mongod-replicaset.conf

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb-replicaset
  journal:
    enabled: true

# Where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb-replicaset/mongod.log

# Network interfaces.
net:
  port: 27018  # Different port for the second instance
  bindIp: 0.0.0.0  # Listen on all IP addresses

# Enable replica set
replication:
  replSetName: rs0  # Replica set name

# Enable security and authentication if required
security:
  authorization: enabled
Enter fullscreen mode Exit fullscreen mode

This configuration makes sure the second MongoDB instance runs on port 27018 and has its own directories for data and logs.


Step 4: Start the Second MongoDB Instance

Now it’s time to get the second MongoDB instance up and running. You’ll want to create a separate service for it if you're using systemd.

Copy the MongoDB Service File

sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod-replicaset.service
Enter fullscreen mode Exit fullscreen mode

Edit the New Service File

In /lib/systemd/system/mongod-replicaset.service, change the ExecStart line to point to your new configuration file:

[Service]
ExecStart=/usr/bin/mongod --config /etc/mongod-replicaset.conf
Enter fullscreen mode Exit fullscreen mode

Reload systemd and Start the Second MongoDB

sudo systemctl daemon-reload
sudo systemctl start mongod-replicaset
sudo systemctl enable mongod-replicaset
Enter fullscreen mode Exit fullscreen mode

Now, your second MongoDB instance should be up and running on port 27018.


Step 5: Initialize the Replica Set

Once the second MongoDB instance is running, it's time to initialize the replica set.

Connect to the Second MongoDB Instance

mongosh --port 27018
Enter fullscreen mode Exit fullscreen mode

Run the Replica Set Initialization Command

rs.initiate()
Enter fullscreen mode Exit fullscreen mode

You can check the replica set status with:

rs.status()
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify and Connect to Both MongoDB Instances

You now have two MongoDB instances running on the same server.

Standalone Instance (Port 27017)

To connect to the standalone instance, run:

mongosh --port 27017
Enter fullscreen mode Exit fullscreen mode

Replica Set Instance (Port 27018)

To connect to the replica set instance, run:

mongosh --port 27018
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following this guide, you've successfully set up two MongoDB instances on the same server—one as a standalone instance and the other as part of a replica set. You’ve also learned how to handle data and logs for each instance.

This approach is perfect for testing or running multiple MongoDB instances without extra hardware. You’ve got it—time to flex those MongoDB skills!


Frequently Asked Questions

1. Can I add more members to the replica set later?
Yes, you can add more members to your replica set by using the rs.add() command.

2. How can I secure the second MongoDB instance?
Make sure that both instances are secured with authentication, IP-based access control, and firewalls.

3. Can I run more than two MongoDB instances on the same server?
Yes, you can run additional MongoDB instances on the same server as long as they have separate ports, data directories, and configuration files.

4. How do I stop the second MongoDB instance?
You can stop it by running:

sudo systemctl stop mongod-replicaset
Enter fullscreen mode Exit fullscreen mode

That’s all, my friend! You’ve optimally used your server resources and got a great setup to manage different environments with MongoDB.


Top comments (0)