Logo Png Mongodb

TLDR;
Instruction to setup MongoDB 4.x cluster in Ubuntu 16.04 which is highly available.

Architecture

Mongos/query router: These serve requests between the application and the shards, where the data is distributed.
Mongo Config servers: These servers have metadata and configuration of other nodes. In this guide, we’ll use one config server for simplicity but in production environments, this should be a replica set of at least three Linodes.
Shard servers: A shard is a DB server that holds a part of the collection or data. This is where our actual Database data is going to live.

Step 1: Prerequisites

  • 3 servers – say config-01, config-02 & config-03
  • 2 servers – say mongos-01 & mongos-02
  • 3 servers – say shard1-01, shard1-02 & shard1-03
  • OS: Ubuntu 16.04
  • CPU: minimum of 1 CPU server
  • Memory: No restriction
  • Network: All the servers should be able to talk to each other. The same network is preferred.

Step 2: Setup Apt repo

Add MongoDB public GPG key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Add the repository details in apt config:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Update the apt cache:

sudo apt-get update

Step 3: Mongo Installation

Install the Mongo package:

sudo apt-get install mongodb-org

Update the following configs with the sample content:

Mongos server: /etc/mongos.conf

---
net:
  port: 27017
  bindIp: 0.0.0.0
systemLog:
  path: "/var/log/mongodb/mongos.log"
  logAppend: true
  destination: file
sharding:
  configDB: conf-rep-1/config-01:27019,config-02:27019,config-03:27019
security:
  keyFile: "/srv/mongodb/mongodb-keyfile"
Note "configDB", which is a list of config servers. Here 'conf-rep-1' is the name of Config server replicaset.

Mongo Config server: /etc/mongod.conf

---
systemLog:
  path: "/var/log/mongodb/mongod.log"
  logAppend: true
  destination: file
net:
  port: 27019
  bindIp: 0.0.0.0
security:
  keyFile: "/srv/mongodb/mongodb-keyfile"
  authorization: disabled
storage:
  dbPath: "/var/lib/mongodb"
  journal:
    enabled: true
replication:
  replSetName: conf-rep-1
sharding:
  clusterRole: configsvr
Note the "replSetName". This is the name of the Config server's replica set. This is important if we have multiple Config server replicasets.

Mongo Shard server: /etc/mongod.conf

---
systemLog:
  path: "/mongo-log/mongodb/mongod.log"
  logAppend: true
  destination: file
net:
  port: 27017
  bindIp: 0.0.0.0
security:
  keyFile: "/srv/mongodb/mongodb-keyfile"
  authorization: disabled
storage:
  dbPath: "/mongo-data/mongo"
  journal:
    enabled: true
replication:
  replSetName: rep-1
sharding:
  clusterRole: shardsvr

Create the keyfile: /srv/mongodb/mongodb-keyfile

openssl rand -base64 756 > /srv/mongodb/mongodb-keyfile

sudo chmod 400 /srv/mongodb/mongodb-keyfile

sudo chown mongodb:mongodb /srv/mongodb/mongodb-keyfile

Restart Mongo service:

sudo systemctl restart mongod #only in Config and Shard server

sudo systemctl restart mongos #only in Mongos server

Step 4: Configure Config server

Get into Mongo shell of one of the config server:

mongo --port 27019

Initiate the replica-set for config server:

rs.initiate( { _id: "conf-rep-1", configsvr: true, members: [ { _id: 0, host: "config-01:27019" }, { _id: 1, host: "config-02:27019" }, { _id: 2, host: "config-03:27019" } ] } );

rs.status();

Create admin user in the PRIMARY server:

use admin;

db.createUser({user: "admin", pwd: "password", roles:[{role: "root", db: "admin"}]})
Please remember to use stronger password above.

Step 5: Configure Mongos server

  1. Make sure the configs server in “/etc/mongos.conf” are the same IP addresses or hostnames of Mongo config servers
  2. Once Mongo is up, you should be able to access it using the same username and password that was created in Config server(step 4):
mongo -u admin -p --host hostname -I

Step 6: Configure Mongo Shard server

Get the “replSetName” from “/etc/mongod.conf”. We need this for initiating the replica-set. Login to Mongo shell and initiate the replica-set:

mongo 

rs.initiate( { _id: "rep-1", members: [ { _id: 0, host: "shard1-01:27017" }, { _id: 1, host: "shard1-02:27017" }, { _id: 2, host: "shard1-03:27017" } ] } );

rs.status();

Add to this replicaset as Shards to the cluster by logging into one of the Mongos server:

mongo --host -u admin -p --authenticationDatabase admin

sh.addShard( "rep-1/shard1-01:27017,shard1-02:27017,shard1-03:27017" );

db.adminCommand({ listShards: 1 });

You are now done with the setup of Highly Available Cluster of Mongodb.

Leave a Reply

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