Installation Steps

1. Operating system configurations

Turn off the operating system's firewall (ufw for Ubuntu)

sudo systemctl stop firewalld

sudo systemctl disable firewalld
POWERSHELL

Turn off Selinux

Selinux of the operating system is turned off on CentOS.

sudo setenforce 0

sudo sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
POWERSHELL

Disable Swap

Disable the swap to prevent communication problems on all nodes. For this, the following commands are executed and the swap line in the /etc/fstab file is deleted.

sudo swapoff -a

sudo vi /etc/fstab              
POWERSHELL

2. MongoDB Installation

Ubuntu 2020.04 

Centos 7.9

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
POWERSHELL
sudo apt update

sudo apt install mongodb-org -y
sudo apt-mark hold mongodb-org
POWERSHELL
sudo vi /etc/yum.repos.d/mongodb.repo
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
POWERSHELL
sudo yum install -y mongodb-org
POWERSHELL

3. MongoDB configuration and initialization


sudo vi /etc/mongod.conf
POWERSHELL
ip: 0.0.0.0
port: 25080

replication:
 replSetName: apinizer-replicaset

security:
    authorization: "enabled"
POWERSHELL
sudo systemctl start mongod

sudo systemctl enable mongod
POWERSHELL

4. Replica set configuration and Authorized user definition

mongo mongodb://localhost:25080
#(At this stage, if there is a connection error, the server address and server name should be added under /etc/hosts and one of the values of the expression 127.0.0.1 should be checked for localhost)  

> rs.initiate()

> rs.status()

POWERSHELL
> use admin
> db.createUser(
  {
    user: 'apinizer',
    pwd: 'YOURPASSWORD',
    roles: [ { role: 'root', db: 'admin' } ],
	mechanisms:["SCRAM-SHA-1"] 
  }
 );

> exit;

# (Remove this for Centos ",mechanisms:[ "SCRAM-SHA-1"]")
POWERSHELL

To change password:

use admin

db.changeUserPassword("apinizer", passwordPrompt())
POWERSHELL


mongo  mongodb://localhost:25080 --authenticationDatabase "admin" -u "apinizer" -p

> cfg = rs.conf()
> cfg.members[0].host = "YOURMONGOIPADDRESS:25080"
> rs.reconfig(cfg)
> rs.status()
POWERSHELL

To authorize an existing user:

mongo mongodb://localhost:25080

> use admin;

> db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])
POWERSHELL

5. MongoDB Replicaset Installation on 3 Servers

On Node 1 (mongoDb01):
sudo openssl rand -base64 756 > /home/apinizer/mongo-key  
sudo chmod 400 /home/apinizer/mongo-key
sudo chown -R mongodb:mongodb /home/apinizer/mongo-key
POWERSHELL

Copy mongo-key file to "/home/apinizer/" path (mongoDb02, mongoDb03) on all secondary nodes

On all Mongo servers, the mongod.conf file should be as follows.

On node 1 => mongoDb01
# network interfaces
net:
	port: 25080
	bindIp: "127.0.0.1,mongoDb02,mongoDb03,k8sWorkerIP"
security:
	authorization: enabled
	keyFile:  /home/apinizer/mongo-key
replication:
	replSetName: "apinizer-replicaset"
POWERSHELL

On node 2 => mongoDb02
# network interfaces
net:
	port: 25080
	bindIp: "127.0.0.1,mongoDb01,mongoDb03,k8sWorkerIP"
security:
	authorization: enabled
	keyFile:  /home/apinizer/mongo-key
replication:
	replSetName: "apinizer-replicaset"
POWERSHELL

On node 3=> mongoDb03
# network interfaces
net:
	port: 25080
	bindIp: "127.0.0.1,mongoDb01,mongoDb02,k8sWorkerIP"
security:
	authorization: enabled
	keyFile:  /home/apinizer/mongo-key
replication:
	replSetName: "apinizer-replicaset"
POWERSHELL

After restarting the mongod, Secondary servers are then connected to the Primary by the following commands.
mongo  mongodb://localhost:25080 --authenticationDatabase "admin" -u "apinizer" -p 

> rs.add("mongoDb02:25080")
> rs.add("mongoDb03:25080")
POWERSHELL