Warning
This article is old and some of the information may be outdated or no longer valid.
If you have any questions or need to confirm any details, we recommend contacting our support team to receive up-to-date information and personalized assistance.
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/hostsAdd the following lines:
master-ip-address masternode
client1-ip-address client1
client2-ip-address client2Save 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 -yAfter 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 -yNext, install the MongoDB server package with the following command:
# apt install mongodb-org -yAfter installing the MongoDB, start the MongoDB service and enable it to start at system reboot:
# systemctl start mongod
# systemctl enable mongodCreate 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:
# mongoAfter connecting to the Mongo shell, switch the database to admin:
# use adminTo 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:
# exitConfigure 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/keyfileNext, 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.confAdd 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.confAdd 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/keyfileOn client2 node, edit the file mongod.conf:
# nano /etc/mongod.confAdd 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/keyfileNext, you'll need to restart the MongoDB service on all nodes one by one. You can do it with the following command:
# systemctl restart mongodInitiate 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 adminOnce 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 adminNext, 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 dbsThis 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 adminAfter 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.