How exactly to Install a MongoDB Sharded Cluster on CentOS 7 13


Sharding is a MongoDB procedure to keep data-set across various devices. Permits you to definitely do a scale that is horizontal of, partition data across independent instances, and it can be ‘Replica Sets’. Data-set partitioning on ‘Sharding’ uses key that is shard. Sharding lets you include more devices centered on information development on your own stack.

Sharding and Replication

Let’s ensure it is easy. When you’ve got collections of music, ‘Sharding’ will save you and keep your music collections into a folder that is different. ‘Replication,’ on the other hand, is just syncing your music collections to other instances.

Three Sharding Components

Shard – Used to store all data, and in a production environment, each shard is replica sets. Provides high-availability and data consistency.

Config Server – Used to store cluster metadata, contains a mapping of cluster data set and shards. This data is used by mongos/query server to deliver operations. It’s recommended to use more than 3 instances in production.

Mongos/Query Router – This is instances that are just mongo as application interfaces. The applying can certainly make demands to mongos circumstances, then mongos will deliver the demands making use of key that is shard the shards replica sets.


  • 2 centOS 7 server as Config Replica Sets
      •      configsvr1
      •      configsvr2
  • 4 CentOS 7 server as Shard Replica Sets
      •      shardsvr1
      •      shardsvr2
      •      shardsvr3
      •      shardsvr4
  • 1 CentOS 7 server as mongos/Query Router
      •       mongos
  • Root privileges
  • Each server connected to another server

Step 1 – Disable SELinux and Configure Hosts

For this tutorial, we will disable SELinux. Change SELinux configuration from ‘enforcing’ to ‘disabled’.

Connect to all nodes through OpenSSH.

ssh [email protected]

Disable SELinux by editing the configuration file.

vim /etc/sysconfig/selinux

Change SELinux value to ‘disabled’.


Save and exit.

Next, edit the hosts file on each server.

vim /etc/hosts

Paste the hosts that are following:      configsvr1      configsvr2      mongos      shardsvr1      shardsvr2      shardsvr3      Shardsvr4

( exit and*******)Save.

Now restart all servers:


Step 2 – Install MongoDB on All Instances

We will use latest MongoDB (MongoDB 3.4) for all instances. Add MongoDB that is new repository performing the next commands:

cat <<‘EOF’ >> /etc/yum.repos.d/mongodb.repo
name=MongoDB Repository

Now install mongodb 3.4 from mongodb repository utilising the yum demand below.

sudo yum -y install mongodb-org

After mongodb is set up, usage ‘mongo‘ or ‘mongod‘ demand into the way that is following check version details.

mongod –version

Install MongoDB on All Instances

Step 3 – Create Config Server Replica Set

In the prerequisites section, we’ve already defined config server with 2 machines ‘configsvr1’ and ‘configsvr2’. And in this step, we shall configure that it is a replica set.

If there’s service that is mongod on the server, stop it with the following systemctl command.

systemctl stop mongod

Edit the default mongodb configuration ‘mongod.conf‘.

vim /etc/mongod.conf

Change DB storage path to your own directory. We will use ‘/data/db1’ for the server that is first and ‘/data/db2’ directory for 2nd config host.

  dbPath: /data/db1

Change the worth of this line ‘bindIP’ towards network that is internal address. ‘configsvr1’ with IP address, and the second server with


In the replication section, set a replication name.

  replSetName: "replconfig01"

And under sharding section, define a role of the instances. We shall make use of those two circumstances as ‘configsvr’.

  clusterRole: configsvr

Save and exit.

Next, we ought to produce a directory that is new MongoDB data, and then change the ownership permissions of that directory to the ‘mongod’ user.

mkdir -p /data/db1
chown -R mongod:mongod /data/db1

Next, start the mongod service with the following command.

mongod –config /etc/mongod.conf

You can check mongod service is running on port 27017 with the command that is netstat

netstat -plntu

Create Config Server Replica Set

Configsvr1 and Configsvr2 are set the reproduction set. Connect with the ‘configsvr1’ access and server the mongo shell.

ssh [email protected]
mongo –host configsvr1 –port 27017

Initiate the replica set name with all configsvr members using the query below.

_id: “replconfig01”,
configsvr: true,
members: [
{ _id : 0, host : “configsvr1:27017” },
{ _id : 1, host : “configsvr2:27017” }

If you can get a results ‘{ “ok” : 1 }‘, it means the configsvr is already configured with the replica set.

Initiate the replica set name with all configsvr members

and you will be able to see which node is master and which node is secondary.


The configuration of Config Server Replica Set is done.

configuration of Config Server Replica Set

Step 4 – Create Shard Replica Sets

In this step, we will configure 4 centos 7 servers as ‘Shard’ server with 2 ‘Replica Set’.

  • 2 server – ‘shardsvr1‘ and ‘shardsvr2‘ with replica set name: ‘shardreplica01
  • 2 server – ‘shardsvr3‘ and ‘shardsvr4‘ with replica set name: ‘shardreplica02

Connect to each server and stop the mongod service (If the service is running), and edit the MongoDB configuration file.

systemctl stop mongod
vim /etc/mongod.conf

Change default storage to your specific directory.

  dbPath: /data/db1

In the ‘bindIP’ line, change the value to your internal network address.


In the replication section, you can use ‘shardreplica01‘ for the first and instances that are second. And make use of ‘shardreplica02‘ for 3rd and shard that is fourth.

  replSetName: "shardreplica01"

Next, define the role of the server. We shall make use of all of this as shardsvr circumstances.

  clusterRole: shardsvr

Save and exit.

Now produce a directory that is new MongoDB data.

mkdir -p /data/db1
chown -R mongod:mongod /data/db1

Start the mongod service.

mongod –config /etc/mongod.conf

Check whether MongoDB is running with the command below:

netstat -plntu

You will see MongoDB is running on the local network address.

Create Shard Replica Sets

Next, create a replica that is new of these 2 shard circumstances. Connect with ‘shardsvr1’ and access the mongo shell.

ssh [email protected]
mongo –host shardsvr1 –port 27017

Initiate the reproduction set using the title ‘shardreplica01‘, plus the people are ‘shardsvr1‘ and ‘shardsvr2‘.

    _id : "shardreplica01",
    members: [
      { _id : 0, host : "shardsvr1:27017" },
      { _id : 1, host : "shardsvr2:27017" }

If there is no error, you will see results as shown below.

Results from shardsvr3 and shardsvr4 with replica set name ‘shardreplica02‘.

Initiate the replica set

Redo this step on shardsvr3 and shardsvr4 servers with different replica set name ‘shardreplica02‘.

Now we’ve created 2 replica sets as the shard – ‘shardreplica01‘ and ‘shardreplica02‘.

Step 5 – Configure mongos/Query Router

The ‘Query Router’ or mongos is just instances that are running ‘mongos’. You can run mongos with the configuration file, or just run it with a command line.

Login on mongos host preventing the MongoDB solution.

ssh [email protected]
systemctl end mongod

Run mongos using the demand below.

mongos –configdb “replconfig01/configsvr1:27017,configsvr2:27017”

use the ‘–configdb’ substitute for determine the server that is config. If you are on production, use at least 3 servers that are config

You might find outcomes below.

Successfully attached to configsvr1*****************************************************************************************************************************************)( that is connected to configsvr2:27017

mongos instances are running.

Configure mongos/Query Router

Step 6 – Add shards to mongos/Query Router

Open another shell from step 5, connect to the mongos server again and access the mongo shell.

ssh [email protected]
mongo –host mongos –port 27017

Add shard server with sh mongodb query.

For ‘shardreplica01‘ instances.

sh.addShard( “shardreplica01/shardsvr1:27017”)
sh.addShard( “shardreplica01/shardsvr2:27017”)

For ‘shardreplica02‘ instances.

sh.addShard( “shardreplica02/shardsvr3:27017”)
sh.addShard( “shardreplica02/shardsvr4:27017”)

Make sure there is no error and check the status that is shard


You will sharding status as shown into the screenshot below.

Add shards to mongos/Query Router

We have actually 2 replica that is shard and 1 mongos instance running on our stack.

Step 7 – Testing

Now we’ll test the MongoDB server by enabling sharding and add documents. then

Access the mongos host mongo shell.

ssh [email protected]
mongo –host mongos –port 27017

Enable Sharding for a Database

Create a fresh database and enable sharding the brand new database.

use lemp

Enable sharding.

Now begin to see the status of this database – it was partitioned on reproduction set ‘shardreplica01’.

Enable Sharding for Collections

Next, include brand new collections on database with sharding help. We are going to include collection that is new ‘stack’ with shard collection ‘name’, and then see database and collections status.

sh.shardCollection(“lemp.stack”, {“name”:1})

Enable Sharding for Collections

New collections ‘stack’ with shard collection ‘name’ is added.

Add papers to collections ‘stack’.

Now insert the papers on collections. We must include the ‘shard key’.( when we add documents to the collection on sharded cluster,**********)

You can use an example below. We are using key that is shardname‘, once we included whenever allowing sharding for collections.{
“name”: “LEMP Stack”,
“apps”: [“Linux”, “Nginx”, “MySQL”, “PHP”],

Documents in successfully added to the collection, as shown in the screenshot that is following

Add documents to collections 'stack'.

(you can connect to the replica set ‘shardreplica01‘ PRIMARY server and open the mongo shell*******)If you want to test the database. I will be signing into the ‘shardsvr2’ MAIN host.

ssh [email protected]
mongo –host shardsvr2 –port 27017

Check database on the reproduction set.

show dbs
use lemp

You might find the database, collections, and papers can be purchased in the reproduction set.

You will see the database, collections, and documents are available in the replica set.

MongoDB sharded group on CentOS 7 effectively set up and implemented.

Burning information to an Object space provider with Restic

Previous article

Install Froxlor control interface Part-2

Next article

You may also like


Leave a Reply

More in centos