I give you a step-by-step guide to create several sharps and instances of MongoDB on a single machine (obviously, to do this on multiple nodes, some changes should be made)
1- First we go to the route where we are going to work and create directories to store our data:
mkdir shard_example
cd shard_example
mkdir s0_0 s0_1 s1_0 s1_1 config_0 config_1
2- Created the directories, we raise the servers that are going to maintain the configuration data of our cluster:
mongod --configsvr --dbpath config_0 --port 26050 --logpath log.cfg0 --logappend --replSet configRS --fork
mongod --configsvr --dbpath config_1 --port 26051 --logpath log.cfg1 --logappend --replSet configRS --fork
3- I raise 4 mongo servers, 2 for a shard (with its replica set) and 2 for the other (EYE, never use an even number of nodes in a shard, if you need a replication factor of 2, get a referee). The configurations of --smallfiles and --oplogSize are set because I create a "toy" environment
mongod --shardsvr --replSet S0 --dbpath s0_0 --logpath log.s0_0 --port 27000 --logappend --smallfiles --oplogSize 50 --fork
mongod --shardsvr --replSet S0 --dbpath s0_1 --logpath log.s0_1 --port 27001 --logappend --smallfiles --oplogSize 50 --fork
mongod --shardsvr --replSet S1 --dbpath s1_0 --logpath log.s1_0 --port 27100 --logappend --smallfiles --oplogSize 50 --fork
mongod --shardsvr --replSet S1 --dbpath s1_1 --logpath log.s1_1 --port 27101 --logappend --smallfiles --oplogSize 50 --fork
4- We raise mongos, which will act as a "resolutor" of requests, indicating where to consult the configuration of our cluster:
mongos --configdb configRS / localhost: 26050, localhost: 26051 --logpath log.mongos0 --logappend --port 27017 --fork
(This is using the localhost configuration for everything, remember to change it if it is not your case)
4- Initialize Replica Set of the ConfigServer:
mongo --port 26050
rs.initiate ()
rs.add ("localhost: 26051")
4.1: As probably the Replica Set was created with the name of the host, and we are using localhost, change node 0 to localhost:
cfg=rs.config()
cfg.members[0].host="localhost:26050"
rs.reconfig(cfg)
5: Initialize SHARDS, We connect to mongos and we do:
mongos
db = connect ("localhost: 27000 / test")
rs.initiate ()
This is to change the hostname to localhost,
cfg=rs.config()
cfg.members[0].host="localhost:27000"
rs.reconfig(cfg)
rs.add ("localhost: 27001")
db = connect ("localhost: 27017 / test")
sh.addShard ("S0 / localhost: 27000");
6: We repeat for SHARD 2, we connect to mongos and we do:
mongos
db = connect ("localhost: 27100 / test")
rs.initiate ()
This is to change the hostname to localhost,
cfg=rs.config()
cfg.members[0].host="localhost:27100"
rs.reconfig(cfg)
rs.add ("localhost: 27101")
db = connect ("localhost: 27017 / test")
sh.addShard ("S1 / localhost: 27100");
With this you should already have created a cluster, with a mongos server that attacks two config servers to obtain the cluster information, which is formed by two shards, each with two nodes.
Greetings