Friday , June 21 2024

Restore a Sharded Cluster with Database Dumps in MongoDB


Before restoring database dumps to new servers we need to install MongoDB on each system.

Step 1: Installation:


inside it:


Configure SELinux

If you are using SELinux, you must configure SELinux to allow MongoDB to start on Red Hat Linux-based systems (Red Hat Enterprise Linux or CentOS Linux).

To configure SELinux, administrators have three options:

-If SELinux is in enforcing mode, enable access to the relevant ports that the MongoDB deployment will use (e.g. 27017). See Default MongoDB Port for more information on MongoDB’s default ports.

For default settings, this can be accomplished by running


-Disable SELinux by setting the SELINUX setting to disabled in /etc/selinux/config.

You must reboot the system for the changes to take effect.


-Set SELinux to permissive mode in /etc/selinux/config by setting the SELINUX setting to permissive.

You must reboot the system for the changes to take effect.

You can instead use setenforce to change to permissive mode. setenforce does not require a reboot but is not persistent.

Alternatively, you can choose not to install the SELinux packages when you are installing your Linux operating system, or choose to remove the relevant packages. This option is the most invasive and is not recommended.


Start MongoDB


Stop MongoDB


Restart MongoDB


Begin using MongoDB


After installation is completed the second step is deploying the replica set:

Step 2: Deploy a new replica set for each shard.

Replica set deployment

1- Create /mongodb directory on each shard


2- change owner of the directory to mongo user


3- create shard and config directories under the /mongodb directory.

Note: In this test environment I use 3 servers for 3 shard. And in each server I put the relicas of other shards.

In busy production environments all the replicas can be located in different servers.


4- Under /mongodb directory start the config servers in each server:


5- Under /mongodb server create the shard servers in each server:

Note: If you use one server for two or more shard, config or mongos, you need to give different ports for each process.
In my test case in one server I create 3 different shards, one config and one mongos so I gave different ports for each of them.


6- Initiate the replica sets

Log on to each shard using appropriate port and run the rs.initiate command:


7- Start the config servers in different servers:


Step 3: Start the mongoses in different servers:


Step 4: Adding shards:

connect to mongos:


Step 5: Shut down the mongos instances.


Step 6: Restore the shard data:

Restore each shard dump to the appropriate primary replica of each shard:


Step 7: Restore the config server data.


Step 8: Start mongos instances in each server.


Step 9: If shard hostnames have changed, update the config database.

connect to the primary config:


Step 10 : Restart all the shard mongod instances.

Step 11: Restart the other mongos instances.

Step 12: Verify that the cluster is operational.


About Selcen Sahin

Leave a Reply

Your email address will not be published. Required fields are marked *