Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Convert a Standalone mongod to a Replica Set

A standalone 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.

Important

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.

Before you convert your standalone instance, consider whether a replica set or a sharded cluster is more appropriate for your workload.

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.

1

Use mongosh to connect to your mongod instance.

mongosh

Switch to the admin database and run shutdown.

use admin
db.adminCommand(
{
shutdown: 1,
comment: "Convert to cluster"
}
)
2

If you configure your mongod instance from the command line, use the --replSet option to set a name for your replica set.

A typical command line invocation might include:

Purpose
Option
Cluster name
Network details
Data path
Authentication details

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

Note

Optional

You can specify the data directory, replica set name, and the IP binding in the mongod.conf configuration file, and start the mongod with the following command:

mongod --config /etc/mongod.conf
3

To initialize the replica set, use mongosh to reconnect to your server instance. Then, run rs.initiate().

rs.initiate()

You only have to initiate the replica set once.

To view the replica set configuration, use rs.conf().

To check the status of the replica set, use rs.status().

4

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 rs.add().

5

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.

←  Add an Arbiter to Replica SetAdd Members to a Replica Set →