MongoDB Cluster with 3 Linux Instances
Setup MongoDB Cluster
Prerequisite
Require two or more servers to set cluster
Ubuntu 22.04
Follow the below steps to set up a 3-node MongoDB cluster:
Configuration and installation
Update && Upgrade the system
Before processing, update and upgrade all packages.
Check your current hostname by running the following command:
Set the Hostname in each server
To change the hostname, you can use the hostnamectl
command. For example, to set the hostname to "hostname", you would run.
On Linux and Unix-like systems, the hosts
file is located in the /etc/
directory. To edit this file on each of your three servers, use your preferred text editor.
Server Hostname Configuration
Configuring your servers with descriptive hostnames is advisable as it enhances clarity and ease of management.
mongo0.replset.member
mongo1.replset.member
mongo2.replset.member
Using these hostnames, your /etc/hosts
files would look similar to the following highlighted lines:
To add the MongoDB GPG key
Import the MongoDB GPG Key
First, import the MongoDB GPG key to verify the authenticity of the software packages:
To get the latest version of the software, add MongoDB’s dedicated package repository to your APT sources. Then, install the meta-package that always points to the newest MongoDB version.
First, import the public GPG key for the latest stable version of MongoDB using the following command:
Add MongoDB Repository
Next, add the MongoDB repository to your system's list:
APT (Advanced Package Tool) uses two main locations for finding online package repositories: the sources.list
file and the sources.list.d
directory. The sources.list
file lists repository sources line-by-line, placing the most preferred at the top. The sources.list.d
directory allows for modular and organized management by maintaining additional repository sources in separate files.
Execute the following command to create a file named mongodb-org-4.4.list
in the sources.list.d
directory:
After executing this command, refresh your server’s local package index to ensure APT can locate the MongoDB-org package:
Install MongoDB
Now, you can install MongoDB using the following command:
This command will install the MongoDB package and its dependencies.
Start MongoDB Service
After installing MongoDB, it should start automatically. To check the service status, run:
Enable MongoDB to Start on the Boot
Verify Installation
To verify that MongoDB is installed successfully, open the MongoDB shell by following these steps:
Enable Replication in Each Server's
MongoDB Configuration File:-
You have successfully configured the /etc/hosts file on your servers to map hostnames to their respective IP addresses.
To change MongoDB settings, edit the primary configuration file found at /etc/mongod.conf. This file is created automatically during installation.
Use a text editor vi to open and modify the file. Here’s the command:
In MongoDB, various configuration options allow you to customize the server's behavior. The system log option manages the database’s logging settings, enabling you to specify what gets logged and where. Conversely, the net option is essential for configuring network-related settings, providing control over parameters such as port configuration and network interfaces.
Update the IP address in each server from 127.0.0.0 to 0.0.0.0.
Create a mongo key file in each server
Now create an OpenSSL and run these commands
Copy these mongo-key files in another 2 vms
$ sftp root@your_IP_address
Enabling Replication in Each Server's MongoDB Configuration File
After copying the necessary files, proceed by adding the specified keys to the /etc/mongod.conf
file on each server.
Uncomment the security option and add the key
Add the key
keyFile: /etc/mongodb/key-files/mongo-key
Configure the file permissions and ownership in each server
Before executing the following command, ensure the Mongod service is stopped.
Replication runs these commands in all nodes for set replace
Also, add replication of all nodes
Starting the Replica Set and Adding Members:
After setting up your three MongoDB instances, initiate replication by opening a MongoDB shell and adding each instance as a replication member.
To begin the replication process, execute these commands:
Conclusion
MongoDB is a free, open-source NoSQL database management system (DBMS) popular for large-scale websites or applications. Since it doesn’t use a fixed schematic structure to store data, it is more flexible and scalable than SQL.
Last updated