Hi Everyone,
I’m trying to create a 2 shards with 3 replica sets on each and config server with 3 replicas
Below is my code
Config server:
start mongod --configsvr --port 28041 --bind_ip localhost --replSet config_repl --dbpath C:\mongo_shard\sharding_demo\configsrv
start mongod --configsvr --port 28042 --bind_ip localhost --replSet config_repl --dbpath C:\mongo_shard\sharding_demo\configsrv1
start mongod --configsvr --port 28043 --bind_ip localhost --replSet config_repl --dbpath C:\mongo_shard\sharding_demo\configsrv2
mongosh --host localhost --port 28041
rsconf = {
_id: "config_repl",
members: [
{
_id: 0,
host: "localhost:28041"
},
{
_id: 1,
host: "localhost:28042"
},
{
_id: 2,
host: "localhost:28043"
}
]
}
rs.initiate(rsconf)
rs.status()
Shard server 1:
start mongod --shardsvr --port 28081 --bind_ip localhost --replSet shard_repl --dbpath C:\mongo_shard\sharding_demo\shardrep1
start mongod --shardsvr --port 28082 --bind_ip localhost --replSet shard_repl --dbpath C:\mongo_shard\sharding_demo\shardrep2
start mongod --shardsvr --port 28083 --bind_ip localhost --replSet shard_repl --dbpath C:\mongo_shard\sharding_demo\shardrep3
mongosh --host localhost --port 28081
rsconf = {
_id: "shard_repl",
members: [
{
_id: 0,
host: "localhost:28081"
},
{
_id: 1,
host: "localhost:28082"
},
{
_id: 2,
host: "localhost:28083"
}
]
}
rs.initiate(rsconf)
rs.status()
Shard server 2:
start mongod --shardsvr --port 29081 --bind_ip localhost --replSet shard2_repl --dbpath C:\mongo_shard\sharding_demo\shard2rep1
start mongod --shardsvr --port 29082 --bind_ip localhost --replSet shard2_repl --dbpath C:\mongo_shard\sharding_demo\shard2rep2
start mongod --shardsvr --port 29083 --bind_ip localhost --replSet shard2_repl --dbpath C:\mongo_shard\sharding_demo\shard2rep3
mongosh --host localhost --port 29081
rsconf = {
_id: "shard2_repl",
members: [
{
_id: 0,
host: "localhost:29081"
},
{
_id: 1,
host: "localhost:29082"
},
{
_id: 2,
host: "localhost:29083"
}
]
}
rs.initiate(rsconf)
rs.status()
MongoS:
mongos --configdb config_repl/localhost:28041,localhost:28042,localhost:28043 --port 47024
Connect to the Sharded Cluster
mongosh --host localhost --port 47024
sh.addShard( "shard_repl/localhost:28081,localhost:28082,localhost:28083")
sh.addShard( "shard2_repl/localhost:29081,localhost:29082,localhost:29083")
sh.enableSharding("db") --- users is a database name
sh.status()
db.customers.createIndex({"cust_id":"hashed"})
sh.shardCollection("db.customers",{"cust_id":"hashed"})
db.getSiblingDB("db").customers.getShardDistribution();
If you see below the data resisdes on single shard server only not on shard 2 server
Shard shard_repl at shard_repl/localhost:28081,localhost:28082,localhost:28083
{
data: '231.71MiB',
docs: 499998,
chunks: 1,
'estimated data per chunk': '231.71MiB',
'estimated docs per chunk': 499998
}
---
Totals
{
data: '231.71MiB',
docs: 499998,
chunks: 1,
'Shard shard_repl': [
'100 % data',
'100 % docs in cluster',
'485B avg obj size on shard'
]
}
Even i tried splitAt command, but it was not working