In this blog, I am going to explain how we can convert or migrate standalone mongoDB to replica sets also design standalone servers and replicaset. Basically, for Development and QA environment we can use standalone but for production, a replica set is the best option to set up.
Pros with Standalone mongo server :
- Single server maintenance.
- Easy to manage.
Problem with Standalone mongo server :
You don’t have high availability. Thus it’s not recommended for production deployment. This is fine for development though. A standalone is an instance of mongod that runs on a single server but is not part of a replica set.
MongoDB in production was designed with a replica set deployment in mind, for:
- High availability in the face of node failures
- Rolling maintenance/upgrades with no downtime
- Possibility to scale-out reads
- Possibility to have a replica of data in a special-purpose node that is not part of the high availability nodes
Before starting to migrate the database please have a backup of the database and server. We can convert Standalone to Replicase set in below 5 Steps.
- Shut down the standalone mongod instance.
sudo systemctl stop mongod
2. Start the instance. Use the — replSet option to specify the name of the new replica set.
mongod — port 27017 — dbpath /srv/mongodb/db0 — replSet rs0 — bind_ip localhost
3. Check the log file and confirm it is not stuck and waiting. Default Location “/var/log/mongodb/mongod.log”
tail -f /var/log/mongodb/mongod.log
4. Connect to mongosh to the mongod instance.
5. Initiate replicaset instance
rs.initiate()
6. The replica set is now operational. To view the replica set configuration, use rs.conf(). To check the status of the replica set
use rs.status()