This guide outlines the steps to set up a MongoDB sharded cluster with three replica sets, each containing three members.
mkdir replica11 replica12 replica13
mkdir replica21 replica22 replica23
mkdir replica31 replica32 replica33
mkdir cfg0 cfg1 cfg2chmod -R 777 replica11
chmod -R 777 replica12
chmod -R 777 replica13
chmod -R 777 replica21
chmod -R 777 replica22
chmod -R 777 replica23
chmod -R 777 replica31
chmod -R 777 replica32
chmod -R 777 replica33
chmod -R 777 cfg0
chmod -R 777 cfg1
chmod -R 777 cfg2mongod --dbpath replica11 --port 27011 --logpath replica11/log --logappend --fork --bind_ip_all --replSet replicaset1 --shardsvr
mongod --dbpath replica12 --port 27012 --logpath replica12/log --logappend --fork --bind_ip_all --replSet replicaset1 --shardsvr
mongod --dbpath replica13 --port 27013 --logpath replica13/log --logappend --fork --bind_ip_all --replSet replicaset1 --shardsvr
mongod --dbpath replica21 --port 27021 --logpath replica21/log --logappend --fork --bind_ip_all --replSet replicaset2 --shardsvr
mongod --dbpath replica22 --port 27022 --logpath replica22/log --logappend --fork --bind_ip_all --replSet replicaset2 --shardsvr
mongod --dbpath replica23 --port 27023 --logpath replica23/log --logappend --fork --bind_ip_all --replSet replicaset2 --shardsvr
mongod --dbpath replica31 --port 27031 --logpath replica31/log --logappend --fork --bind_ip_all --replSet replicaset3 --shardsvr
mongod --dbpath replica32 --port 27032 --logpath replica32/log --logappend --fork --bind_ip_all --replSet replicaset3 --shardsvr
mongod --dbpath replica33 --port 27033 --logpath replica33/log --logappend --fork --bind_ip_all --replSet replicaset3 --shardsvrps -ef | grep mongomongo --port 27011Run the following commands in the MongoDB shell:
rs.initiate()
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27012")
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27013")
rs.conf()
exitmongo --port 27021Run the following commands in the MongoDB shell:
rs.initiate()
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27022")
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27023")
rs.conf()
exitmongo --port 27031Run the following commands in the MongoDB shell:
rs.initiate()
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27032")
rs.add("ip-172-21-35-50.ap-south-1.compute.internal:27033")
rs.conf()
exitmongod --configsvr --dbpath cfg0 --port 26050 --fork --logpath log.cfg0 --replSet cfg
mongod --configsvr --dbpath cfg1 --port 26051 --fork --logpath log.cfg1 --replSet cfg
mongod --configsvr --dbpath cfg2 --port 26052 --fork --logpath log.cfg2 --replSet cfgmongo --port 26050Run the following commands in the MongoDB shell:
rs.initiate()
rs.add("localhost:26051")
rs.add("localhost:26052")
rs.status()
exitps -ef | grep mongod | grep a0mongos --configdb "cfg/localhost:26050,localhost:26051,localhost:26052" --fork --logpath log.mongos1 --port 26061
mongos --configdb "cfg/localhost:26050,localhost:26051,localhost:26052" --fork --logpath log.mongos2 --port 26062mongo --port 26061Run the following commands in the MongoDB shell:
sh.addShard("replicaset1/localhost:27011")
sh.addShard("replicaset2/localhost:27021")
sh.addShard("replicaset3/localhost:27031")
sh.status()
exitmongo --port 26061Run the following commands in the MongoDB shell:
sh.enableSharding("dbname")
sh.status()
exitmongo --port 26061Run the following commands in the MongoDB shell:
sh.shardCollection("dbname.collectionname", { _id: 1 })
sh.status()
exitRun the following commands in the MongoDB shell:
use dbname
db.sales.insert({"name": "vivek"})
db.getLastErrorObj()
use config
db.shards.find()
exit