MongoDB Sharding-02–Setup Config Servers and Shard mongod Processes

Stop mongod service
[localhost]$ sudo service mongod stop

Start Config Server 1 on Port 27018
[root]#mongod --configvr --dbpath /var/lib/cfgsvr1 --port 27018 --fork --logpath /var/lib/cfgsvr1/log --logappend --nojournal

Start Config Server 2 on Port 27019
[root]#mongod --configvr --dbpath /var/lib/cfgsvr2 --port 27019 --fork --logpath /var/lib/cfgsvr2/log --logappend –nojournal

Start Config Server 3 on Port 27020
[root]#mongod --configvr --dbpath /var/lib/cfgsvr3 --port 27020 --fork --logpath /var/lib/cfgsvr3/log --logappend –nojournal

Start Shard 1 Replica 1 on port 27011 with ReplicaSet1 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet1 --dbpath /var/lib/replica11 --logpath /var/lib/replica11/log --port27011 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 1 Replica 2 on port 27012 with ReplicaSet1 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet1 --dbpath /var/lib/replica12 --logpath /var/lib/replica12/log --port27012 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 1 Replica 3 on port 27013 with ReplicaSet1 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet1 --dbpath /var/lib/replica13 --logpath /var/lib/replica13/log --port27013 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 2 Replica 1 on port 27021 with ReplicaSet2 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet2 --dbpath /var/lib/replica21 --logpath /var/lib/replica21/log --port27021 --logappend --smallfiles --oplogSize50 --fork –nojournal

Start Shard 2 Replica 2 on port 27022 with ReplicaSet2 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet2 --dbpath /var/lib/replica22 --logpath /var/lib/replica22/log --port27022 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 2 Replica 3 on port 27023 with ReplicaSet2 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet2 --dbpath /var/lib/replica23 --logpath /var/lib/replica23/log --port27023 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 3 Replica 1 on port 27031 with ReplicaSet3 as the name of Replica set
--[root]#mongodshardsvr --replSet ReplicaSet3	--dbpath /var/lib/replica31 --logpath	
	/var/lib/replica31/log  port27031  logappend---- --smallfiles --oplogSize50 --fork --nojournal

Start Shard 3 Replica 2 on port 27032 with ReplicaSet3 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet3 --dbpath /var/lib/replica32 --logpath /var/lib/replica32/log --port27032 --logappend --smallfiles --oplogSize50 --fork --nojournal

Start Shard 3 Replica 3 on port 27033 with ReplicaSet3 as the name of Replica set
[root]# mongod --shardsvr --replSet ReplicaSet3 --dbpath /var/lib/replica33 --logpath /var/lib/replica33/log --port27033 --logappend --smallfiles --oplogSize50 --fork --nojournal

FAQs

What is MongoDB sharding?

Sharding is the process of partitioning data across multiple replica sets (hosts), enabling horizontal scaling.

How does MongoDB sharding work?

In MongoDB, a sharded cluster consists of shards, routers, and config servers. The data is distributed across the shards, the routers handle client requests, and the config servers maintain the overall shard state.

What are shard keys in MongoDB?

Shard keys are based on fields inside each document. The values in those fields will decide on which shard the document will reside, according to the shard ranges and amount of chunks. This data is stored and kept in the config server replica set.

Each shard key will require an index or auto-build one, as part of the sharding command, if the collection is empty and the required index is missing. The shard key has a direct impact on the cluster performance and could lead to bottlenecks for your application access patterns. Use the following documentation to choose the best shard key for you.

Also See:

Leave a Reply