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
- Primary (Master):
- 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:
sudo apt update && sudo apt upgrade -y
1.2. Add the MongoDB Repo
MongoDB isn’t included in the default Debian repo, so we gotta add it manually.
Install gnupg and curl :
sudo apt-get install gnupg curl
Add MongoDB’s GPG key:
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo tee /usr/share/keyrings/mongodb-server-8.gpg
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
Update package lists:
sudo apt update
1.3. Install MongoDB
sudo apt install -y mongodb-org
1.4. Start and Enable MongoDB
sudo systemctl enable mongod
sudo systemctl start mongod
1.5. Check If It’s Running
sudo systemctl status mongod
If you see Active (running), MongoDB is alive!
Step 2: Configure Master-Slave Setup
2.1. Setup the Master (Primary) Server
- Open the MongoDB config:
sudo nano /etc/mongod.conf - Edit these lines:
net: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0" - 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:
- Edit MongoDB config:
sudo nano /etc/mongod.conf - Same changes as above:
net: bindIp: 0.0.0.0 port: 27017 replication: replSetName: "rs0" - Restart MongoDB:
sudo systemctl restart mongod
Step 3: Initialize the Replica Set
On the Primary (Master) Server 10.11.33.111, run:
mongosh
Then, initiate the replica set:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "10.11.33.111:27017" },
{ _id: 1, host: "10.11.33.121:27017" },
{ _id: 2, host: "10.11.33.122:27017" }
]
});
Check if it’s working:
rs.status()
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:
mongosh
Then add an admin:
use admin;
db.createUser({
user: "admin",
pwd: "SuperStrongPass123!",
roles: [{ role: "root", db: "admin" }]
});
4.2. Enable Authentication
Edit MongoDB config on all servers:
sudo nano /etc/mongod.conf
Add:
security:
authorization: enabled
Restart MongoDB:
sudo systemctl restart mongod
4.3. Verify
Try logging in:
mongosh -u admin -p SuperStrongPass123! --authenticationDatabase admin
Step 5: Test the Replication
5.1. Insert Data on Primary
On the Primary Server (10.11.33.111):
use testdb;
db.products.insertOne({ name: "Laptop", price: 1200 });
5.2. Read Data from Secondary
On a Secondary Server (10.11.33.121):
mongosh -u admin -p SuperStrongPass123! --authenticationDatabase admin
Enable reading from the slave:
rs.slaveOk();
use testdb;
db.products.find();
If you see { "name": "Laptop", "price": 1200 }, replication is working!
Step 6: Auto-Start MongoDB on Boot
Ensure MongoDB starts automatically:
sudo systemctl enable mongod
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
mongodumpandmongorestorefor safe data backups. - Monitoring: Run
mongostatandmongotopto track performance. - Sharding: If you need even more scale, look into MongoDB sharding.
For more cool guides, check out diditho.com! 🚀