MongoDB is an open-source and document-oriented NoSQL database system used for storing and retrieving large volumes of data. It was developed by MongoDB Inc and uses JSON-like documents with optional schemas instead of using tables and rows. MongoDB uses a replica set for redundancy and high availability. You can use a replica set to distribute the databases across many nodes. Replica set uses one node as a master node, responsible for writing operations while others are client nodes that perform reading operations.
In this tutorial, we'll guide you on how to configure three-node MongoDB cluster on Ubuntu 20.04 VPS.
Prerequisites
- Three Ubuntu 20.04 cloud servers to set up the MongoDB cluster.
Set Up Hostfile
First, you'll need to set up the hosts file on each node to perform hostname resolution between each node.
Edit the /etc/hosts file on each node:
# nano /etc/hosts
Add the following lines:
master-ip-address masternode
client1-ip-address client1
client2-ip-address client2
Save and close the file when you are finished.
Installing MongoDB
Next, you'll need to install the MongoDB server package on all nodes.
First, install the required dependencies using the following command:
# apt install apt-transport-https software-properties-common gnupg2 dirmngr -y
After installing all the dependencies, add the MongoDB repository with the following command:
# wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'
Once the repository is added, update the repository with the following command:
# apt update -y
Next, install the MongoDB server package with the following command:
# apt install mongodb-org -y
After installing the MongoDB, start the MongoDB service and enable it to start at system reboot:
# systemctl start mongod
# systemctl enable mongod
Create Administrative User on Master Node
Next, you'll need to create an administrative user to authenticate MongoDB on the Master node.
First, connect to MongoDB using the following command:
# mongo
After connecting to the Mongo shell, switch the database to admin:
# use admin
To go on, create an administrative user with name admin and set a password for it:
# db.createUser({user: "admin", pwd: "adminpassword", roles:[{role: "root", db: "admin"}]})
This is the output that you should get:
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
To finish with, exit from the MongoDB shell with the following command:
# exit
Configure Master Node
Now you'll need to create a primary key on the Master node and then, copy it to the remaining nodes.
First, create a primary key and set proper permission on the Master node:
# openssl rand -base64 756 > /mnt/keyfile
# chmod 400 /mnt/keyfile
# chown mongodb:mongodb /mnt/keyfile
Next, copy the keyfile to the client nodes:
# scp /mnt/keyfile root@client1:/mnt/
# scp /mnt/keyfile root@client2:/mnt/
To go on, edit the MongoDB main configuration file and define replica set, keyfile and IP address:
# nano /etc/mongod.conf
Add or modify the following lines:
net:
port: 27017
bindIp: 127.0.0.1,master-ip-address
security:
keyFile: /mnt/keyfile
replication:
replSetName: "replica1"
Save and close the file when you are finished.
Configure the Remaining Nodes
Next, you'll need to edit the MongoDB main configuration file on client1 and client2 nodes.
On client1 node, edit the file mongod.conf:
# nano /etc/mongod.conf
Add or modify the following lines:
net:
port: 27017
bindIp: 127.0.0.1,client1-ip-address
security:
keyFile: /mnt/keyfile
replication:
replSetName: "replica1"
Save the file and then set proper permission to the keyfile.
# chmod 400 /mnt/keyfile
# chown mongodb:mongodb /mnt/keyfile
On client2 node, edit the file mongod.conf:
# nano /etc/mongod.conf
Add or modify the following lines:
net:
port: 27017
bindIp: 127.0.0.1,client2-ip-address
security:
keyFile: /mnt/keyfile
replication:
replSetName: "replica1"
Save the file and then set proper permission to the keyfile:
# chmod 400 /mnt/keyfile
# chown mongodb:mongodb /mnt/keyfile
Next, you'll need to restart the MongoDB service on all nodes one by one. You can do it with the following command:
# systemctl restart mongod
Initiate Replica Set
Now you'll need to initiate the replica set on the Master node.
First, connect to MongoDB with the following command:
# mongo -u admin -p --authenticationDatabase admin
Once you are logged in, run the following command to initiate the replica set:
> rs.initiate()
This is the output that you should get:
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "master-address:27017",
"ok" : 1
}
For the next step, exit from the MongoDB shell, wait some time and then log in again with the following command:
# mongo -u admin -p --authenticationDatabase admin
Next, add the client1 node as a member with the following command:
replica1:PRIMARY> rs.add("client1")
This is the output that you should see:
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1625489476, 1),
"signature" : {
"hash" : BinData(0,"Igm6kb044jADRUx+qaU0q5iKZX4="),
"keyId" : NumberLong("6981423727095316484")
}
},
"operationTime" : Timestamp(1625489476, 1)
}
Next, add the client2 node as a member with the following command:
replica1:PRIMARY> rs.add("client2")
Now, check the status of all nodes with the following command:
> rs.status()
Verify Replication
To go on, you'll need to verify whether replication between all nodes is working or not.
In order to do so, create a database and add some values on the Master node:
# mongo -u admin -p --authenticationDatabase admin
replica1:PRIMARY> use mydb
replica1:PRIMARY> for (var i = 0; i <= 10; i++) db.myCollection.insert( { x : i } )
Next, run the following command to verify your database:
replica1:PRIMARY> show dbs
This is the output that you should get:
admin 0.000GB
config 0.000GB
mydb 0.000GB
local 0.000GB
Next, go to the client1 node and log in to MongoDB with the following command:
# mongo -u admin -p --authenticationDatabase admin
After login, enable the secondary member read operations on a per-connection basis using the following command:
replica1:SECONDARY> db.getMongo().setSecondaryOk()
To go on, switch the database to mydb and check all documents with the following command:
replica1:SECONDARY> use mydb
replica1:SECONDARY> db.myCollection.find()
You should see all documents created on the Master node in the following output:
{ "_id" : ObjectId("60e300746d386a637d52b9f1"), "x" : 0 }
{ "_id" : ObjectId("60e300746d386a637d52b9f2"), "x" : 1 }
{ "_id" : ObjectId("60e300746d386a637d52b9fa"), "x" : 9 }
{ "_id" : ObjectId("60e300746d386a637d52b9f7"), "x" : 6 }
{ "_id" : ObjectId("60e300746d386a637d52b9fb"), "x" : 10 }
{ "_id" : ObjectId("60e300746d386a637d52b9f8"), "x" : 7 }
{ "_id" : ObjectId("60e300746d386a637d52b9f3"), "x" : 2 }
{ "_id" : ObjectId("60e300746d386a637d52b9f9"), "x" : 8 }
{ "_id" : ObjectId("60e300746d386a637d52b9f6"), "x" : 5 }
{ "_id" : ObjectId("60e300746d386a637d52b9f4"), "x" : 3 }
{ "_id" : ObjectId("60e300746d386a637d52b9f5"), "x" : 4 }
Conclusion
We hope you have now enough understanding of the MongoDB cluster.
You can also add more clients to the cluster as per your requirements.