MongoDB master-slave replication provides data redundancy and read scalability for production environments. This guide walks through a streamlined three-step process to deploy a master-slave setup on Windows using batch scripts.
Step 1: Prepare the Binaries
Download the MongoDB installation package and extract it. Navigate to the bin folder and copy its contents to your desired installation directory. You can also copy the bin folder directly from an existing MongoDB installation on your machine or another server.
Step 2: Create Directory Structure
Create a data directory at the same level as your bin folder. Inside this directory, set up two subdirectories: db for database storage and log for server logs. Your final structure should look like this:
MongoDB/
├── bin/
├── data/
│ ├── db/
│ └── log/
Step 3: Configure and Launch Services
Save the appropriate batch script below based on whether you are setting up a primary or secondary instance. Double-click the script to execute.
Primary Node Script:
@echo off
cd C:\MongoDB\Server\bin
mongod --repair --dbpath "C:\MongoDB\Server\data\db"
mongod --install --serviceName "MongoPrimary" --serviceDisplayName "MongoDB Primary" --dbpath "C:\MongoDB\Server\data\db" --logpath "C:\MongoDB\Server\data\log\mongo.log" --master
net start MongoPrimary
@pause
Secondary Node Script:
@echo off
cd D:\MongoDB\Replica\bin
mongod --repair --dbpath "D:\MongoDB\Replica\data\db"
mongod --install --serviceName "MongoSecondary" --serviceDisplayName "MongoDB Secondary" --dbpath "D:\MongoDB\Replica\data\db" --logpath "D:\MongoDB\Replica\data\log\mongo.log" --port=28018 --slave --source=127.0.0.1:27017
net start MongoSecondary
@pause
Configuration Notes
Several important considerations when deploying this setup:
- The batch file location does not matter; the cd command handles directory navigation to your MongoDB installation path.
- Adjust installation paths, data directories, and log paths to match your specific environment.
- For local testing with multiple instances on the same machine, repeat steps 1 and 2 with different installation directories. Use the primary location at C:\Program Files\MongoDB and secondary at D:\Program Files\MongoDB.
- Always start the primary instance first before launching secondary instances. Both will register as Windows services and start automatically.
- To add additional secondary nodes, repeat the secondary configuration script. When running multiple instances on one machine, ensure each uses a unique port number. In distributed environments with separate machines, the default MongoDB port 27017 can be used.
Removing a Service
Use the following script to uninstall a MongoDB service when needed:
@echo off
echo Stopping service...
net stop "MongoSecondary"
echo Removing service...
sc delete "MongoSecondary"
echo Press any key to exit
pause
Single Instance Configuration
For environments requiring only a standalone instance, use this simplified configuration:
@echo off
cd E:\MongoDB\Standalone\bin
mongod --repair --dbpath "E:\MongoDB\Standalone\data\db"
mongod --install --serviceName "MongoStandalone" --serviceDisplayName "MongoDB Standalone" --dbpath "E:\MongoDB\Standalone\data\db" --logpath "E:\MongoDB\Standalone\data\log\mongo.log" --port=29019
net start MongoStandalone
@pause