In previous post we saw, to create a replicaset like standby in standalone server.
Basically with a replicaset the read/writes can be achieved via primary node and secondary nodes are inaccessible, this creates a concurrency issues with single node cluster. So we create a shard i.e data partitioning across nodes. The data will be distributed to shards , shard 1 with replicaset and to shard2 with replicaset.
To do this, we need
- The existing replicaset should be modified to enable sharding
- Create a mongoc instance , to store the metadata for data distribution for shard keys
- Create a mongos instance, to route the database requests to right shard
The steps broadly will be,
- Stop all the mongod instances
- Change the configuration file to add shardsvr = true
- Create config and mongos nodes
- Start the mongod instances in replicaset rs0
- Add the replicatset rs0 to a shard
###Stop all the mongod instances
mongod -f /backups/data/cluster/shard1/rs0.conf --shutdown
mongod -f /backups/data/cluster/shard1/rs1.conf --shutdown
mongod -f /backups/data/cluster/shard1/rs2.conf --shutdown
###Change the configuration file
vi /backups/data/cluster/shard1/rs0.conf
vi /backups/data/cluster/shard1/rs1.conf
vi /backups/data/cluster/shard1/rs2.conf
Append this line
sharding:
clusterRole: shardsvr
### Create Mongo config node by creating directories and configuration file
mkdir -p /backups/data/cluster/mongoc/logs
mkdir -p /backups/data/cluster/mongoc/data
vi /backups/data/cluster/mongoc/mongoc.conf
systemLog:
destination: file
path: "/backups/data/cluster/mongoc/logs/mongoc.log"
logAppend: true
processManagement:
pidFilePath: "/backups/data/cluster/mongoc/mongoc.pid"
fork: true
net:
bindIp: 127.0.0.1
port: 37019
storage:
engine: "wiredtiger"
dbPath: "/backups/data/cluster/mongoc/data"
directoryPerDB: true
sharding:
clusterRole: configsvr
operationProfiling:
mode: "all"
replication:
oplogSizeMB: 5120
replSetName: "crs0"
Note: the Port and Cluster role is configsvr and the directory path
### Create mongos instances configuration
mkdir -p /backups/data/cluster/mongos/logs
mkdir -p /backups/data/cluster/mongos/data
vi /backups/data/cluster/mongos/mongos.conf
systemLog:
destination: file
path: "/backups/data/cluster/mongos/logs/mongos.log"
logAppend: true
processManagement:
pidFilePath: "/backups/data/cluster/mongos/mongos.pid"
fork: true
net:
bindIp: 127.0.0.1
port: 27017
sharding:
configDB: "crs0/localhost:37019"
autoSplit: true
Note: For mongos instance it is wise to use 27017 a default port and look at configDB points to Mongo Config Server (port)
###Start the servers
###Firstly Start the mongod instances
mongod -f /backups/data/cluster/shard1/rs0.conf
mongod -f /backups/data/cluster/shard1/rs1.conf
mongod -f /backups/data/cluster/shard1/rs2.conf
###Start the mongod config server instance, from 3.4 onwards the config server must have replicaset enabled, hence we are adding replicaset with one node for development purpose.
mongod -f /backups/data/cluster/mongoc/mongoc.conf
mongo localhost:37019
rs.initiate({_id:"crs0", members: [{"_id":1, "host":"localhost:37019"}
###Start the mongos
mongos -f /backups/data/cluster/mongos/mongos.conf
### Finally , Add the replica set to shard
###Connect to mongos, where our mongos is running on port 27017
mongo localhost:27017
sh.addShard("rs0/localhost:47018,localhost:47019,localhost:47020");
###Enable the sharding for foo database
mongo localhost:27017
sh.enableSharding("mydb")
###Check the shard status
mongo localhost:27017
sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5834e646a2c25f3b06d65226")
}
shards:
{ "_id" : "rs0", "host" : "rs0/localhost:47018,localhost:47019,localhost:47020" }
active mongoses:
"3.2.1" : 1
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" : "foo", "primary" : "rs0", "partitioned" : true }
The above shows that the replicaset rs0 is a shard and this cluster has only single shard with database foo sharding is enabled.
Next Post , Adding a New Shard in standalone server
-Thanks
GeekDBA
Follow Me!!!