Table of Contents

Overview

Like any other Database, mongo is also obliged to provide Active/Active load balancing configuration. Also like many other databases (except Oracle) Mongo doesn't require shared storage or ultra-complex configuration / prerequisites in order to provide this. In fact, so far, Mongo was the easiest Active / Active and Active / Passive Configuration which I ever configured.

Set up Mongo Instances

The set up of the Mongo Instances was already provided in the previous sections, just install the following packages on each server and disabled the selinux, and you should be good to go:

[root@lparamongo ~]# rpm -qa | grep mongo
mongodb-org-server-2.6.12-1.x86_64
mongodb-org-tools-2.6.12-1.x86_64
mongodb-org-2.6.12-1.x86_64
mongodb-org-mongos-2.6.12-1.x86_64
mongodb-org-shell-2.6.12-1.x86_64
[root@lparamongo ~]#

In our configuration we will have 4 servers in order to configure Mongo sharding:

So let's get started :)

Config Server Database

As everything with Mongo, things are simple, just run your database with the –configsvr option:

[root@lparamongo ~]# mongod -f /etc/mongod.conf -fork --configsvr
about to fork child process, waiting until server is ready for connections.
forked process: 18957
child process started successfully, parent exiting
[root@lparamongo ~]#

Next we have to configure the shard controller to connect to this database as follows:

[root@lparbmongo ~]# mongos --configdb lparamongo:9005 --port 9005 --chunkSize 1
2018-04-05T14:14:40.851+0200 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
2018-04-05T14:14:40.862+0200 [mongosMain] MongoS version 2.6.12 starting: pid=18617 port=9005 64-bit host=lparbmongo (--help for usage)
2018-04-05T14:14:40.862+0200 [mongosMain] db version v2.6.12
*****************************************************************************

With this, the shard controller is connected to our config database.

Start up the Shard servers

The shard servers has to be started as normal:

lparcmongo

[root@lparcmongo ~]# mongod -f /etc/mongod.conf -fork
about to fork child process, waiting until server is ready for connections.
forked process: 13778
child process started successfully, parent exiting
[root@lparcmongo ~]# mongo --port 9005
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:9005/test
Server has startup warnings:
2018-04-05T14:15:48.321+0200 [initandlisten]
2018-04-05T14:15:48.321+0200 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
2018-04-05T14:15:48.321+0200 [initandlisten] **          Restart with --replSet unless you are doing maintenance and no other clients are connected.
2018-04-05T14:15:48.321+0200 [initandlisten] **          The TTL collection monitor will not start because of this.
2018-04-05T14:15:48.321+0200 [initandlisten] **          For more info see http://dochub.mongodb.org/core/ttlcollections
2018-04-05T14:15:48.321+0200 [initandlisten]
>

lpardmongo

[root@lpardmongo ~]#  mongod -f /etc/mongod.conf -fork
about to fork child process, waiting until server is ready for connections.
forked process: 17962
child process started successfully, parent exiting

Once the servers are started we have to configure the shards to communicate via the proxy shard controller. In order to do that, just connect to the shard controller and add them in the configuration:

[root@lparbmongo ~]# mongo --port 9005
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:9005/test
mongos>
mongos> sh.addShard("lparcmongo:9005")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("lpardmongo:9005")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.printShardingStatus();
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("5ac61332fa1e510ac2fa7fe1")
}
  shards:
        {  "_id" : "shard0000",  "host" : "lparcmongo:9005" }
        {  "_id" : "shard0001",  "host" : "lpardmongo:9005" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

So far we have added sharding but didn't enable it, nor we have created some sharded collection. In order to enable sharding we have to enable it and we have to specify a shard key for a collection as follows:

mongos> sh.enableSharding("ShardDB") <- Enable Sharding for database: ShardDB
{ "ok" : 1 }
mongos> show dbs
admin   (empty)
config  0.016GB
mongos> sh.shardCollection("ShardDB.shardCollection",{shardke:1}) <- Create a collection in that database
{ "collectionsharded" : "ShardDB.shardCollection", "ok" : 1 }
mongos> show dbs
ShardDB  0.031GB <- New Sharded database with sharded collection inside:
admin    (empty)
config   0.016GB
mongos> use ShardDB
switched to db ShardDB
mongos> show collections
shardCollection <- Sharded collection, empty
system.indexes
mongos> db.shardCollection.find()
mongos>

Unlike other databases, where the software specify how a data should be distributed in Mongo, the DBA has a total control of how the data is to be spread among the nodes.