Databases 8 min read

Master MongoDB Sharding: Step-by-Step Setup and Verification

This guide walks through the reasons for sharding in MongoDB, explains the sharding architecture, and provides detailed commands to configure config servers, shard servers, and mongos, add shards, enable sharding on databases and collections, insert test data, and verify successful data distribution across shards.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master MongoDB Sharding: Step-by-Step Setup and Verification

Data Sharding

Why Sharding? Sharding is MongoDB's method for splitting large collections across multiple servers (a cluster) when a single server's CPU, memory, or I/O cannot meet demand, thereby reducing server pressure.

Sharding Architecture

Experimental Topology

Configuration Process

Clean Existing Data

[root@node1 ~]# service mongod stop
Stopping mongod:                               [  OK  ]
[root@node1 ~]# rm -rf /mongodb/data/*

Execute the above on each node; skip if this is the first run.

Config Server Setup

Install required packages:

[root@node2 mongodb]# ls
mongodb-org-server-2.6.10-1.x86_64.rpm  mongodb-org-tools-2.6.10-1.x86_64.rpm
mongodb-org-shell-2.6.10-1.x86_64.rpm
[root@node2 mongodb]# yum install *.rpm -y

Modify Configuration File [root@node2 ~]# vim /etc//mongod.conf Set the following options:

logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongodb/data
configsvr=true   # enable config server
pidfilepath=/var/run/mongodb/mongod.pid
#bind_ip=127.0.0.1
httpinterface=true
rest=true

Create Data Directory

[root@node2 ~]# mkdir /mongodb/data -pv
mkdir: created directory `/mongodb'
mkdir: created directory `/mongodb/data'
[root@node2 ~]# chown -R mongod.mongod /mongodb

Start Service

[root@node2 ~]# service mongod start
Starting mongod:                               [  OK  ]
[root@node2 ~]# ss -tnl | grep 27019
LISTEN      0      128               *:27019                *

Note: the listening port has changed from the default 27017.

Mongos Configuration

Install only the mongos package:

[root@node1 ~]# yum install mongodb-org-mongos-2.6.10-1.x86_64.rpm -y

Start mongos:

[root@node1 ~]# mongos --configdb=172.16.10.124 --fork --logpath=/var/log/mongodb/mongos.log

Output indicates successful fork and background execution.

Shard Configuration

Modify each shard's /etc/mongod.conf similarly to the config server (set configsvr=true for config server only).

[root@node3 ~]# vim /etc/mongod.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
dbpath=/mongodb/data
pidfilepath=/var/run/mongodb/mongod.pid
#bind_ip=127.0.0.1
httpinterface=true
rest=true
[root@node3 ~]# service mongod start
Starting mongod:                               [  OK  ]

Repeat on both shard nodes.

Add Shard Nodes to Mongos

[root@node1 ~]# mongo --host 172.16.10.123
mongos> sh.addShard("172.16.10.125")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("172.16.10.126")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()

Shows the two shards and their hosts.

Enable Sharding on a Database

mongos> sh.enableSharding("testdb")
{ "ok" : 1 }

Shard a Collection

mongos> sh.shardCollection("testdb.students", {"age": 1})
{ "collectionsharded" : "testdb.students", "ok" : 1 }

Insert Test Data and Verify Distribution

mongos> use testdb
mongos> for (i=1;i<=100000;i++) db.students.insert({name:"student"+i, age:(i%120), addr:"china"})
WriteResult({ "nInserted" : 1 })
mongos> db.students.find().count()
100000
mongos> sh.status()

The status output shows chunks distributed across shard0000 and shard0001, confirming successful sharding.

Conclusion

MongoDB replication sets and data sharding provide superior performance and simpler operations compared to MySQL for large‑scale data handling. Although MongoDB is still evolving and has open issues, its capabilities make it a strong choice for future projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databaseshardingReplicationClusterMongoDB
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.