Create a replica set on windows with mongo db comunity edition 6.0

I have been trying to create a replica set for a work project, but I can’t find any help, only, all videos are from MongoDB 4.0. I have been using ChatGPT to create a replica set using pymongo he made this code:

`from pymongo import MongoClient
from pymongo.errors import OperationFailure

Define the replica set name and member hosts

replica_set_name = ‘myReplicaSet’
hosts = [‘localhost:27017’, ‘localhost:27018’, ‘localhost:27019’]

Connect to the primary member of the replica set

client = MongoClient(hosts[0])

Initialize the replica set configuration object

config = {
‘_id’: replica_set_name,
‘members’: [
{‘_id’: 0, ‘host’: hosts[0]},
{‘_id’: 1, ‘host’: hosts[1]},
{‘_id’: 2, ‘host’: hosts[2]}

Issue the rs.initiate() command to initialize the replica set

client.admin.command(‘replSetInitiate’, config)
print(f"Replica set ‘{replica_set_name}’ created successfully")
except OperationFailure as e:
print(f"Failed to create replica set: {e}")`

But once I run it in VS Code gives me this error:
pymongo.errors.OperationFailure: This node was not started with replication enabled., full error: {‘ok’: 0.0, ‘errmsg’: ‘This node was not started with replication enabled.’, ‘code’: 76, ‘codeName’: ‘NoReplicationEnabled’}

How did you start your 3 mongods?
You should add replsetname in the config file or mention the same if you started from command line
I am guessing you most likely connected to default mongod whic runs on port 27017 as service
This will not have any replset param set

Refer to mongodb documentation

The config file is the one here “C:\Program Files\MongoDB\Server\6.0\bin” if so, what parameter do I edit and how?
Currently, it’s like this.

Another question is it easier to create a replica set using MongoDB compass, I’m also having problems with that approach. If you know how to do it in compass, please tell me.

Hi @Henrique_Eira,
as mentioned from @Ramachandra_Tummala, you did not add the correct parameters to initialize the repl set.


this should help you!


I managed to follow a guide and created a replica set with 2 secondary nodes from this YouTube video: (4) 8.MongoDB DBA Tutorials: MongoDB Replication Setup on Windows - YouTube
But after I restarted my pc I can’t access any of the nodes, what do I need to do?

When you reboot your server all mongods will be terminated
Try to bring up your mongods

How do I do that?
In mongo Compass i put in the URI: mongodb://localhost:27017, localhost:27020, localhost:27021/?replicaSet=r2schools
and it gives me the error with TLS/SSL as default: getaddrinfo ENOTFOUND localhost
with TLS/SSL on gives me this error: read ECONNRESET.
I believe that I have to put “r2schools” in the URI because of how I created the replicas.

What you have put us the connect string to connect to your replica
First your replica should be up & running to connect to it
Please follow steps you used to start mongods
You have to stop default mongod which came up as service
Then start 3 mongods of your replica starting with primary then secondaries
Once all 3 are up try to connect with your connect string

After I restart my pc to star the 3 mongods I just have to go into cmd and type this?
mongod --dbpath “c:\data1\db” --logpath “c:\data1\log\mongod.log” --port 27020 --storageEngine=wiredTiger --journal --replSet r2schools
Or is it something else?

Yes from cmd line run it
Have you captured rs status() when your replica was working?
I think port 27017 was primary as per that video
What port numbers you have used,?

Port 27030 now is primary for some reason, what do I need to retrieve from rs.staus()?

So is your replica up?
rs.status shows status of your replica like which node is primary,node status etc

It is now I’m afraid if I restart my pc all the replicas will be gone. This is what shows after rs.status()

After I restart, I just run this line in cmd for the two replicas
for port 27020–> mongod --dbpath “c:\data1\db” --logpath “c:\data1\log\mongod.log” --port 27020 --storageEngine=wiredTiger --journal --replSet r2schools
for port 27030 -->mongod --dbpath “c:\data2\db” --logpath “c:\data2\log\mongod.log” --port 27030 --storageEngine=wiredTiger --journal --replSet r2schools

And why did port 27030 became primary?

Quick update I restarted my pc and ran two cmd windows with the lines: for port 27030 -->mongod --dbpath “c:\data2\db” --logpath “c:\data2\log\mongod.log” --port 27030 --storageEngine=wiredTiger --journal --replSet r2schools and for port 27020–> mongod --dbpath “c:\data1\db” --logpath “c:\data1\log\mongod.log” --port 27020 --storageEngine=wiredTiger --journal --replSet r2schools
Now when I do rs.status() the port 27017 shows like this
Since it’s working, I guess there is nothing to worry.
Replica sets are now working, thank you, Ramachandra_Tummala for your patience and time.

Please refer to mongodb documentation on replica
When one of the node in 3 node replica goes down an election takes places and new primary is elected
Refer to my earlier reply.All 3 nodes should be up for high availability
Make sure the default mongod on port 27017 is down(if it came up) and bringup mongod on port 27017 from cmd line similar to other 2 nodes

Hi @Henrique_Eira,
you’ re using the same dbpath & the same logpath for 3 different instance in the same host…
as suggested from @Ramachandra_Tummala read from docs how to create correctly a repl set in only one host for test purpose.


Since I’m running multiple instances on my local machine, there’s not a problem for them to be in different ports?

Your dbpath,logpath dirs look good.
They are all different like c:\data1,c:\data2 etc
As long as your mongods use their own dbpath & logpath and port_number you are fine

1 Like

You’ re right, I had read it wrong😂

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.