MongoDB Replication: A complete installation guide(with scripts)

Anant Kumar
3 min readJul 5, 2022

--

A replica set in MongoDB is a group of MongoDB processes that maintain the same data set. Replica sets provide redundancy and high availability and are the basis for all production deployments.

A replica set contains several data-bearing nodes and optionally one arbiter node. Of the data-bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes. The primary node receives all write operations. Replica sets use elections to determine which set member will become primary. In the case of even nodes, the arbiter node is used to break a tie during elections that do not replicate data or accept write operations.

Source: www.mongodb.com

We are going with 3 node replica set for our replica set setup.

Update: I have written scripts for the same which allow installation of mongoDB replica set under 2 minutes. If you want to skip step by step installation, you can jump to the below link:

https://medium.com/@anant9524/mongodb-replication-under-2-minutes-12fd1f327960

Install all dependencies on all nodes:

apt-get install dirmngr gnupg apt-transport-https ca-certificates software-properties-common -y

Install MongoDB on all nodes:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl enable mongod
sudo systemctl start mongod

Create an Admin User on Primary Node

On the primary node, connect to the MongoDB:

mongo

Once connected, change the database to admin:

use admin

Next, create a user named admin with root privileges(change password in below command):

db.createUser({user: "admin", pwd: "password", roles:[{role: "root", db: "admin"}]})

Next, exit from the MongoDB:

exit

Create mongo-keyfile to enhance mongo security

Generate a key file for MongoDB on the primary node:

openssl rand -base64 756 > /opt/mongo-keyfile

On second and third nodes:

touch /opt/mongo-keyfile

Next, copy the contents of /opt/mongo-keyfile file from primary node and paste it to /opt/mongo-keyfile file on second and third node.

On all nodes, run below commands to set proper ownership and permission to the mongo-key file:

chmod 400 /opt/mongo-keyfile
chown mongodb:mongodb /opt/mongo-keyfile

Configure replica set nodes

Edit the MongoDB main configuration file on all nodes:

nano /etc/mongod.conf

net:
port: 27017
bindIp: 127.0.0.1,node-ip-address/DNS address
security:
keyFile: /opt/mongo-keyfile
replication:
replSetName: "replica01"

Notes:

  1. Add IP address in case of on-prem server/DNS address in case of AWS server in bindIP.
  2. Change port as per the availability. 27017 is default port used for mongoDB.
  3. Choose replSetName as per your choice.

Restart MongoDB Service on All Nodes:

systemctl restart mongod

Start Replication and Add Members

connect to the Mongo with the admin user:

mongo -u admin -p --authenticationDatabase admin

Initiate the replica set:

rs.initiate()

add second and third nodes as a member:

rs.add("node-ip-address/DNS address")

Check the status of all nodes:

rs.status()

Replica set is ready to serve mongoDB. You can connect to it either by URI or MongoDB compass.

MongoDB Basic Commands

Create user:

db.createUser(
{
user: "test",
pwd: "test123",
roles: [ { role: "readWrite", db: "test" } ]
}
)

Update user:

db.updateUser( "test",
{
roles : ["clusterMonitor"]
}
)

Delete user:

db.dropUser("test")

Create Collection in a database:

db.createCollection("test_collection")

Comment if you have any queries. Stay tuned for more updates.

--

--