For setting up the sharded cluster and configuring it through docker, follow the below link.
Link : MongoDB sharded cluster configuration ✨
To set up locally without docker, follow the below steps.
MongoDB
- Download the tar ball. Here I am giving the link for Ubuntu 18.04 Linux x64. Download according to your requirements from here Download mongoDB
- Extract the files from the downloaded archive
tar -zxvf mongodb-linux-*-4.2.6.tgz
-
Create the data and log directories.
a. Create a directory where the MongoDB instance stores its data
sudo mkdir -p /var/lib/mongo
b. Create a directory where the MongoDB instance stores its log
sudo mkdir -p /var/log/mongodb.
c. The user that starts the MongoDB process must have read and write permission to these directories. For example, if you intend to run MongoDB as yourself:
sudo chown
whoami
/var/lib/mongo # Or substitute another usersudo chown
whoami
/var/log/mongodb # Or substitute another user -
Run MongoDB. Go to its bin folder under mongodb installation.
./mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
For Verification.
./mongo
sudo ./mongod --configsvr --replSet creplicaset --dbpath /data/configdb --port 27019 --bind_ip localhost
Then connect to it mongo shell and initiate the replicaSet.
rs.initiate({_id: "creplicaset", configsvr: true, members: [{ _id : 0, host : "localhost:27019" } ] })
- Shard server 1
sudo ./mongod --shardsvr --replSet rs1 --dbpath /data/db --port 27018 --bind_ip localhost
Connect to its mongo shell.
rs.initiate({ _id :"rs1", members: [{ _id : 0, host :"localhost:27018"}] })
- Shard server 2
sudo ./mongod --shardsvr --replSet rs2 --dbpath /data/db1 --port 27020 --bind_ip localhost
Connect to its mongo shell.
rs.initiate({ _id :"rs2", members: [{ _id : 0, host :"localhost:27020"}] })
sudo ./mongos --configdb creplicaset/localhost:27019 --bind_ip localhost
Connect to its mongo shell.
sh.addShard("rs1/localhost:27018")
sh.addShard("rs2/localhost:27020")
Here people is my database and persons is my collection. You have to enable sharding on the database and specify shard key for the collection for which sharding should be enabled.
mongos> sh.enableSharding("people")
mongos> sh.shardCollection("people.persons",{"personId":"hashed"})
The above set up is one config router as a replicaset, one mongos query router and two shards(with one node replicaset). You can increase the nodes. Just assign different ports.