Basic Sharding Setup
Overview
This example
involves creating a new sharded cluster that consists of a mongos, the config
server replica set, and two shard replica sets.
Servers:
Config Servers:
Server1 -
27019
Server2 -
27019
Server3 -
27019
Shard 1:
Server4 -
27019
Server5 -
27019
Server6 -
27020
Shard 2:
Server7 -
27018
Server8 -
27018
Server9 -
27018
Mongos:
Server10 -
27017
Config Files:
Config Servers:
net:
port: 27019
processManagement:
fork: true
sharding:
clusterRole: configsvr
replication:
replSetName: csrs
storage:
dbPath: /data/sand/data
systemLog:
destination: file
logAppend: true
logRotate: rename
path: /data/sand/logs/mongod.log
Shard Servers:
net:
port: 27019
processManagement:
fork: "true"
sharding:
clusterRole: shardsvr
replication:
replSetName: rs_0
storage:
dbPath: /data/sand/data
systemLog:
destination: file
logAppend: true
logRotate: rename
path: /data/sand/logs/mongod.log
Mongos:
net:
port: 27017
processManagement:
fork: true
sharding:
configDB: csrs/Server1:27019,Server2:27019,Server3:27019
systemLog:
destination: file
logAppend: true
logRotate: rename
path: /home/x007516a/logs/mongos.log
To start the server, commands:
Shard Servers
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
Config Servers
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod_config.conf
Mongos Servers
/home/x007516a/mongodb_3.4.10/bin/mongos
-f /home/tommy/conf/mongos.conf
Deploy Sharded Cluster
1.
Create the Config Server Replica Set
Start each
member of the config server replica set
Configuration Options
sharding:
clusterRole: configsvr
replication:
replSetName: csrs
Config Servers:
Server1 -
27019
Server2 -
27019
Server3 -
27019
Start all the
servers by using the command,
Server1
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod_config.conf
[tommy@Server1
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 50460
child
process started successfully, parent exiting
[tommy@Server1
~]$
Server2
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod_config.conf
[tommy@Server2
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 40616
child
process started successfully, parent exiting
Server3
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod_config.conf
[tommy@Server3
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 63212
child
process started successfully, parent exiting
Connect
to one of the config servers
Server1
mongo --port
27019
[tommy@Server1
~]$ mongo --port 27019
MongoDB
shell version: 3.0.4
connecting
to: 127.0.0.1:27019/test
>
Initiate and
add members to the Config Sever replica set
#
rs.initiate()
Server1
> rs.initiate()
{
"info2" : "no
configuration specified. Using a default configuration for the set",
"me" : "Server1.pharma.aventis.com:27019",
"ok" : 1,
"$gleStats" : {
"lastOpTime" :
Timestamp(1524662349, 1),
"electionId" :
ObjectId("000000000000000000000000")
}
}
csrs:SECONDARY>
csrs:PRIMARY>
# rs.add("Server2:27019")
csrs:PRIMARY>
rs.add("Server2:27019")
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" :
Timestamp(1524662384, 1),
"t" :
NumberLong(1)
},
"electionId" :
ObjectId("7fffffff0000000000000001")
}
}
# rs.add("Server3:27019")
csrs:PRIMARY>
rs.add("Server3:27019")
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" :
Timestamp(1524662426, 1),
"t" :
NumberLong(1)
},
"electionId" :
ObjectId("7fffffff0000000000000001")
}
}
csrs:PRIMARY>
To check
the Status of the Config Server Replica Set,
# rs.status()
csrs:PRIMARY>
rs.status()
{
"set" : "csrs",
"date" :
ISODate("2018-04-25T13:21:33.605Z"),
"myState" : 1,
"term" : NumberLong(1),
"configsvr" : true,
"heartbeatIntervalMillis" :
NumberLong(2000),
"optimes" : {
"lastCommittedOpTime"
: {
"ts" :
Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"readConcernMajorityOpTime"
: {
"ts" :
Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"appliedOpTime" : {
"ts" :
Timestamp(1524662492, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" :
Timestamp(1524662492, 1),
"t" :
NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" :
"Server1.pharma.aventis.com:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" :
634,
"optime" : {
"ts"
: Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:21:32Z"),
"electionTime" : Timestamp(1524662349, 2),
"electionDate" : ISODate("2018-04-25T13:19:09Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" :
"Server2:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" :
109,
"optime" : {
"ts"
: Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"optimeDurable" : {
"ts" :
Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:21:32Z"),
"optimeDurableDate" : ISODate("2018-04-25T13:21:32Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:21:32.912Z"),
"lastHeartbeatRecv" :
ISODate("2018-04-25T13:21:32.923Z"),
"pingMs" :
NumberLong(0),
"syncingTo" : "Server1.pharma.aventis.com:27019",
"configVersion" : 3
},
{
"_id" : 2,
"name" :
"Server3:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" :
66,
"optime" : {
"ts"
: Timestamp(1524662492, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts"
: Timestamp(1524662492, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:21:32Z"),
"optimeDurableDate" :
ISODate("2018-04-25T13:21:32Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:21:32.911Z"),
"lastHeartbeatRecv"
: ISODate("2018-04-25T13:21:33.277Z"),
"pingMs" :
NumberLong(0),
"syncingTo" :
"Server1.pharma.aventis.com:27019",
"configVersion" : 3
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" :
Timestamp(1524662426, 1),
"t" :
NumberLong(1)
},
"electionId" :
ObjectId("7fffffff0000000000000001")
}
}
csrs:PRIMARY>
2.
Create the Shard Replica Sets
Start
each member of the shard replica set.
Configuration Options
sharding:
clusterRole: shardsvr
replication:
replSetName: rs_0
Shard 1:
Server4 -
27019
Server5 -
27019
Server6 –
27020
Start all
the servers in Shard 1 by using the command,
Server4
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server4
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 23744
child
process started successfully, parent exiting
[tommy@Server4
~]$
Server5
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server5
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 54991
child
process started successfully, parent exiting
[tommy@Server5
~]$
Server6
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server6
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 30514
child
process started successfully, parent exiting
[tommy@Server6
~]$
Connect
to a member of the shard replica set.
Server4
# mongo –port
27019
[tommy@Server4
~]$ mongo --port 27019
MongoDB
shell version: 3.0.7
connecting
to: 127.0.0.1:27019/test
>
Initiate and
add members to the replica set
# rs.initiate()
Server4
>
rs.initiate()
{
"info2" : "no
configuration specified. Using a default configuration for the set",
"me" : "Server4.pharma.aventis.com:27019",
"ok" : 1
}
rs_0:SECONDARY>
rs_0:PRIMARY>
# rs.add("Server5:27019")
rs_0:PRIMARY>
rs.add("Server5:27019")
{
"ok" : 1 }
rs_0:PRIMARY>
# rs.add("Server6:27020")
rs_0:PRIMARY>
rs.add("Server6:27020")
{
"ok" : 1 }
rs_0:PRIMARY>
To check
the Status of the Config Server Replica Set,
# rs.status()
rs_0:PRIMARY>
rs.status()
{
"set" : "rs_0",
"date" :
ISODate("2018-04-25T13:35:33.372Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" :
NumberLong(2000),
"optimes" : {
"lastCommittedOpTime"
: {
"ts" :
Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"appliedOpTime" : {
"ts" :
Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"durableOpTime" : {
"ts" :
Timestamp(1524663328, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" :
"Server4.pharma.aventis.com:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" :
527,
"optime" : {
"ts"
: Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:35:28Z"),
"electionTime" : Timestamp(1524663186, 2),
"electionDate" : ISODate("2018-04-25T13:33:06Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" :
"Server5:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" :
101,
"optime" : {
"ts"
: Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"optimeDurable" : {
"ts"
: Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"optimeDate" :
ISODate("2018-04-25T13:35:28Z"),
"optimeDurableDate" :
ISODate("2018-04-25T13:35:28Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:35:32.943Z"),
"lastHeartbeatRecv" : ISODate("2018-04-25T13:35:31.929Z"),
"pingMs" :
NumberLong(0),
"syncingTo" :
"Server4.pharma.aventis.com:27019",
"configVersion" : 3
},
{
"_id" : 2,
"name" :
"Server6:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" :
64,
"optime" : {
"ts"
: Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"optimeDurable" : {
"ts"
: Timestamp(1524663328, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:35:28Z"),
"optimeDurableDate" :
ISODate("2018-04-25T13:35:28Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:35:32.943Z"),
"lastHeartbeatRecv" :
ISODate("2018-04-25T13:35:32.017Z"),
"pingMs" :
NumberLong(0),
"syncingTo" :
"Server4.pharma.aventis.com:27019",
"configVersion" : 3
}
],
"ok" : 1
}
rs_0:PRIMARY>
Shard 2:
Server7 -
27018
Server8 -
27018
Server9 –
27018
Configuration Options
sharding:
clusterRole: shardsvr
replication:
replSetName: rs_1
Start all
the servers in Shard 1 by using the command,
Server7
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server7
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 9758
child
process started successfully, parent exiting
[tommy@Server7
~]$
Server8
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server8
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 31778
child
process started successfully, parent exiting
[tommy@Server8
~]$
Server9
/data/sand/mongodb_3.4.10/bin/mongod
-f /data/sand/conf/mongod.conf
[tommy@Server9
~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 19566
child
process started successfully, parent exiting
[tommy@Server9
~]$
Connect
to a member of the shard replica set.
Server7
# mongo –port
27018
[tommy@Server7
~]$ mongo --port 27018
MongoDB
shell version: 3.0.7
connecting
to: 127.0.0.1:27018/test
Server has
startup warnings:
>
Initiate and
add members to the replica set
# rs.initiate()
Server7
>
rs.initiate()
{
"info2" : "no
configuration specified. Using a default configuration for the set",
"me" : "Server7.pharma.aventis.com:27018",
"ok" : 1
}
rs_1:SECONDARY>
rs_1:PRIMARY>
# rs.add("Server8:27018")
rs_1:PRIMARY>
rs.add("Server8:27018")
{ "ok"
: 1 }
rs_1:PRIMARY>
# rs.add("Server9:27018")
rs_1:PRIMARY>
rs.add("Server9:27018")
{
"ok" : 1 }
rs_1:PRIMARY>
To check
the Status of the Config Server Replica Set,
# rs.status()
rs_1:PRIMARY>
rs.status()
{
"set" : "rs_1",
"date" :
ISODate("2018-04-25T13:48:27.918Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" :
NumberLong(2000),
"optimes" : {
"lastCommittedOpTime"
: {
"ts" :
Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"appliedOpTime" : {
"ts" :
Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"durableOpTime" : {
"ts" :
Timestamp(1524664106, 1),
"t" :
NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "Server7.pharma.aventis.com:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" :
436,
"optime" : {
"ts" :
Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:48:26Z"),
"infoMessage"
: "could not find member to sync from",
"electionTime" : Timestamp(1524664035, 2),
"electionDate" : ISODate("2018-04-25T13:47:15Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" :
"Server8:27018",
"health" : 1,
"state" : 2,
"stateStr" :
"SECONDARY",
"uptime" : 50,
"optime" : {
"ts"
: Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"optimeDurable" : {
"ts"
: Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:48:26Z"),
"optimeDurableDate"
: ISODate("2018-04-25T13:48:26Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:48:27.693Z"),
"lastHeartbeatRecv" :
ISODate("2018-04-25T13:48:27.625Z"),
"pingMs" :
NumberLong(79),
"syncingTo" :
"Server7.pharma.aventis.com:27018",
"configVersion" : 3
},
{
"_id" : 2,
"name" :
"Server9:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" :
23,
"optime" : {
"ts"
: Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"optimeDurable" : {
"ts"
: Timestamp(1524664106, 1),
"t" :
NumberLong(1)
},
"optimeDate"
: ISODate("2018-04-25T13:48:26Z"),
"optimeDurableDate" :
ISODate("2018-04-25T13:48:26Z"),
"lastHeartbeat" :
ISODate("2018-04-25T13:48:26.558Z"),
"lastHeartbeatRecv" :
ISODate("2018-04-25T13:48:26.870Z"),
"pingMs" :
NumberLong(0),
"syncingTo" :
"Server7.pharma.aventis.com:27018",
"configVersion" : 3
}
],
"ok" : 1
}
rs_1:PRIMARY>
3.
Connect a mongos to
the Sharded Cluster
Connect a
mongos to the cluster
Configuration Options
sharding:
configDB: csrs/Server1:27019,Server2:27019,Server3:27019
Mongos:
Server10 -
27017
Start the
mongos server using the command,
Server10
# /home/tommy/mongodb_3.4.10/bin/mongos
-f /home/tommy/conf/mongos.conf
[tommy@Server10
~]$ /home/tommy/mongodb_3.4.10/bin/mongos -f /home/tommy/conf/mongos.conf
about to
fork child process, waiting until server is ready for connections.
forked
process: 20307
child
process started successfully, parent exiting
[tommy@Server10
~]$
Connect to
the mongos.
# mongo --port 27017
[tommy@Server10 ~]$ mongo --port 27017
MongoDB
shell version v3.4.10
connecting
to: mongodb://127.0.0.1:27017/
MongoDB
server version: 3.4.10
mongos>
4.
Add Shards to the Cluster
Use
the sh.addShard() method to add each shard to
the cluster
If the
shard is a replica set, specify the name of the replica set and specify a
member of the set.
# sh.addShard("rs_0/Server4:27019")
mongos>
sh.addShard("rs_0/Server4:27019")
{
"shardAdded" : "rs_0", "ok" : 1 }
mongos>
# sh.addShard("rs_1/Server7:27018")
mongos>
sh.addShard("rs_1/Server7:27018")
{
"shardAdded" : "rs_1", "ok" : 1 }
mongos>
To check the
status of the Sharded Cluser
# sh.status()
mongos>
sh.status()
---
Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5ae0804e37293cc8956e37ee")
}
shards:
{ "_id" :
"rs_0", "host" :
"rs_0/Server4.tommy.com:27019,Server5:27019,Server6:27020", "state" : 1 }
{ "_id" :
"rs_1", "host" :
"rs_1/Server9:27018,Server7.tommy.com:27018,Server8:27018", "state" : 1 }
active mongoses:
"3.4.10" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5
attempts: 0
Migration Results for the last 24
hours:
No recent migrations
databases:
mongos>
Done…
good work bro.. appreciate ur effort.. we look forward for new things u need to update us :)
ReplyDelete