Windows环境MongoDB副本集配置报错:未启用replSet选项求助
Hey there! Let's get your MongoDB replica set up and running smoothly. The error you're seeing (NoReplicationEnabled) happens because none of your MongoDB instances were started with the required replica set configuration. Here's how to fix this step by step:
1. Create Separate Configuration Files for Each Replica Node
Since you've already created C:/data/rs1, rs2, rs3, each node needs its own config file with unique paths, ports, and the critical replSetName setting.
Example Config for Node 1 (rs1, port 27017)
Save this as mongod-rs1.conf:
# mongod-rs1.conf systemLog: destination: file path: c:\data\rs1\mongod.log # Log file stored in rs1 folder storage: dbPath: c:\data\rs1 # Uses your pre-created rs1 directory net: port: 27017 replication: replSetName: "test" # Must match the _id in your replica set config!
Config for Node 2 (rs2, port 27018)
Save as mongod-rs2.conf:
# mongod-rs2.conf systemLog: destination: file path: c:\data\rs2\mongod.log storage: dbPath: c:\data\rs2 net: port: 27018 replication: replSetName: "test"
Config for Node 3 (rs3, port 27019)
Save as mongod-rs3.conf:
# mongod-rs3.conf systemLog: destination: file path: c:\data\rs3\mongod.log storage: dbPath: c:\data\rs3 net: port: 27019 replication: replSetName: "test"
2. Start All Three Replica Instances
Forget the net start Mongodb command for now—we need to launch each node separately with its own config. Open three separate Command Prompt windows and run these commands one per window:
- Window 1 (Node 1):
mongod --config "C:\path\to\mongod-rs1.conf" - Window 2 (Node 2):
mongod --config "C:\path\to\mongod-rs2.conf" - Window 3 (Node 3):
mongod --config "C:\path\to\mongod-rs3.conf"
Make sure to replace C:\path\to\ with the actual folder where you saved your config files.
Wait until each instance shows a line like [listener] waiting for connections on port XXXX—that means it's ready.
3. Re-Initialize the Replica Set
Now connect to your first node (port 27017) in a new Command Prompt window:
mongo --port 27017
Then run your replica set config and initiate it (just like you tried before, but now all nodes have replSetName set):
config = {_id: "test", members: [ {_id: 0, host: "127.0.0.1:27017"}, {_id: 1, host: "127.0.0.1:27018"}, {_id: 2, host: "127.0.0.1:27019"} ]} rs.initiate(config)
This should return { "ok" : 1 } if successful. You can check the status with:
rs.status()
You'll see all three nodes listed, with one as the primary and the others as secondaries.
Bonus: Set Up Windows Services for Auto-Start
If you want these replica nodes to start automatically when your PC boots, you can create Windows Services for each. For example, for Node 1:
sc create MongoDB-RS1 binPath= "\"C:\Program Files\MongoDB\Server\X.X\bin\mongod.exe\" --config \"C:\path\to\mongod-rs1.conf\"" DisplayName= "MongoDB Replica Set Node 1" start= "auto"
- Replace
X.Xwith your MongoDB version (like 6.0) - Update the config file path to match your setup
- Repeat this for nodes 2 and 3, changing the service name (MongoDB-RS2, MongoDB-RS3) and config path accordingly
内容的提问来源于stack exchange,提问作者Ankit

