mongod instance is useful for testing and
development. A standalone instance isn't a good choice for a production
deployment because it can be a single point of failure. A replica
set, also known as a cluster, provides redundancy and
availability. Always use a replica set in production.
If you have a standalone server with data that you want to use in production, convert the standalone server to a replica set first.
If you convert a development server to a replica set for production use, consult the security checklist before you expose your cluster to the internet.
A sharded cluster is a special kind of cluster. A sharded cluster provides redundancy and availability; it also distributes data across shards. Shards are usually hosted on multiple servers and allow for horizontal scaling.
If you configure your
mongod instance from the command line,
--replSet option to set a
name for your replica set.
A typical command line invocation might include:
Update the example code with the settings for your deployment.
mongod --replSet rs0 \ --port 27017 \ --dbpath /path/to/your/mongodb/dataDirectory \ --authenticationDatabase "admin" \ --username "adminUserName" \ --password
If you use a configuration file to start
mongodb, add a
replication section to your configuration file. Edit the
replSetName value to set the name of your replica set.
replication: replSetName: rs0
To initialize the replica set, use
mongosh to reconnect to
your server instance. Then, run
You only have to initiate the replica set once.
To view the replica set configuration, use
To check the status of the replica set, use
The new replica set has a single, primary node. The next step is to add new nodes to the replica set. Review the documentation on clusters before you add additional nodes:
When you are ready to add nodes, use
After you convert the mongod to a replica set, update the connection string used by your applications to the connection string for your replica set. Then, restart your applications.