Setting up a MongoDB cluster can be a little difficult for the first time.  A MongoDB cluster consists of three types of servers: Config Servers, Query Routers, and Shards.  In a production environment where high-availability is a must, you will need at least (3) config servers, (2) or more Shards, and (1) or more Query Routers.

 In the example below, we will show how to create a very small development cluster using only (3) servers.

Config Servers
Log into server as administrator and execute the following commands:
# adduser mongodbuser
# sudo usermod -a -G sudo mongodbuser

Logout and login as mongodbuser and execute the following commands:

# sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
# echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# sudo apt-get update
# apt-get install build-essential
# sudo apt-get install -y mongodb-org
# vi /etc/mongod.conf
bind_ip = 10.10.10.67 (internal IP)
# mkdir -p /data/db
# mkdir /data/configdb
# mongod –configsvr &

Router Servers (mongos)
Log into server as administrator and execute the following commands:
# adduser mongodbuser
# sudo usermod -a -G sudo mongodbuser

Logout and login as mongodbuser and execute the following commands:

# sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
# echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# sudo apt-get update
# apt-get install build-essential
# sudo apt-get install -y mongodb-org
# vi /etc/mongod.conf
bind_ip = 10.10.10.68 (internal IP)
# mkdir -p /data/db
# mkdir /data/configdb
# mongos –configdb 10.10.10.67:27019 –port 27020 &
# mongo 10.201.9.68:27020/admin (verify working)
mongos>show dbs
mongos> sh.status
mongos>sh.status()

Replica Servers
Log into server as administrator and execute the following commands:
# adduser mongodbuser
# sudo usermod -a -G sudo mongodbuser

Logout and login as mongodbuser and execute the following commands:

# sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
# echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# sudo apt-get update
# apt-get install build-essential
# sudo apt-get install -y mongodb-org
# vi /etc/mongod.conf
bind_ip = 10.10.10.69 (internal IP)
# mkdir -p /data/db
# chown inetsupport /data/db
# mongod –shardsvr &

Back to Router Servers:
Before adding shards, verify working status MongoDB again.

# mongo 10.10.10.68:27020/admin
mongos>show dbs
mongos> sh.status
mongos>sh.status()

Now add shards.

mongos> sh.addShard(“10.10.10.69:27018”)
{ “shardAdded” : “shard0000”, “ok” : 1 }

Verify shard status.

mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“555a06aba67f59d4389f4a78”)
}
shards:
{ “_id” : “shard0000”, “host” : “10.10.10.69:27018” }
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” : “admin”, “partitioned” : false, “primary” : “config” }

Ready to Shard Database – Configuration Complete

mongos> db.runCommand({enablesharding:”testDB”});
{ “ok” : 1 }

mongos> sh.shardCollection(“testDB.testData”,{“name”:1})
{ “collectionsharded” : “testDB.testData”, “ok” : 1 }

mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“version” : 3,
“minCompatibleVersion” : 3,
“currentVersion” : 4,
“clusterId” : ObjectId(“55561ab7fbb9f276a253616b”)
}
shards:
{ “_id” : “shard0000”, “host” : “10.10.10.67:27018” }
{ “_id” : “shard0001”, “host” : “10.10.10.68:27018” }
{ “_id” : “shard0002”, “host” : “10.10.10.69:27018” }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }
{ “_id” : “testDB”, “partitioned” : true, “primary” : “shard0000” }
testDB.testData
shard key: { “name” : 1 }
chunks:
shard0000 1
{ “name” : { “$minKey” : 1 } } –>> { “name” : { “$maxKey” : 1 } } on : shard0000 Timestamp(1, 0)

mongos> sh.getBalancerState()
true

Leave a Reply

Your email address will not be published. Required fields are marked *