MongoDB is a super flexible NoSQL database, and if you’re running it on Debian 12 (Bookworm), setting up Master-Slave (Primary-Secondary) replication can help with scaling and data redundancy.

This guide will take you through installing, running, and configuring MongoDB 8 for a master-slave setup.

Prerequisites

  • 3 Debian 12 servers:
    • Primary (Master): 10.11.33.111
    • Secondary 1 (Slave): 10.11.33.121
    • Secondary 2 (Slave): 10.11.33.122
  • Root or sudo access on all servers
  • Basic terminal knowledge (copy-paste helps too!)

Step 1: Install MongoDB 8 on Debian 12

1.1. Update Your System

Run this to make sure everything is fresh:

1.2. Add the MongoDB Repo

MongoDB isn’t included in the default Debian repo, so we gotta add it manually.

  1. Install gnupg and curl : sudo apt-get install gnupg curl
  2. Add MongoDB’s GPG key: curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo tee /usr/share/keyrings/mongodb-server-8.gpg
  3. Add the official repo: echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
  4. Update package lists: sudo apt update

1.3. Install MongoDB

1.4. Start and Enable MongoDB

1.5. Check If It’s Running

If you see Active (running), MongoDB is alive!

Step 2: Configure Master-Slave Setup

2.1. Setup the Master (Primary) Server

  1. Open the MongoDB config: sudo nano /etc/mongod.conf
  2. Edit these lines: net: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0"
  3. Restart MongoDB: sudo systemctl restart mongod

2.2. Setup the Slaves (Secondary Servers)

Do this on 10.11.33.121 and 10.11.33.122:

  1. Edit MongoDB config: sudo nano /etc/mongod.conf
  2. Same changes as above: net: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0"
  3. Restart MongoDB: sudo systemctl restart mongod

Step 3: Initialize the Replica Set

On the Primary (Master) Server 10.11.33.111, run:

Then, initiate the replica set:

Check if it’s working:

If it shows one Primary and two Secondaries, congrats, it’s working!

Step 4: Secure MongoDB with Authentication

By default, MongoDB is wide open! Let’s lock it down.

4.1. Create an Admin User

Run this on the Primary Server:

Then add an admin:

4.2. Enable Authentication

Edit MongoDB config on all servers:

Add:

Restart MongoDB:

4.3. Verify

Try logging in:

Step 5: Test the Replication

5.1. Insert Data on Primary

On the Primary Server (10.11.33.111):

5.2. Read Data from Secondary

On a Secondary Server (10.11.33.121):

Enable reading from the slave:

If you see { "name": "Laptop", "price": 1200 }, replication is working!

Step 6: Auto-Start MongoDB on Boot

Ensure MongoDB starts automatically:

Final Thoughts

Boom! 💥 You now have a MongoDB 8 Master-Slave (Primary-Secondary) setup running on Debian 12. It’s secured, replicated, and production-ready.

Next Steps

  • Backups: Use mongodump and mongorestore for safe data backups.
  • Monitoring: Run mongostat and mongotop to track performance.
  • Sharding: If you need even more scale, look into MongoDB sharding.

For more cool guides, check out diditho.com! 🚀

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.