
Configure Replicaset1 with mongod Process on Port 27011 as Primary and Add mongod Processes on 27012 and 27013 as Secondary
1. Start mongo process on Port 27011 [[email protected] tom]# mongo --port 27011
MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:27011/test
>
2. Initiate Replicaset
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "localhost.localdomain:27011",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
3. Add Members to the Replicaset ReplicaSetNew1:PRIMARY> rs.add("localhost.localdomain:27012")
{ "ok" : 1 }
ReplicaSetNew1:PRIMARY> rs.add("localhost.localdomain:27013")
{ "ok" : 1 }
4. Check Status of the Replicaset ReplicaSetNew1:PRIMARY> rs.status()
{
"set" : "ReplicaSetNew1",
"date" : ISODate("2014-07-09T23:07:22Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27011",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 754,
"optime" : Timestamp(1404947221, 1),
"optimeDate" : ISODate("2014-07-09T23:07:01Z"),
"electionTime" : Timestamp(1404947128, 2),
"electionDate" : ISODate("2014-07-09T23:05:28Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost.localdomain:27012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 37,
"optime" : Timestamp(1404947221, 1),
"optimeDate" : ISODate("2014-07-09T23:07:01Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:07:21Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:07:22Z"),
"pingMs" : 0,
"syncingTo" : "localhost.localdomain:27011"
},
{
"_id" : 2,
"name" : "localhost.localdomain:27013",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 21,
"optime" : Timestamp(1404947221, 1),
"optimeDate" : ISODate("2014-07-09T23:07:01Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:07:21Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:07:21Z"),
"pingMs" : 0,
"syncingTo" : "localhost.localdomain:27011"
}
],
"ok" : 1
}
5. Exit
ReplicaSetNew1:PRIMARY> exit
bye
Configure Replicaset2 with mongod Process on Port 27021 as Primary and Add mongod Processes on 27022 and 27023 as Secondary
6. Start mongo process on Port 27021 [[email protected] tom]# mongo --port 27021
MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:27021/test
>
7. Initiate Replicaset
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "localhost.localdomain:27021",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
8. ReplicaSetNew2:PRIMARY>
Add Members to the Replicaset
ReplicaSetNew2:PRIMARY> rs.add("localhost.localdomain:27022")
{ "ok" : 1 }
ReplicaSetNew2:PRIMARY> rs.add("localhost.localdomain:27023")
{ "ok" : 1 }
9. Check Status of the Replicaset ReplicaSetNew2:PRIMARY> rs.status()
{
"set" : "ReplicaSetNew2",
"date" : ISODate("2014-07-09T23:08:09Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27021",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 737,
"optime" : Timestamp(1404947282, 1),
"optimeDate" : ISODate("2014-07-09T23:08:02Z"),
"electionTime" : Timestamp(1404947261, 1),
"electionDate" : ISODate("2014-07-09T23:07:41Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost.localdomain:27022",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 12,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:08:07Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:08:08Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "initial sync need a member to be primary or secondary to do
our initial sync"
},
{
"_id" : 2,
"name" : "localhost.localdomain:27023",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 7,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:08:08Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:08:07Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "initial sync need a member to be primary or secondary to do
our initial sync"
}
],
"ok" : 1
}
10. exit
ReplicaSetNew2:PRIMARY> exit
bye
Configure Replicaset3 with mongod Process on Port 27031 as Primary and Add mongod Processes on 27032 and 27033 as Secondary
11. Start mongo process on Port 27031 [[email protected] tom]# mongo --port 27031
MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:27031/test
>
12. Initiate Replicaset
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "localhost.localdomain:27031",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
13. Add Members to the Replicaset
ReplicaSetNew3:PRIMARY> rs.add("localhost.localdomain:27032")
{ "ok" : 1 }
ReplicaSetNew3:PRIMARY> rs.add("localhost.localdomain:27033")
{ "ok" : 1 }
14. Check Status of the Replicaset ReplicaSetNew3:PRIMARY> rs.status()
{
"set" : "ReplicaSetNew3",
"date" : ISODate("2014-07-09T23:08:55Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "localhost.localdomain:27031",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 731,
"optime" : Timestamp(1404947316, 1),
"optimeDate" : ISODate("2014-07-09T23:08:36Z"),
"electionTime" : Timestamp(1404947305, 2),
"electionDate" : ISODate("2014-07-09T23:08:25Z"),
"self" : true
},
{
"_id" : 1,
"name" : "localhost.localdomain:27032",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : Timestamp(1404947316, 1),
"optimeDate" : ISODate("2014-07-09T23:08:36Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:08:54Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:08:55Z"),
"pingMs" : 0,
"syncingTo" : "localhost.localdomain:27031"
},
{
"_id" : 2,
"name" : "localhost.localdomain:27033",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 19,
"optime" : Timestamp(1404947316, 1),
"optimeDate" : ISODate("2014-07-09T23:08:36Z"),
"lastHeartbeat" : ISODate("2014-07-09T23:08:54Z"),
"lastHeartbeatRecv" : ISODate("2014-07-09T23:08:55Z"),
"pingMs" : 0,
"syncingTo" : "localhost.localdomain:27031"
}
],
"ok" : 1
}
15. Exit
ReplicaSetNew3:PRIMARY> exit
bye
Start mongos Process
[[email protected] tom]# mongos --configdb localhost.localdomain:27018 --logpath /var/lib/mongos.log --fork --logappend --chunkSize 10
2014-07-10T01:11:31.886+0200 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 3415
child process started successfully, parent exiting
[[email protected] tom]# mongos --configdb localhost.localdomain:27019 --logpath /var/lib/mongos.log --fork --logappend --chunkSize 10
2014-07-10T01:11:46.458+0200 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 3450
child process started successfully, parent exiting
[[email protected] tom]# mongos --configdb localhost.localdomain:27020 --logpath /var/lib/mongos.log --fork --logappend --chunkSize 10
2014-07-10T01:11:57.112+0200 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 3485
child process started successfully, parent exiting
What are the Benefits of Sharding?
Sharding allows you to scale your database to handle increased loads to a nearly unlimited degree. It does this by increasing read/write throughput, storage capacity, and high availability. Let’s look at each of those in a little more detail:
- Increased read/write throughput: By distributing the data set across multiple shards, both read and write operation capacity are increased as long as those operations are confined to a single shard. This depends heavily on how you configure the shard key for each sharded collection.
- Increased storage capacity: Similarly, by increasing the number of shards, you can also increase overall total storage capacity. This allows near-infinite scalability.
- High availability: Since each shard is a replica set, every piece of data is replicated. Since the data is distributed, even if an entire shard becomes unavailable, the database as a whole still remains partially functional for reads and writes from the remaining shards.
Also See:
- MongoDB Sharding-01 -Directory Creations
- MongoDB Sharding-02–Setup Config Servers and Shard mongod Processes
- MongoDB Sharding-03–Configure Replicasets and start mongos process
- MongoDB Sharding-04-Setting up a Sharded Cluster
- MongoDB Sharding-05-Enable Sharding For a Collection
- MongoDB Sharding-06-Enable Sharding For a Collection
- Sharding in MongoDB