Convert a Self-Managed Replica Set to a Sharded Cluster
Sharded clusters partition data across multiple servers based on a shard key. A sharded cluster scales better than a replica set for deployments with large data sets and high throughput operations.
This tutorial converts a single three-member replica set to a sharded cluster with two shards. Each shard in the new cluster is an independent three-member replica set.
You can convert to a sharded cluster in the UI for deployments hosted in MongoDB Atlas.
About This Task
Some steps in this tutorial cause downtime for your deployment. Individual steps note when downtime will occur.
This tutorial is for deployments that have authentication enabled.
In this tutorial, you specify server settings with configuration files. Configuration files contain settings that are equivalent to the
mongodandmongoscommand-line options.The sharded cluster you deploy with this tutorial contains ten servers:
One server for the
mongos.Three servers each for the two shards in the cluster (six servers in total).
Three servers for the config server replica set.
Server Architecture
This tutorial uses the following servers:
Hostname | Port | Description |
|---|---|---|
|
| Member of the initial data-bearing shard, |
|
| Member of the initial data-bearing shard, |
|
| Member of the initial data-bearing shard, |
|
| Member of the second data-bearing shard, |
|
| Member of the second data-bearing shard, |
|
| Member of the second data-bearing shard, |
|
| The |
|
| Member of the config server replica set. |
|
| Member of the config server replica set. |
|
| Member of the config server replica set. |
The hostnames used in this tutorial are examples. Replace the hostnames used in the example commands with the hostnames used in your deployments.
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address fail startup validation and do not start.
Before You Begin
To complete this tutorial, you must have a replica set that uses either keyfile or X.509 certificate authentication. To deploy a secure replica set that uses one of these authentication methods, see either:
This tutorial uses the default data directories
/data/dband/data/configdb. To use different paths, set thestorage.dbPathsetting in your configuration file.
Steps
Deploy the Config Server Replica Set
Deploy a three-member replica set for the config servers. In this example, the config servers use the following hosts:
mongodb7.example.netmongodb8.example.netmongodb9.example.net
Configure the Config Servers
Configure a
mongodinstance on each config server host. Specify these options in the configuration file for eachmongodinstance:OptionValueconfigReplSetconfigsvrlocalhost, followed by any other hostnames on which themongodshould listen for client connections.replication: replSetName: configReplSet sharding: clusterRole: configsvr net: bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
Start the Config Servers
Deploy the
mongodwith your specified configuration:mongod --config <PATH_TO_CONFIG_FILE> The config servers use the default data directory
/data/configdband the default port27019.Connect to one of the config servers.
Use
mongoshto connect to one of the config servers. For example:mongosh "mongodb://mongodb7.example.net:27019" Initiate the config server replica set.
To initiate the replica set, run
rs.initiate():rs.initiate( { _id: "configReplSet", configsvr: true, members: [ { _id: 0, host: "mongodb7.example.net:27019" }, { _id: 1, host: "mongodb8.example.net:27019" }, { _id: 2, host: "mongodb9.example.net:27019" } ] } ) The preceding command uses the localhost exception to perform administrative actions without authentication.
Important
Run
rs.initiate()on just one and only onemongodinstance for the replica set.
Restore Existing Users and Roles to the New Config
Restore the existing users and roles you got when you ran mongodump.
mongorestore ./adminDump --nsInclude "admin.*" --host <configPrimaryURI>
The preceding command uses the localhost exception to perform administrative actions without authentication.
The output from running this command may be similar to:
0 document(s) restored successfully
This message does not indicate a problem. This output means that 0 documents other than the users and roles were restored.
Secure Config Server Replica Set
Reconfigure and restart the config server replica set.
Reconfigure the Config Servers
Select the tab for your authentication mechanism:
Restart a
mongodinstance on each of these hosts:mongodb7.example.netmongodb8.example.netmongodb9.example.net
Specify these options in the configuration file for each
mongodinstance:OptionValueThe path to the key file used for your initial replica set.
security: keyFile: <PATH_TO_KEYFILE> replication: replSetName: configReplSet sharding: clusterRole: configsvr net: bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
Restart a
mongodinstance on each of these hosts:mongodb7.example.netmongodb8.example.netmongodb9.example.net
Specify these options in the configuration file for each
mongodinstance in addition to the options you already configured:OptionValuex509requireTLSThe absolute path to the
.pemfile that contains both the TLS certificate and key.The absolute path to the
.pemfile that contains the root certificate chain from the Certificate Authority.localhost, followed by any other hostnames on which themongodshould listen for client connections.WARNING: Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist for Self-Managed Deployments. At minimum, consider enabling authentication and hardening network infrastructure.
sharding: clusterRole: configsvr replication: replSetName: configReplSet security: clusterAuthMode: x509 net: tls: mode: requireTLS certificateKeyFile: <FILE_WITH_COMBINED_CERT_AND_KEY> CAFile: <CA_FILE> bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment, such as
net.tls.certificateKeyFilePasswordif your TLS certificate-key file is encrypted with a password.Restart MongoDB
Restart the
mongodwith your specified configuration:mongod --config <PATH_TO_CONFIG_FILE> --shutdown mongod --config <PATH_TO_CONFIG_FILE>
Deploy the mongos
The mongos provides the interface between the client
applications and the sharded cluster.
Create a configuration file for the mongos.
Specify these options in your
mongosconfiguration file:OptionValueconfigReplSet, followed by a slash/and at least one of the config server hostnames and ports.The path to the key file used for your initial replica set.
localhost, followed by any other hostnames on which themongosshould listen for client connections.sharding: configDB: configReplSet/mongodb7.example.net:27019,mongodb8.example.net:27019,mongodb9.example.net:27019 security: keyFile: <PATH_TO_KEYFILE> net: bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
Specify these options in your
mongosconfiguration file:OptionValueconfigReplSet, followed by a slash/and at least one of the config server hostnames and ports.x509requireTLSThe absolute path to the
.pemfile that contains both the TLS certificate and key.The absolute path to the
.pemfile that contains the root certificate chain from the Certificate Authority.localhost, followed by any other hostnames on which themongosshould listen for client connections.sharding: configDB: configReplSet/mongodb7.example.net:27019,mongodb8.example.net:27019,mongodb9.example.net:27019 security: clusterAuthMode: x509 net: tls: mode: requireTLS certificateKeyFile: <FILE_WITH_COMBINED_CERT_AND_KEY> CAFile: <CA_FILE> bindIp: localhost,<ipaddress> Include any additional options appropriate for your deployment.
Deploy the mongos.
Deploy the
mongoswith your specified configuration:mongos --config <PATH_TO_CONFIG_FILE>
Restart the Initial Replica Set as a Shard
In this example, your initial replica set is a three-member replica set. This step updates the initial replica set so that it can be added as a shard to your sharded cluster.
The replica set runs on these hosts:
mongodb0.example.net:27017mongodb1.example.net:27017mongodb2.example.net:27017
For sharded clusters, you must set the role for each
mongod instance in the shard to shardsvr. To specify
the server role, set the sharding.clusterRole setting in the
mongod configuration file.
Note
Connect to a member of your initial replica set.
Use
mongoshto connect to one of the members of your initial replica set.mongosh "mongodb://<username>@mongodb0.example.net:27017" If your deployment uses X.509 authentication, specify these
mongoshoptions:For example:
mongosh "mongodb://<username>@mongodb0.example.net:27017" --tls --tlsCAFile <CA_FILE> --tlsCertificateKeyFile <filename> Determine the replica set's primary and secondaries.
Run
rs.status()to determine the primary and secondaries:rs.status() In the command output, the
replSetGetStatus.members[n].stateStrfield indicates which member is the primary and which members are secondaries.Restart the secondaries with the
--shardsvroption.Warning
This step requires some downtime for applications connected to the replica set secondaries.
Connect to a secondary.
Use
mongoshto connect to one of the secondaries.mongosh "mongodb://<username>@<host>:<port>" Shut down the secondary.
Run the following commands:
use admin db.shutdownServer() Edit the secondary's configuration file.
In the secondary's configuration file, set
sharding.clusterRoletoshardsvr:security: keyFile: <PATH_TO_KEYFILE> replication: replSetName: rs0 sharding: clusterRole: shardsvr net: port: 27017 bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
Restart the secondary as a shard server.
Run the following command on the host containing the secondary:
mongod --config <PATH_TO_CONFIG_FILE> Repeat the shut down and restart steps for the other secondary.
Connect to a secondary.
Use
mongoshto connect to one of the secondaries.If your deployment uses X.509 authentication, specify these
mongoshoptions:mongosh "mongodb://<username>@<host>:<port>" --tls --tlsCAFile <CA_FILE> --tlsCertificateKeyFile <filename> Shut down the secondary.
Run the following commands:
use admin db.shutdownServer() Edit the secondary's configuration file.
In the secondary's configuration file, set
sharding.clusterRoletoshardsvr:replication: replSetName: rs0 sharding: clusterRole: shardsvr security: clusterAuthMode: x509 net: port: 27017 tls: mode: requireTLS certificateKeyFile: <FILE_WITH_COMBINED_CERT_AND_KEY> CAFile: <CA_FILE> bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment, such as
net.tls.certificateKeyFilePasswordif your TLS certificate-key file is encrypted with a password.Restart the secondary as a shard server.
Run the following command on the host containing the secondary:
mongod --config <PATH_TO_CONFIG_FILE> Repeat the shut down and restart steps for the other secondary.
Restart the primary with the --shardsvr option.
Warning
This step requires some downtime for applications connected to the primary of the replica set.
Connect to the primary.
Use
mongoshto connect to the primary:mongosh "mongodb://<username>@<host>:<port>" Step down the primary.
Run the following command:
rs.stepDown() Verify that the step down is complete.
Run
rs.status()to confirm that the member you are connected to has stepped down and is now a secondary:rs.status() Shut down the former primary.
Run the following commands:
use admin db.shutdownServer() Wait for the shutdown to complete.
Edit the primary's configuration file.
In the primary's configuration file, set
sharding.clusterRoletoshardsvr:security: keyFile: <PATH_TO_KEYFILE> replication: replSetName: rs0 sharding: clusterRole: shardsvr net: port: 27017 bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
Restart the primary as a shard server.
Run the following command on the host containing the primary:
mongod --config <PATH_TO_CONFIG_FILE>
Connect to the primary.
Use
mongoshto connect to one of the secondaries.If your deployment uses X.509 authentication, specify these
mongoshoptions:If your deployment uses X.509 authentication, specify these
mongoshoptions:mongosh "mongodb://<username>@<host>:<port>" --tls --tlsCAFile <CA_FILE> --tlsCertificateKeyFile <filename> Step down the primary.
Run the following command:
rs.stepDown() Verify that the step down is complete.
Run
rs.status()to confirm that the member you are connected to has stepped down and is now a secondary:rs.status() Shut down the former primary.
Run the following commands:
use admin db.shutdownServer() Wait for the shutdown to complete.
Edit the primary's configuration file.
In the primary's configuration file, set
sharding.clusterRoletoshardsvr:replication: replSetName: rs0 sharding: clusterRole: shardsvr security: clusterAuthMode: x509 net: port: 27017 tls: mode: requireTLS certificateKeyFile: <FILE_WITH_COMBINED_CERT_AND_KEY> CAFile: <CA_FILE> bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment, such as
net.tls.certificateKeyFilePasswordif your TLS certificate-key file is encrypted with a password.Restart the primary as a shard server.
Run the following command on the host containing the primary:
mongod --config <PATH_TO_CONFIG_FILE>
Add the Initial Replica Set as a Shard
After you convert the initial replica set (rs0) to a shard, add it
to the sharded cluster.
Connect to the
mongosas your cluster's administrative user.The
mongosinstance is running on hostmongodb6.example.net.To connect
mongoshto themongos, run the following command:mongosh "mongodb://admin01@mongodb6.example.net:27017" This command authenticates you as the
admin01user you created on the sharded cluster. After you enter the command, enter your user's password.Add the shard.
To add a shard to the cluster, run the
sh.addShard()method:sh.addShard( "rs0/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017" )
Update Your Application Connection String
After you add the first shard to your cluster, update the connection string used by your applications to the connection string for your sharded cluster. Then, restart your applications.
Warning
Once the new shard is active, mongosh and other clients must
always connect to the mongos instance. Do not connect
directly to the mongod instances. If your clients connect to
shards directly, you may create data or metadata inconsistencies.
Deploy a Second Replica Set
Deploy a new replica set called rs1. The members of replica set
rs1 are on the following hosts:
mongodb3.example.netmongodb4.example.netmongodb5.example.net
Start each member of the replica set.
For each
mongodinstance in the replica set, create a configuration file with these options:OptionValueThe path to the key file used for your initial replica set.
rs1shardsvrlocalhost, followed by any other hostnames on which themongodshould listen for client connections.security: keyFile: <PATH_TO_KEYFILE> replication: replSetName: rs1 sharding: clusterRole: shardsvr net: bindIp: localhost,<ipaddress> Include additional options as appropriate for your deployment.
For each member, start a
mongodwith the following options:OptionValuers1shardsvrx509requireTLSThe absolute path to the
.pemfile that contains both the TLS certificate and key.The absolute path to the
.pemfile that contains the root certificate chain from the Certificate Authority.localhost, followed by any other hostnames on which themongodshould listen for client connections.replication: replSetName: rs1 sharding: clusterRole: shardsvr security: clusterAuthMode: x509 net: tls: mode: requireTLS certificateKeyFile: <FILE_WITH_COMBINED_CERT_AND_KEY> CAFile: <CA_FILE> bindIp: localhost,<ipaddress> Deploy the
mongodwith your specified configuration:mongod --config <PATH_TO_CONFIG_FILE> Note
When you specify the
--shardsvroption for amongodinstance, the instance runs on port27018by default.Start each member of the replica set.
Connect to a replica set member.
Use
mongoshto connect to one of the replica set members. For example:mongosh "mongodb://mongodb3.example.net:27018" mongosh "mongodb://mongodb3.example.net:27018" --tls --tlsCAFile <CA_FILE> --tlsCertificateKeyFile <filename> Initiate the replica set.
In
mongosh, run thers.initiate()method to initiate a replica set that contains the current member:rs.initiate( { _id : "rs1", members: [ { _id: 0, host: "mongodb3.example.net:27018" }, { _id: 1, host: "mongodb4.example.net:27018" }, { _id: 2, host: "mongodb5.example.net:27018" } ] } ) The preceding command requires the localhost exception to perform administrative actions without authentication.
Important
Run
rs.initiate()on just one and only onemongodinstance for the replica set.Add an administrative user for the replica set.
After you deploy the replica set, use the localhost exception to create the replica set's first user.
Determine the replica set primary.
To determine the primary, run
rs.status():rs.status() In the command output, the
replSetGetStatus.members[n].stateStrfield indicates which member is the primary.Connect to the replica set primary.
Connect to the replica set primary with
mongosh. For example, if the primary ismongodb4.example.net, run this command:mongosh "mongodb://mongodb4.example.net:27018" Create an administrative user.
Run the following
db.createUser()method to create a user namedrs1Adminwith theuserAdminrole:use admin db.createUser( { user: "rs1Admin", pwd: passwordPrompt(), roles: [ { role: "userAdmin", db: "admin" } ] } ) After you run the command, the database prompts you to enter a password for the
rs1Adminuser.
Add the Second Replica Set to the Cluster as a Shard
Add the new replica set, rs1, to the sharded cluster.
Connect
mongoshto themongos.Run the following command from the command line to connect to the
mongosinstance running on hostmongodb6.example.net:mongosh "mongodb://admin01@mongodb6.example.net:27017/admin" mongosh "mongodb://admin01@mongodb6.example.net:27017/admin" --tls --tlsCAFile <CA_FILE> --tlsCertificateKeyFile <filename> This command authenticates you as the
admin01user you created on the sharded cluster. After you enter the command, enter your user's password.Add the second shard.
After you connect to the
mongos, add the replica setrs1as a shard to the cluster with thesh.addShard()method:sh.addShard( "rs1/mongodb3.example.net:27018,mongodb4.example.net:27018,mongodb5.example.net:27018" )
Shard a Collection
The final step of the procedure is to shard a collection in the sharded cluster.
Determine the shard key.
Determine the shard key for the collection. The shard key indicates how MongoDB distributes the documents between shards. Good shard keys:
Have values that are evenly distributed among all documents.
Group documents that are often accessed at the same time into contiguous chunks.
Allow for effective distribution of activity among shards.
For more information, see Choose a Shard Key.
This procedure uses the
numberfield as the shard key for thetest_collectioncollection.Create an index on the shard key.
Before you shard a non-empty collection, create an index on the shard key:
use test db.test_collection.createIndex( { "number" : 1 } ) Shard the collection.
In the
testdatabase, shard thetest_collection. Specifynumberas the shard key.sh.shardCollection( "test.test_collection", { "number" : 1 } ) The next time that the balancer runs, it redistributes chunks of documents between shards. As clients insert additional documents into this collection, the
mongosroutes the documents to the appropriate shard.When the balancer redistributes chunks, it may negatively impact your application's performance. To minimize performance impact, you can specify when the balancer runs so it does not run during peak hours. To learn more, see Schedule the Balancing Window.
Learn More
For more sharding tutorials and procedures, see these pages: