Mongodb Sharding
# MongoDB Sharding
* * *
## Sharding
In MongoDB, there is another type of cluster called sharding, which can meet the needs of massive data growth in MongoDB.
When MongoDB stores massive amounts of data, a single machine may not be sufficient to store the data, nor may it provide acceptable read/write throughput. In such cases, we can split the data across multiple machines, enabling the database system to store and process more data.
* * *
## Why Use Sharding
* Replicates all write operations to the primary node
* Latency-sensitive data is queried on the primary node
* A single replica set is limited to 12 nodes
* Memory exhaustion can occur when request volume is huge.
* Local disk space is insufficient
* Vertical scaling is expensive
* * *
## MongoDB Sharding
The diagram below illustrates the structure of a sharded cluster in MongoDB:
!(#)
The diagram above contains the following three main components:
* **Shard:**
Used to store the actual data chunks. In a production environment, a shard server role can be assumed by a replica set composed of several machines to prevent single points of failure.
* **Config Server:**
A mongod instance that stores the entire ClusterMetadata, including chunk information.
* **Query Routers:**
The front-end router through which clients connect, making the entire cluster appear as a single database. Front-end applications can use it transparently.
* * *
## Sharding Example
The port distribution for the sharding structure is as follows:
Shard Server 1: 27020
Shard Server 2: 27021
Shard Server 3: 27022
Shard Server 4: 27023
Config Server: 27100
Route Process: 40000
Step 1: Start the Shard Servers
[root@100 /]# mkdir -p /www/mongoDB/shard/s0
[root@100 /]# mkdir -p /www/mongoDB/shard/s1
[root@100 /]# mkdir -p /www/mongoDB/shard/s2
[root@100 /]# mkdir -p /www/mongoDB/shard/s3
[root@100 /]# mkdir -p /www/mongoDB/shard/log
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork
....
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork
Step 2: Start the Config Server
[root@100 /]# mkdir -p /www/mongoDB/shard/config
[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork
**Note:** Here, we can start it just like starting a regular MongoDB service, without adding the --shardsvr and configsvr parameters. Because the function of these two parameters is to change the startup port, we can specify the port ourselves.
Step 3: Start the Route Process
/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500
In the mongos startup parameters, the chunkSize item is used to specify the size of the chunk, in MB, with a default size of 200MB.
Step 4: Configure Sharding
Next, we use the MongoDB Shell to log in to mongos and add Shard nodes.
[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000
MongoDB shell version: 2.0.7
connecting to: 127.0.0.1:40000/admin
mongos> db.runCommand({ addshard:"localhost:27020" })
{ "shardAdded" : "shard0000", "ok" : 1 }
......
mongos> db.runCommand({ addshard:"localhost:27029" })
{ "shardAdded" : "shard0009", "ok" : 1 }
mongos> db.runCommand({ enablesharding:"test" }) #Set the database for sharded storage
{ "ok" : 1 }
mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}})
{ "collectionsharded" : "test.log", "ok" : 1 }
Step 5: No major changes are needed in the program code. Simply connect to the interface port 40000 just as you would connect to a regular MongoDB database.
[](#)[MongoDB Replication (Replica Set)](#)")
[MongoDB Backup (mongodump) & Restore (mongorestore)](#) & Restore (mongorestore)")[](#)
[Volcengine Coding Plan supports Doubao, GLM, DeepSeek, Kimi, MiniMax and other mainstream large models, officially supplied, stable and reliable. Configuration Guide Β₯9.9/month Subscribe Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E)
## 1 Note Write Note
1. #0 chad
li1***6@163.com
(https://segmentfault.com/a/1190000012148843) [](#)139 **1. Create Sharding Replica Set rs0**
# mkdir /data/log
# mkdir /data/db1
# nohup mongod --port 27020 --dbpath=/data/db1 --logpath=/data/log/rs0-1.log --logappend --fork --shardsvr --replSet=rs0 &
# mkdir /data/db2
# nohup mongod --port 27021 --dbpath=/data/db2 --logpath=/data/log/rs0-2.log --logappend --fork --shardsvr --replSet=rs0 &
1.1 Replica Set rs0 Configuration
# mongo localhost:27020
> rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'localhost:27020'}, {_id: 1, host: 'localhost:27021'}]})
> rs.isMaster() #Check primary-secondary relationship
**2. Create Sharding Replica Set rs1**
# mkdir /data/db3
# nohup mongod --port 27030 --dbpath=/data/db3 --logpath=/data/log/rs1-1.log --logappend --fork --shardsvr --replSet=rs1 &
# mkdir /data/db4
# nohup mongod --port 27031 --dbpath=/data/db4 --logpath=/data/log/rs1-2.log --logappend --fork --shardsvr --replSet=rs1 &
2.1 Replica Set rs1 Configuration
# mongo localhost:27030
> rs.initiate({_id: 'rs1', members: [{_id: 0, host: 'localhost:27030'}, {_id: 1, host: 'localhost:27031'}]})
> rs.isMaster() #Check primary-secondary relationship
**3. Create Config Replica Set conf**
# mkdir /data/conf1
# nohup mongod --port 27100 --dbpath=/data/conf1 --logpath=/data/log/conf-1.log --logappend --fork --configsvr --replSet=conf &
# mkdir /data/conf2
# nohup mongod --port 27101 --dbpath=/data/conf2 --logpath=/data/log/conf-2.log --logappend --fork --configsvr --replSet=conf &
3.1 Replica Set conf Configuration
# mongo localhost:27100
> rs.initiate({_id: 'conf', members: [{_id: 0, host: 'localhost:27100'}, {_id: 1, host: 'localhost:27101'}]})
>
YouTip