Replica set is the resolution for the issue for sure
But doing replica of 3 nodes is not mandatory.
Solution 1 (for standalone setup)
For standalone mongo installation you can skip configuring 2nd or 3rd node as described on the official mongo documentations here
And you'll need to set a replSetName in the configuration
replication:
oplogSizeMB: <int>
replSetName: <string>
enableMajorityReadConcern: <boolean>
and then run details of which are here
rs.initiate()
after this the connection string would be like below:-
mongodb://localhost:27017/<database_name>?replicaSet=<replSet_Name>
keys above that you need to replace:-
database_name = name of the database
replSet_Name = name of the replica set you setup in the above configuration
Solution 2 (only for docker based requirement)
Example Docker image with single node replica set acting as primary node for development environment is as below:-
I had hosted the docker image on the docker hub
docker pull krnbr/mongo:latest
Contents of the same Dockerfile are below:-
FROM mongo
RUN echo "rs.initiate({'_id':'rs0','members':[{'_id':0,'host':'127.0.0.1:27017'}]});" > /docker-entrypoint-initdb.d/replica-init.js
RUN cat /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--bind_ip_all", "--replSet", "rs0" ]
Docker run command (replace with the image name that you build yoursef or use the on shared above i.e krnbr/mongo):-
without volume
docker run -d --name mongo -p 27017:27017 <Image Name> mongod --replSet rs0 --port 27017
with volume
docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --replSet rs0 --port 27017
for supporting binding of any ip
docker run -d --name mongo -p 27017:27017 -v ~/.mongodb:/data/db <Image Name> mongod --bind_ip_all --replSet rs0 --port 27017
db.version()
. Even if you were using a development branch server then the current "cut" is 3.7.5. So you're talking about the "driver" and I'm talking about the "server". – Ragouthttp://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.7.9.zip?_ga=2.134458040.816321646.1525581231-1505195349.1522993255
anddb.version()
inside the mongo shell returns3.7.9
– Cerargyrite