MongoDB 3.2 Shard creation script

Below shell script will be useful to create a MongoDB’s sharded environment on a single sever/multiple servers depending on your requirement.

Things to remember to execute below automated script.

1) We should have required privileges to the user on your Linux box ( preferebly root user )
2) We need to replace the hostname of yours through out the script
3) Please check whether the /tmp has ample amount of space to create the required mongo prerequisites, else change the directory path to other one.
4) This script is intended for your testing if you’re creating this Shard on a single server.

# cat /etc/mongod.conf
# MongoDB Configuration File

# General Settings
fork = true
quiet = true

# Logging
verbose = true
logappend = true
logpath = /opt/mongodb/mongod.log

Shard creation script :
======================

##### Killing the existing Mongo processes ################
for i in `ps -ef | egrep ‘shardsvr|configsvr|replSet|configdb’ | grep -v egrep | awk -F” ” ‘{print $2}’`; do kill -9 $i; done

##### Creating Mongo data & log files ################

rm -rf /home/dbversity/mongodb-3.2/data/*
mkdir -p /home/dbversity/mongodb-3.2/data/ /home/dbversity/mongodb-3.2/data/logs

cd /home/dbversity/mongodb-3.2/data/

mkdir -p config1 config2 config3 arbiter1 arbiter2 arbiter3 router /home/dbversity/mongodb-3.2/data/shard1_1 /home/dbversity/mongodb-3.2/data/shard1_2 /home/dbversity/mongodb-3.2/data/shard2_1 /home/dbversity/mongodb-3.2/data/shard2_2 /home/dbversity/mongodb-3.2/data/shard3_1 /home/dbversity/mongodb-3.2/data/shard3_2

cd /home/dbversity/mongodb-3.2/mongodb-linux-x86_64-ubuntu1404-3.2.0/bin

##### Starting the Mongo Config,Shard,Arbiter & Router services ################

## Config Servers #####
mongod –configsvr –dbpath /home/dbversity/mongodb-3.2/data/config1 –logpath /home/dbversity/mongodb-3.2/data/logs/config1.log –port 39000 –config /etc/mongod.conf &
mongod –configsvr –dbpath /home/dbversity/mongodb-3.2/data/config2 –logpath /home/dbversity/mongodb-3.2/data/logs/config2.log –port 39001 –config /etc/mongod.conf &
mongod –configsvr –dbpath /home/dbversity/mongodb-3.2/data/config3 –logpath /home/dbversity/mongodb-3.2/data/logs/config3.log –port 39002 –config /etc/mongod.conf &

## Replica Set 1 ######
mongod –shardsvr –replSet rs1 –dbpath /home/dbversity/mongodb-3.2/data/shard1_1 –logpath /home/dbversity/mongodb-3.2/data/logs/shard1_1.log –port 27010 –config /etc/mongod.conf &
mongod –shardsvr –replSet rs1 –dbpath /home/dbversity/mongodb-3.2/data/shard1_2 –logpath /home/dbversity/mongodb-3.2/data/logs/shard1_2.log –port 27011 –config /etc/mongod.conf &

## Replica Set 2 ######
mongod –shardsvr –replSet rs2 –dbpath /home/dbversity/mongodb-3.2/data/shard2_1 –logpath /home/dbversity/mongodb-3.2/data/logs/shard2_1.log –port 27020 –config /etc/mongod.conf &
mongod –shardsvr –replSet rs2 –dbpath /home/dbversity/mongodb-3.2/data/shard2_2 –logpath /home/dbversity/mongodb-3.2/data/logs/shard2_2.log –port 27021 –config /etc/mongod.conf &

## Replica Set 3 ######
mongod –shardsvr –replSet rs3 –dbpath /home/dbversity/mongodb-3.2/data/shard3_1 –logpath /home/dbversity/mongodb-3.2/data/logs/shard3_1.log –port 27030 –config /etc/mongod.conf &
mongod –shardsvr –replSet rs3 –dbpath /home/dbversity/mongodb-3.2/data/shard3_2 –logpath /home/dbversity/mongodb-3.2/data/logs/shard3_2.log –port 27031 –config /etc/mongod.conf &

## Arbiters ####
mongod –replSet rs1 –dbpath /home/dbversity/mongodb-3.2/data/arbiter1 –logpath /home/dbversity/mongodb-3.2/data/logs/arbiter1.log –port 27012 –config /etc/mongod.conf &
mongod –replSet rs2 –dbpath /home/dbversity/mongodb-3.2/data/arbiter2 –logpath /home/dbversity/mongodb-3.2/data/logs/arbiter2.log –port 27022 –config /etc/mongod.conf &
mongod –replSet rs3 –dbpath /home/dbversity/mongodb-3.2/data/arbiter3 –logpath /home/dbversity/mongodb-3.2/data/logs/arbiter3.log –port 27032 –config /etc/mongod.conf &

sleep 200

mongos –configdb dbfry.com:39000,dbfry.com:39001,dbfry.com:39002 –logpath /home/dbversity/mongodb-3.2/data/logs/router.log –port 10000 &

sleep 200
mongo dbfry.com:27010/admin –eval “rs.initiate()”
mongo dbfry.com:27020/admin –eval “rs.initiate()”
mongo dbfry.com:27030/admin –eval “rs.initiate()”

sleep 100
echo -e “\n\n Replica sets are being added. \n\n”

mongo dbfry.com:27010/admin –eval “rs.add(\”dbfry.com:27011\”)”
mongo dbfry.com:27020/admin –eval “rs.add(\”dbfry.com:27021\”)”
mongo dbfry.com:27030/admin –eval “rs.add(\”dbfry.com:27031\”)”

mongo dbfry.com:27010/admin –eval “rs.addArb(\”dbfry.com:27012\”)”
mongo dbfry.com:27020/admin –eval “rs.addArb(\”dbfry.com:27022\”)”
mongo dbfry.com:27030/admin –eval “rs.addArb(\”dbfry.com:27032\”)”

sleep 200
mongo dbfry.com:10000/admin –eval “sh.addShard(\”rs1/dbfry.com:27010,dbfry.com:27011\”)”
mongo dbfry.com:10000/admin –eval “sh.addShard(\”rs2/dbfry.com:27020,dbfry.com:27021\”)”
mongo dbfry.com:10000/admin –eval “sh.addShard(\”rs3/dbfry.com:27030,dbfry.com:27031\”)”

Script execution :
=================
#sh -x shrd_script.sh
+ + ps -ef
egrep shardsvr|configsvr|replSet|configdb
+ + awk -F {print $2}
grep -v egrep
+ kill -9 5142
+ kill -9 5143
+ kill -9 5144
+ kill -9 5145
+ kill -9 5146
+ kill -9 5147
+ kill -9 5148
+ kill -9 5149
+ kill -9 5150
+ kill -9 5151
+ kill -9 5152
+ kill -9 5153
+ kill -9 5447
+ rm -rf /home/dbversity/mongodb-3.2/data/arbiter1 /home/dbversity/mongodb-3.2/data/arbiter2 /home/dbversity/mongodb-3.2/data/arbiter3 /home/dbversity/mongodb-3.2/data/config1 /home/dbversity/mongodb-3.2/data/config2 /home/dbversity/mongodb-3.2/data/config3 /home/dbversity/mongodb-3.2/data/logs /home/dbversity/mongodb-3.2/data/router /home/dbversity/mongodb-3.2/data/shard1_1 /home/dbversity/mongodb-3.2/data/shard1_2 /home/dbversity/mongodb-3.2/data/shard2_1 /home/dbversity/mongodb-3.2/data/shard2_2 /home/dbversity/mongodb-3.2/data/shard3_1 /home/dbversity/mongodb-3.2/data/shard3_2
+ mkdir -p /home/dbversity/mongodb-3.2/data/ /home/dbversity/mongodb-3.2/data/logs
+ cd /home/dbversity/mongodb-3.2/data/
+ mkdir -p config1 config2 config3 arbiter1 arbiter2 arbiter3 router /home/dbversity/mongodb-3.2/data/shard1_1 /home/dbversity/mongodb-3.2/data/shard1_2 /home/dbversity/mongodb-3.2/data/shard2_1 /home/dbversity/mongodb-3.2/data/shard2_2 /home/dbversity/mongodb-3.2/data/shard3_1 /home/dbversity/mongodb-3.2/data/shard3_2
+ cd /home/dbversity/mongodb-3.2/mongodb-linux-x86_64-ubuntu1404-3.2.0/bin
+ mongod –configsvr –dbpath /home/dbversity/mongodb-3.2/data/config1 –logpath /home/dbversity/mongodb-3.2/data/logs/config1.log –port 39000 –config /etc/mongod.conf
+ + mongod –shardsvrmongod –replSet rs1 –configsvr –dbpath /home/dbversity/mongodb-3.2/data/shard1_1 –logpath –dbpath /home/dbversity/mongodb-3.2/data/logs/shard1_1.log /home/dbversity/mongodb-3.2/data/config3 –port –logpath 27010 /home/dbversity/mongodb-3.2/data/logs/config3.log –config –port /etc/mongod.conf 39002
–config+ + + mongodmongod /etc/mongod.conf
–shardsvr –replSet –shardsvr rs1 –dbpath –replSet /home/dbversity/mongodb-3.2/data/shard1_2 rs2 –logpath –dbpath /home/dbversity/mongodb-3.2/data/logs/shard1_2.log /home/dbversity/mongodb-3.2/data/shard2_1 –port+ –logpath /home/dbversity/mongodb-3.2/data/logs/shard2_1.log 27011 –config –port /etc/mongod.conf 27020
–config /etc/mongod.conf
+ sleep+ 200
mongod –shardsvrmongod –shardsvr –replSet rs2 –dbpath /home/dbversity/mongodb-3.2/data/shard2_2 –logpath /home/dbversity/mongodb-3.2/data/logs/shard2_2.log –port 27021 –config /etc/mongod.conf
–replSetmongod rs3 –dbpath –configsvr+ mongod+ –replSet rs1 –dbpathmongod /home/dbversity/mongodb-3.2/data/arbiter1 –logpath –replSet rs3 /home/dbversity/mongodb-3.2/data/logs/arbiter1.log –port –dbpath /home/dbversity/mongodb-3.2/data/arbiter3 27012 –config –logpath /home/dbversity/mongodb-3.2/data/logs/arbiter3.log /etc/mongod.conf
–port 27032 –config /etc/mongod.conf
/home/dbversity/mongodb-3.2/data/shard3_1 –dbpath –logpath /home/dbversity/mongodb-3.2/data/config2 /home/dbversity/mongodb-3.2/data/logs/shard3_1.log –logpath –port /home/dbversity/mongodb-3.2/data/logs/config2.log 27030+ –port –configmongod+ 39001 –config –shardsvr –replSet /etc/mongod.conf
mongod –replSet rs2 –dbpath /home/dbversity/mongodb-3.2/data/arbiter2 –logpath /home/dbversity/mongodb-3.2/data/logs/arbiter2.log –port 27022 –config /etc/mongod.conf
rs3 –dbpath /home/dbversity/mongodb-3.2/data/shard3_2 –logpath /home/dbversity/mongodb-3.2/data/logs/shard3_2.log –port 27031 –config /etc/mongod.conf
/etc/mongod.conf

+ sleep 200
+ mongos –configdb dbfry.com:39000,dbfry.com:39001,dbfry.com:39002 –logpath /home/dbversity/mongodb-3.2/data/logs/router.log –port 10000

+ mongo dbfry.com:27010/admin –eval rs.initiate()
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27010/admin
{
“info2” : “no configuration specified. Using a default configuration for the set”,
“me” : “dbfry.com:27010”,
“ok” : 1
}
+ mongo dbfry.com:27020/admin –eval rs.initiate()
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27020/admin
{
“info2” : “no configuration specified. Using a default configuration for the set”,
“me” : “dbfry.com:27020”,
“ok” : 1
}
+ mongo dbfry.com:27030/admin –eval rs.initiate()
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27030/admin
{
“info2” : “no configuration specified. Using a default configuration for the set”,
“me” : “dbfry.com:27030”,
“ok” : 1
}
+ sleep 100
+ echo -e \n\n Replica sets are being added. \n\n
-e

Replica sets are being added.
+ mongo dbfry.com:27010/admin –eval rs.add(“dbfry.com:27011”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27010/admin
{ “ok” : 1 }
+ mongo dbfry.com:27020/admin –eval rs.add(“dbfry.com:27021”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27020/admin
{ “ok” : 1 }
+ mongo dbfry.com:27030/admin –eval rs.add(“dbfry.com:27031”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27030/admin
{ “ok” : 1 }
+ mongo dbfry.com:27010/admin –eval rs.addArb(“dbfry.com:27012”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27010/admin
{ “ok” : 1 }
+ mongo dbfry.com:27020/admin –eval rs.addArb(“dbfry.com:27022”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27020/admin
{ “ok” : 1 }
+ mongo dbfry.com:27030/admin –eval rs.addArb(“dbfry.com:27032”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:27030/admin
{ “ok” : 1 }
+ sleep 200

+ mongo dbfry.com:10000/admin –eval sh.addShard(“rs1/dbfry.com:27010,dbfry.com:27011”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:10000/admin
{ “shardAdded” : “rs1”, “ok” : 1 }
+ mongo dbfry.com:10000/admin –eval sh.addShard(“rs2/dbfry.com:27020,dbfry.com:27021”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:10000/admin
{ “shardAdded” : “rs2”, “ok” : 1 }
+ mongo dbfry.com:10000/admin –eval sh.addShard(“rs3/dbfry.com:27030,dbfry.com:27031”)
MongoDB shell version: 3.2.0
connecting to: dbfry.com:10000/admin
{ “shardAdded” : “rs3”, “ok” : 1 }
#
#
#
#
#mongo –port 27010
MongoDB shell version: 3.2.0
connecting to: 127.0.0.1:27010/test
rs1:PRIMARY>
rs1:PRIMARY> rs.status()
{
“set” : “rs1”,
“date” : ISODate(“2015-12-30T20:28:23.057Z”),
“myState” : 1,
“term” : NumberLong(1),
“heartbeatIntervalMillis” : NumberLong(2000),
“members” : [
{
“_id” : 0,
“name” : “dbfry.com:27010”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 743,
“optime” : {
“ts” : Timestamp(1451507067, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“electionTime” : Timestamp(1451506963, 2),
“electionDate” : ISODate(“2015-12-30T20:22:43Z”),
“configVersion” : 3,
“self” : true
},
{
“_id” : 1,
“name” : “dbfry.com:27011”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 236,
“optime” : {
“ts” : Timestamp(1451507067, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“lastHeartbeat” : ISODate(“2015-12-30T20:28:21.598Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:22.656Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
},
{
“_id” : 2,
“name” : “dbfry.com:27012”,
“health” : 1,
“state” : 7,
“stateStr” : “ARBITER”,
“uptime” : 235,
“lastHeartbeat” : ISODate(“2015-12-30T20:28:21.597Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:18.675Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
}
],
“ok” : 1
}
rs1:PRIMARY>
rs1:PRIMARY>
bye
#
#
#mongo –port 27020
MongoDB shell version: 3.2.0
connecting to: 127.0.0.1:27020/test
rs2:PRIMARY> rs.status()
{
“set” : “rs2”,
“date” : ISODate(“2015-12-30T20:28:30.085Z”),
“myState” : 1,
“term” : NumberLong(1),
“heartbeatIntervalMillis” : NumberLong(2000),
“members” : [
{
“_id” : 0,
“name” : “dbfry.com:27020”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 750,
“optime” : {
“ts” : Timestamp(1451507067, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“electionTime” : Timestamp(1451506965, 1),
“electionDate” : ISODate(“2015-12-30T20:22:45Z”),
“configVersion” : 3,
“self” : true
},
{
“_id” : 1,
“name” : “dbfry.com:27021”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 243,
“optime” : {
“ts” : Timestamp(1451507067, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“lastHeartbeat” : ISODate(“2015-12-30T20:28:29.857Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:29.376Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
},
{
“_id” : 2,
“name” : “dbfry.com:27022”,
“health” : 1,
“state” : 7,
“stateStr” : “ARBITER”,
“uptime” : 242,
“lastHeartbeat” : ISODate(“2015-12-30T20:28:29.857Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:28.834Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
}
],
“ok” : 1
}
rs2:PRIMARY>
bye
#
#
#
#mongo –port 27030
MongoDB shell version: 3.2.0
connecting to: 127.0.0.1:27030/test
rs3:PRIMARY>
rs3:PRIMARY>
rs3:PRIMARY> rs.status()
{
“set” : “rs3”,
“date” : ISODate(“2015-12-30T20:28:37.382Z”),
“myState” : 1,
“term” : NumberLong(1),
“heartbeatIntervalMillis” : NumberLong(2000),
“members” : [
{
“_id” : 0,
“name” : “dbfry.com:27030”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 757,
“optime” : {
“ts” : Timestamp(1451507067, 2),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“electionTime” : Timestamp(1451506967, 1),
“electionDate” : ISODate(“2015-12-30T20:22:47Z”),
“configVersion” : 3,
“self” : true
},
{
“_id” : 1,
“name” : “dbfry.com:27031”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 250,
“optime” : {
“ts” : Timestamp(1451507067, 2),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2015-12-30T20:24:27Z”),
“lastHeartbeat” : ISODate(“2015-12-30T20:28:36.049Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:34.474Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
},
{
“_id” : 2,
“name” : “dbfry.com:27032”,
“health” : 1,
“state” : 7,
“stateStr” : “ARBITER”,
“uptime” : 249,
“lastHeartbeat” : ISODate(“2015-12-30T20:28:36.049Z”),
“lastHeartbeatRecv” : ISODate(“2015-12-30T20:28:33.980Z”),
“pingMs” : NumberLong(0),
“configVersion” : 3
}
],
“ok” : 1
}
rs3:PRIMARY>
bye
#
#
#
#mongo –port 10000
MongoDB shell version: 3.2.0
mongos>
mongos>
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“56843c4fc0c1d7e9199cce2f”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/dbfry.com:27010,dbfry.com:27011” }
{ “_id” : “rs2”, “host” : “rs2/dbfry.com:27020,dbfry.com:27021” }
{ “_id” : “rs3”, “host” : “rs3/dbfry.com:27030,dbfry.com:27031” }
active mongoses:
“3.2.0” : 1
balancer:
Currently enabled: yes
Currently running: yes
Balancer lock taken at Thu Dec 31 2015 01:58:45 GMT+0530 (IST) by dbfry.com:10000:1451506761:1804289383:Balancer:1681692777
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:

mongos>
mongos>
mongos>
mongos> use shrdb
switched to db shrdb
mongos>
mongos> db.shrcol.ensureIndex({“user_id” : 1})
{
“raw” : {
“rs1/dbfry.com:27010,dbfry.com:27011” : {
“createdCollectionAutomatically” : true,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1,
“$gleStats” : {
“lastOpTime” : Timestamp(1451507491, 2),
“electionId” : ObjectId(“56843d130000000000000001”)
}
}
},
“ok” : 1
}
mongos>
mongos>
mongos> show collections
shrcol
mongos>
mongos> db.shrcol.find()
mongos>
mongos> sh.enableSharding(“shrdb”)
{ “ok” : 1 }
mongos>
mongos>
mongos> use admin
switched to db admin
mongos>
mongos> db.runCommand( { shardCollection: “shrdb.shrcol”, key : {user_id:1}})
{ “collectionsharded” : “shrdb.shrcol”, “ok” : 1 }
mongos>
mongos>
mongos>
mongos> sh.startBalancer()
mongos> sh.isBalancerRunning()
false
mongos> sh.getBalancerState()
true
mongos>
mongos> use shrdb
switched to db shrdb
mongos>
mongos>
mongos>
mongos> // inserting dummy data using a for loop fo 10 lakh records
mongos> for(var i=1; i <= 1000000 ; i++){db.shrcol.insert({ “user_id” : i, “name” : “DBFry.COM is now in-association with DBVersity.COM”})}

In the middle of above inserts … few stats.
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“56843c4fc0c1d7e9199cce2f”)
}
shards:
{ “_id” : “rs1”, “host” : “rs1/dbfry.com:27010,dbfry.com:27011” }
{ “_id” : “rs2”, “host” : “rs2/dbfry.com:27020,dbfry.com:27021” }
{ “_id” : “rs3”, “host” : “rs3/dbfry.com:27030,dbfry.com:27031” }
active mongoses:
“3.2.0” : 1
balancer:
Currently enabled: yes
Currently running: yes
Balancer lock taken at Thu Dec 31 2015 02:18:43 GMT+0530 (IST) by dbfry.com:10000:1451506761:1804289383:Balancer:1681692777
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
3 : Success
2 : Failed with error ‘aborted’, from rs1 to rs2
databases:
{ “_id” : “shrdb”, “primary” : “rs1”, “partitioned” : true }
shrdb.shrcol
shard key: { “user_id” : 1 }
unique: false
balancing: true
chunks:
rs1 2
rs2 2
rs3 1
{ “user_id” : { “$minKey” : 1 } } –>> { “user_id” : 2 } on : rs2 Timestamp(2, 0)
{ “user_id” : 2 } –>> { “user_id” : 12 } on : rs3 Timestamp(3, 0)
{ “user_id” : 12 } –>> { “user_id” : 83898 } on : rs1 Timestamp(4, 1)
{ “user_id” : 83898 } –>> { “user_id” : 173734 } on : rs1 Timestamp(3, 3)
{ “user_id” : 173734 } –>> { “user_id” : { “$maxKey” : 1 } } on : rs2 Timestamp(4, 0)

mongos>
mongos> show dbs
config 0.001GB
shrdb 0.011GB
mongos>
mongos>
mongos> use shrdb
switched to db shrdb
mongos>
mongos> show collections
shrcol
mongos>
mongos> db.shrcol.find()
{ “_id” : ObjectId(“56844098d8ff860c53740863”), “user_id” : 12, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“56844098d8ff860c53740864”), “user_id” : 13, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740865”), “user_id” : 14, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740866”), “user_id” : 15, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740867”), “user_id” : 16, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740868”), “user_id” : 17, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740869”), “user_id” : 18, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086a”), “user_id” : 19, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086b”), “user_id” : 20, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086c”), “user_id” : 21, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086d”), “user_id” : 22, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086e”), “user_id” : 23, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c5374086f”), “user_id” : 24, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740870”), “user_id” : 25, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740871”), “user_id” : 26, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740872”), “user_id” : 27, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740873”), “user_id” : 28, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740874”), “user_id” : 29, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740875”), “user_id” : 30, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
{ “_id” : ObjectId(“568440bad8ff860c53740876”), “user_id” : 31, “name” : “DBFry.COM is now in-association with DBVersity.COM” }
Type “it” for more
mongos>
mongos>
mongos> db.shrcol.count()
283974
mongos>
mongos>
bye
#
#

#mongo –port 27010
MongoDB shell version: 3.2.0
connecting to: 127.0.0.1:27010/test
rs1:PRIMARY>
rs1:PRIMARY> use shrdb
switched to db shrdb
rs1:PRIMARY> db.shrcol.count()
173722
rs1:PRIMARY>
bye
#

#mongo localhost:27020/shrdb
MongoDB shell version: 3.2.0
connecting to: localhost:27020/shrdb
rs2:PRIMARY>
rs2:PRIMARY> use shrdb
switched to db shrdb
rs2:PRIMARY> db.shrcol.count()
168026

#mongo localhost:27030/shrdb
MongoDB shell version: 3.2.0
connecting to: localhost:27030/shrdb
rs3:PRIMARY> db.shrcol.count()
10063
#mongo localhost:10000/shrdb
MongoDB shell version: 3.2.0
connecting to: localhost:10000/shrdb
mongos>
mongos> db.shrcol.find({“user_id”:1}).explain()
{
“queryPlanner” : {
“mongosPlannerVersion” : 1,
“winningPlan” : {
“stage” : “SINGLE_SHARD”,
“shards” : [
{
“shardName” : “rs2”,
“connectionString” : “rs2/dbfry.com:27020,dbfry.com:27021”,
“serverInfo” : {
“host” : “dbfry.com”,
“port” : 27020,
“version” : “3.2.0”,
“gitVersion” : “45d947729a0315accb6d4f15a6b06be6d9c19fe7”
},
“plannerVersion” : 1,
“namespace” : “shrdb.shrcol”,
“indexFilterSet” : false,
“parsedQuery” : {
“user_id” : {
“$eq” : 1
}
},
“winningPlan” : {
“stage” : “FETCH”,
“inputStage” : {
“stage” : “SHARDING_FILTER”,
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“user_id” : 1
},
“indexName” : “user_id_1”,
“isMultiKey” : false,
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 1,
“direction” : “forward”,
“indexBounds” : {
“user_id” : [
“[1.0, 1.0]”
]
}
}
}
},
“rejectedPlans” : [ ]
}
]
}
},
“ok” : 1
}
mongos>
mongos>
mongos>
mongos>
bye
#
#
#mongo localhost:39001/admin
MongoDB shell version: 3.2.0
connecting to: localhost:39001/admin
configsvr> use config
switched to db config
configsvr>
configsvr>
configsvr>
configsvr>
configsvr>
configsvr> db.chunks.find({“ns” :”shrdb.shrcol”},{“min.user_id”:1,”max.user_id”:1,”_id”:0}).limit(5)
{ “min” : { “user_id” : { “$minKey” : 1 } }, “max” : { “user_id” : 2 } }
{ “min” : { “user_id” : 2 }, “max” : { “user_id” : 12 } }
{ “min” : { “user_id” : 12 }, “max” : { “user_id” : 83898 } }
{ “min” : { “user_id” : 83898 }, “max” : { “user_id” : 173734 } }
{ “min” : { “user_id” : 173734 }, “max” : { “user_id” : 257620 } }
configsvr>
configsvr>
configsvr>
configsvr> db.chunks.find({“ns” :”shrdb.shrcol”},{“max.user_id”:1,”_id”:0}).sort({“max.user_id”:-1}).limit(1)
{ “max” : { “user_id” : { “$maxKey” : 1 } } }
configsvr>
configsvr>
configsvr>

  • Ask Question