MongoDB Replication / Replica set – In MongoDB, a copy set is a gathering of mongod measures that keep up a similar informational index. Imitation sets are the reason for all creation arrangements as they give information excess and high accessibility.
instructions to design MongoDB Replica Set on Ubuntu 18.04/20.04 worker. MongoDB is a venture class NoSQL information base framework with auto-scaling, high accessibility and superior. In a NoSQL data set, information is put away in an archive structure utilizing MongoDB BSON design. SQL articulations can’t be utilized in MongoDB to embed or recover information.
Here you can get the bulk data insert python3 script for load testing.
https://www.techbeginner.in/2021/06/how-to-bulk-insert-in-mongodb-using.html
In this post, We will install and configure MongoDB Cluster on ubuntu 18.04/20.04 LTS
MongoDB replication required minimum 3 nodes in the cluster, In my case i have following nodes.
10.10.10.11 mongo-node-2 10.10.10.12 mongo-node-3 10.10.10.10 mongo-node-1 |
Step 1: Configure DNS with MongoDB Nodes
You need to map ip address with names to resolved it via DNS, In my case i am going to use hosts file to achieve local DNS pointing.
sudo vim /etc/hosts
# Add the following ip address and hostname according to your env.
10.10.10.10 mongo-node-1
10.10.10.11 mongo-node-2
10.10.10.12 mongo-node-3
Save and exit from the vim editor.
Step 2: Install MongoDB
Click here find the installation of MongoDB on different version of ubuntu, The following command work on ubuntu 20.04 LTS only.
Import the public key
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Add MongoDB Repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Install MongoDB
sudo apt-get update
sudo apt-get install mongodb-org -y
Verify the MongoDB Service
sudo systemctl start mongod && sudo systemctl status mongod
Note : Make sure you have installed mongoDB on all the 3 nodes and all the mongoDB nodes should be able to ping with defined DNS names in hosts file.
Step 3: Bind IP Address with MongoDB
You need to bind the IP address with MongoDB by using the following command.
sudo vim /etc/mongod.conf
Add your server IP address like this :-
net:
port: 27017
bindIp: localhost,10.10.10.10
Save and exit, You need to restart the mongod service by using following commands.
sudo systemctl restart mongod.service
Verify with netstat commands like this :-
netstat -plntu | grep 27017
You need to bind the IP Address all the MongoDB nodes.
Step 4: Set Up MongoDB Authentication
To authentication with all the MongoDB nodes, We need to generate key file by using the given command, Execute the commands on all the MongoDB nodes.
To Generate the key
Execute the following command.
cd /opt/mongodb/ && openssl rand -base64 756 > mongo-keyfile
Update the file permission and configure with mongoDB config file.
chmod 400 /opt/mongodb/mongo-keyfile && chown mongodb:mongodb /opt/mongodb/mongo-keyfile
Key Configuration with MongoDB
After getting key, We need to configure with MongoDB for authentication, Use the following command for the same. |
||||
Open the mongoDB config file with your text editor.
sudo vim /etc/mongod.conf
Add the following keyFile like this.
security:
keyFile: /opt/mongodb/mongo-keyfile
replication:
replSetName: rs0
In the end save and exit from the MongoDB config file and restart the MongoDB service by using the given command.
sudo systemctl restart mongod
Step 5: Create an Administrative User
You need to create root user to manage MongoDB database, To achieve this, You need to execute the following command one by one.
To log in Mongo Shell type Mongo on linux terminal.
mongo
After that you need to choose the admin database.
use admin
and then you need to execute the MongoDB query like this to create the root user with password.
db.runCommand({
"createUser" : "root",
"pwd" : "YOUR_PASSWORD",
"customData" : {
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
});
After that we need to apply authentication security by following to add lines in mongoDB config files.
sudo vim /etc/mongod.conf
and add or update the below parameter
security:
authorization: enabled
keyFile: /opt/mongodb/mongo-keyfile
setParameter:
enableLocalhostAuthBypass: false
Save and exit from the MongoDB config file, To get effect you need to restart your MongoDB service by using given commands.
|
|||||||||||||||||||||||||||||||||||||
Step 6:Enable MongoDB Replication
We are ready to enable MongoDB replication by using MongoDB main config file, Use the following command for the same.
Add or update the following parameter.
Save and exit and To get effect you need to restart your mongoDB service by using given commands.
Once MongoDB is restart, We need to login in MongoDB Shell and execute the following MongoDB query to initialization and Add MongoDB nodes.
Login MongoDB Shell
Enable MongoDB Replication
Adding MongoDB Nodes
Step 7:Testing MongoDB Replication
Now we need to test MongoDB replication, To do that, We need to Add some sample data in MongoDB Master node, To achieve this We are going to use the for loop, Execute the given command one by one to add data.
Execute on Primary MongoDB (Master Node)
Execute on Slave Nodes
Conclusion |
|
||||||||||||||||||||||