I have mongodb sharded cluster in my local machine(windows). But I didn't understand how to test this.
My configurations are:
Replicaset 1:
mongod --replSet s0 --dbpath E:/mongo/data/shard0/rs0 --port 37017 --shardsvr
mongod --replSet s0 --dbpath E:/mongo/data/shard0/rs1 --port 37018 --shardsvr
mongod --replSet s0 --dbpath E:/mongo/data/shard0/rs2 --port 37019 --shardsvr
mongo --port 37017
config = { _id: "s0", members:[
{ _id : 0, host : "localhost:37017" },
{ _id : 1, host : "localhost:37018" },
{ _id : 2, host : "localhost:37019" }]};
rs.initiate(config)
Replicaset 2:
mongod --replSet s1 --dbpath E:/mongo/data/shard1/rs0 --port 47017 --shardsvr
mongod --replSet s1 --dbpath E:/mongo/data/shard1/rs1 --port 47018 --shardsvr
mongod --replSet s1 --dbpath E:/mongo/data/shard1/rs2 --port 47019 --shardsvr
mongo --port 47017
config = { _id: "s1", members:[
{ _id : 0, host : "localhost:47017" },
{ _id : 1, host : "localhost:47018" },
{ _id : 2, host : "localhost:47019" }]};
rs.initiate(config)
Replicaset 3:
mongod --replSet s2 --dbpath E:/mongo/data/shard2/rs0 --port 57017 --shardsvr
mongod --replSet s2 --dbpath E:/mongo/data/shard2/rs1 --port 57018 --shardsvr
mongod --replSet s2 --dbpath E:/mongo/data/shard2/rs2 --port 57019 --shardsvr
mongo --port 57017
config = { _id: "s2", members:[
{ _id : 0, host : "localhost:57017" },
{ _id : 1, host : "localhost:57018" },
{ _id : 2, host : "localhost:57019" }]};
rs.initiate(config)
Config server Replicaset:
mongod --dbpath E:/mongo/data/config/config-a --replSet conf --port 57040 --configsvr
mongod --dbpath E:/mongo/data/config/config-b --replSet conf --port 57041 --configsvr
mongod --dbpath E:/mongo/data/config/config-c --replSet conf --port 57042 --configsvr
mongo --port 57040
config = { _id: "conf", members:[
{ _id : 0, host : "localhost:57040" },
{ _id : 1, host : "localhost:57041" },
{ _id : 2, host : "localhost:57042" }]};
rs.initiate(config)
Connecting to cluster:
mongos --configdb conf/localhost:57040,localhost:57041,localhost:57042 --port 57050
mongo --port 57050
Adding shards:
db.adminCommand( { addshard : "s0/"+"localhost:37017" } );
db.adminCommand( { addshard : "s1/"+"localhost:47017" } );
db.adminCommand( { addshard : "s2/"+"localhost:57017" } );
Enabling sharding on Database and Collection with shard key:
db.adminCommand({enableSharding: "chat"});
db.adminCommand({shardCollection: "chat.user", key: {userId:1}});
Following is the log When I check shard status with command db.printShardingStatus();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dfa6c3cb121a735f9ad8f6e")
}
shards:
{ "_id" : "s0", "host" : "s0/localhost:37017,localhost:37018,localhost
:37019", "state" : 1 }
{ "_id" : "s1", "host" : "s1/localhost:47017,localhost:47018,localhost
:47019", "state" : 1 }
{ "_id" : "s2", "host" : "s2/localhost:57017,localhost:57018,localhost
:57019", "state" : 1 }
active mongoses:
"4.2.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "chat", "primary" : "s1", "partitioned" : true, "ve
rsion" : { "uuid" : UUID("233a3161-e3d0-4e49-8051-9ebe0580f5bb"), "lastMod" :
1 } }
chat.user
shard key: { "userId" : 1 }
unique: false
balancing: true
chunks:
s1 1
{ "userId" : { "$minKey" : 1 } } -->> { "userId" : { "$m
axKey" : 1 } } on : s1 Timestamp(1, 0)
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
s0 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey"
: 1 } } on : s0 Timestamp(1, 0)
I have inserted 100k documents into user collection of database which was running on 57050. Entire data is stored in only one shard i.e. second replicaset(s1). I didn't understand how the sharding is working here. Its not splitting the across all shards.
When I check the configdb mongos(57040) in cache.databases collection I see only one replica set like below.
"_id" : "chat",
"primary" : "s1",
"partitioned" : true,
"version" : {
"uuid" : UUID("233a3161-e3d0-4e49-8051-9ebe0580f5bb"),
"lastMod" : 1
}
Later if I drop chat db and again i will create new db now in some other shard it's storing. Every time we create database it is taking random replica set.