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
Issue the rs.initiate() command to initialize the replica set
try:
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
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.
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,?
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
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.
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