MongoDB Cluster with 3 Linux Instances
Follow the below steps to set up a 3-node MongoDB cluster:
Update && Upgrade all nodes
$ sudo apt-get update -y && ap-get upgrade -y
Configuring DNS Resolution
On Linux and other 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.
$ vi /etc/hosts
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:
Import the MongoDB GPG Key
First, you need to import the MongoDB GPG key to ensure the authenticity of the software packages:
To obtain the most recent version of this software, you must include MongoDB’s dedicated package repository in your APT sources. Then, you’ll be able to install, a meta-package that always points to the latest version of MongoDB.
To start, import the public GPG key for the latest stable version of MongoDB by running the following command
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Add MongoDB Repository
Next, you'll add the MongoDB repository to your system's repository list:
APT (Advanced Package Tool) relies on two key locations to find online package repositories for downloading and installing software: the sources.list
file and the sources.list.d
directory. The sources.list
file contains a list of repository sources, each on a separate line, with the most preferred sources at the beginning of the list. Meanwhile, the sources.list.d
directory offers a way to manage additional repository sources by placing them in separate files, making repository management more modular and organized.
Run the following command, which creates a file in the sources.list.d
directory named mongodb-org-4.4.list
.
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
After running this command, update your server’s local package index so APT knows where to find the mongodb-org package:
$ sudo apt-get update -y
Install MongoDB
Now, you can install MongoDB using the following command:
$ sudo apt-get install mongodb-org -y
This command will install the MongoDB package and its dependencies.
Start MongoDB Service
After installation, MongoDB should start automatically. You can verify the status of the service by running:
$ sudo systemctl status mongod
$ sudo systemctl start mongod
Enable MongoDB to Start on the Boot
$ sudo systemctl enable mongod
Verify Installation
To confirm MongoDB's successful installation, open the MongoDB shell by following these steps:
$ mongo --version
$ mongo
Enable Replication in Each Server's MongoDB Configuration File:-
By updating the /etc/hosts
files on your servers, you've successfully set up hostnames to correlate with their respective IP addresses.
To modify the MongoDB database settings, edit the main configuration file located at /etc/mongod.conf
. This file is automatically generated during the MongoDB installation process.
Use a text editor vi
to open and modify the file. Here’s the command:
$ vi /etc/mongod.conf
In MongoDB, various configuration options allow you to customize the server's behavior. The systemLog option controls the logging settings of your database, enabling you to define what gets logged and where. On the other hand, the net option is pivotal for adjusting network-related settings, offering control over aspects like port configuration and network interfaces.
here change the in each server IP 127.0.0.0 > 0.0.0.0
Create a mongo key file in each server
$ mkdir -p /etc/mongodb/key-files/
Now create an OpenSSL and run these commands
$ openssl rand -base64 756 > /etc/mongodb/key-files/mongo-key
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.
$ vi /etc/mongod.conf
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
$ chmod 400 /etc/mongodb/key-files/mongo-key
$ chown -R mongodb:mongodb /etc/mongodb
Before executing the following command, ensure the Mongod service is stopped.
$ systemctl stop mongod
Replication runs these commands in all nodes for set replset
$ /usr/bin/mongod --replSet "evermight" --config /etc/mongod.conf --fork
Also add replication all nodes
$ systemctl start mongod
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.
$ mongo
To begin the replication process, execute these commands:
mongo> rs.initiate({_id: "evermight", members: [{_id: 1, host: "mongo1"},{_id: 2, host: "mongo2"}] });
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