How to setup mongoDB cluster on ubuntu 18.04

LORY
3 min readAug 10, 2020

--

Install mongoDB

sudo apt-get install mongodb –y

If you mess up installation . try below commands to clean up existing folders

sudo apt-get purge mongo-tools*

sudo apt-get purge mongodb*

sudo apt purge mongod*

The setup of mongodb will be on all 3 Ubuntu virtual machine

192.168.11.137 (router, c0, rs0,rs1)

192.168.11.138 (c0,rs0,rs1)

192.168.11.139 (c0,rs1,rs1)

Components

C0: configure replica set

rs0 and rs1 : shard replica set

For demo purpose ,we launch everything in command line mode .deployment can be done with configuration file

c0- run config service on each machine

192.168.11.137

sudo mongod — configsvr — replSet c0 — dbpath data\configdb — bind_ip_all — port 27018

192.168.11.138

sudo mongod — configsvr — replSet c0 — dbpath data\configdb — bind_ip_all — port 27018

192.168.11.139

sudo mongod — configsvr — replSet c0 — dbpath data\configdb — bind_ip_all — port 27018

now ,connect to one of the master node

mongo — host 192.168.11.137 — port 27018

group them into replica set

rs.initiate(

{

_id: “c0”,

configsvr: true,

members: [

{ _id : 0, host : “192.168.11.137:27018” },

{ _id : 1, host : “192.168.11.138:27018” },

{ _id : 2, host : “192.168.11.139:27018” }

]

}

)

Next , data shard

We will create 2 shard on each machine

192.168.11.137

mongod — shardsvr — replSet rs0 — dbpath data\db — bind_ip_all — port 27019

mongod — shardsvr — replSet rs1 — dbpath data\db1 — bind_ip_all — port 27020

192.168.11.138

mongod — shardsvr — replSet rs0 — dbpath data\db — bind_ip_all — port 27019

mongod — shardsvr — replSet rs1 — dbpath data\db1 — bind_ip_all — port 27020

192.168.11.139

mongod — shardsvr — replSet rs0 — dbpath data\db — bind_ip_all — port 27019

mongod — shardsvr — replSet rs1 — dbpath data\db1 — bind_ip_all — port 27020

login one of the machine and group them into replicate set (like how we did for configure service), taking care of the port

mongo — host 192.168.11.137 — port 27019

rs.initiate(

{

_id : ‘rs0’,

members: [

{ _id : 0, host : “192.168.11.137:27019” },

{ _id : 1, host : “192.168.11.138:27019” },

{ _id : 2, host : “192.168.11.139:27019” }

]

}

)

mongo — host 192.168.11.137 — port 27020

rs.initiate(

{

_id : ‘rs1’,

members: [

{ _id : 0, host : “192.168.11.137:27020” },

{ _id : 1, host : “192.168.11.138:27020” },

{ _id : 2, host : “192.168.11.139:27020” }

]

}

)

Next ,connect routing to configure service(port :27019)

important : add shard after ‘use admin’

mongos — configdb c0/192.168.11.137:27018,192.168.11.138:27018,192.168.11.139:27018 — port 37017

use admin

sh.addShard(‘rs0/192.168.11.137:27019’)

sh.addShard(‘rs0/192.168.11.138:27019’)

sh.addShard(‘rs0/192.168.11.139:27019’)

sh.addShard(‘rs1/192.168.11.137:27020’)

sh.addShard(‘rs1/192.168.11.138:27020’)

sh.addShard(‘rs1/192.168.11.139:27020’)

check status

db.runCommand({listshards:1})

Almost done . Now enable sharding on collection

Create sample database

sh.enableSharding(‘persondb’)

sh.shardCollection(“persondb.person”,{_id:”hashed”})

use persondb

for(var i=0;i<10;i++){db.person.insert({name:”aaa”+i});}

if the id(key) is range based :

sh.shardCollection(“persondb.id”,{id:1},true)

Finally , use stats command to verify result

db.person.stats()

Other Balancing commands

sh.enableBalancing(“persondb.person”)

sh.stopBalancer()

sh.getBalancerState()

sh.startBalancer()

--

--

LORY

A channel which focusing on developer growth and self improvement