---
# System prepended metadata

title: MongoDB Replication Setup by Nayana

---

# MongoDB Replication Setup by Nayana

> Document Created by: Nayana
> Date: 9-Aug-2021
> Ref setup document : [From Ramesh](https://hackmd.io/qT1FVIzaQXuGEY4fQ4bKjw)

## Ubuntu Details
1) Check Ubuntu version
```
Command: lsb_release -a

Output:
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.3 LTS
Release:	18.04
Codename:	bionic
```
---

## Install Docker
2) Install Docker on Ubuntu machine

[Install docker](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)
[Docker Installation Ref from Ramesh](https://hackmd.io/vKk34V79SlSGL0A0Pee_fQ?view#Install-docker)
1. Allow apt to use repository over HTTPS
```
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
```
2. Add Docker’s official GPG key:
```
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
 ```
 
3. Setup stable repository 
 ```
 echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
```
4. Install docker engine and verify installation  
```
sudo apt-get update
 sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo docker run hello-world
  ```
 ***verify docker version***
 ```
 docker --version
 ```
![](https://i.imgur.com/1Fc4Q66.png)

---
## Install MongoDB
[MongoDB Installation Reference](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/)
**NOTE: Primary and container MongoDB version should be same**
```
Step 1:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

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

Step 3:
sudo apt-get update

Step 4: Install MongoDB Package
sudo apt-get install -y mongodb-org

Step5: Start MongoDB server
sudo systemctl start mongod

Step 6: Check MongoDB status
sudo systemctl status mongod

```
---
## Create the in-container MongoDB server suitable for replication set
1) apt update
2) apt install -y vim
3) Create directory mkdir mongo_docker
4) Create default.js file and insert code [default.js](https://gitlab.ieiworld.com/edge-server-docker/mongodb-docker/blob/master/src/default.js)
5) Create  docker file => "vi Dockerfile ", 
 Copy below lines to Dockerfile 
> FROM mongo:3.6-xenial
> RUN apt update && apt install -y vim
> ENV MONGO_INITDB_DATABASE dtaDB
> ADD default.js /docker-entrypoint-initdb.d/default.js
> EXPOSE 27017

## Build Docker images & Configure MongoDB
1) Build docker image "mongo:0.1"
`docker build -f Dockerfile -t mongo:0.1 .`

2)  Create folder under mongo_docker folder
    ```
     mkdir -p sec1/conf
     mkdir -p sec2/conf
    ```
     Above commands will create folder {/sec1/conf,  sec2/conf}
3) Create mongod.conf file 
    ```
    cp /etc/mongod.conf sec1/conf/
    cp /etc/mongod.conf sec2/conf/
    ```
4) Update mongod.conf 
   Files to update: /etc/mongod.conf,  sec1/conf/mongod.conf, sec2/conf/mongod.conf
   Update below information 
    ```
    dbPath : /data/db
    bindIP : 0.0.0.0
    ```
## Create Docker container
[Docker container Ref](https://docs.docker.com/engine/reference/commandline/container_run/)
```
docker run -d --name factory_sec_1 -v /home/test/mongo_docker/sec1/conf:/etc/mongo -v /home/test/mongo_docker/sec1/mongodb:/data/db -p 27018:27017 mongo:0.1 --config /etc/mongo/mongod.conf --logpath /data/db/mongod.log

docker run -d --name factory_sec_2 -v /home/test/mongo_docker/sec2/conf:/etc/mongo -v /home/test/mongo_docker/sec2/mongodb:/data/db -p 27019:27017 mongo:0.1 --config /etc/mongo/mongod.conf --logpath /data/db/mongod.log
```
#### Access secondary server from host
```
mongo 127.0.0.1:27018
mongo 127.0.0.1:27019
```
#### Access the secondary replication servers via the respective containers
```
docker exec -it factory_sec_1 /bin/bash
docker exec -it factory_sec_2 /bin/bash
```

#### Change replication setting in primary server 
```
vi  /etc/mongodb.conf

replSet=rs_factory
```
Restart primary server
` systemctl restart mongodb `

#### Initialize repication set in primary server (mongo shell)

```
mongo
rs.initiate()
```

#### Change the repication settings in the secondary servers
Files to update: vi sec1/conf/mongod.conf, sec2/conf/mongod.conf
```
replication:  
  replSetName: "rs_factory"
```

#### Restart container
```
docker restart factory_sec_1
docker restart factory_sec_2
```

#### Add the secondary servers to the primary server (mongo shell)
```
mongo

rs.add("127.0.0.1:27018")
rs.add("127.0.0.1:27019")
```

**Note:** Instead of local IP (127.0.0.1) use public IP(10.9.2.20 like this) to access replication from outside.

---
---
---

#### Docker and container's useful commands 
Check List of docker container
```
docker ps -a
```
Restart container
```
docker restart <container_name>
```
stop & remove Container
    ```
    docker stop <container_name>
    docker rm <container_name>
    ```

#### Check list of docker images &  Remove docker image
    ```
    docker images
    docker rmi <image_name:tag>
    ```
#### Check primary mongodb server status
    ```
    mongo
    rs.status()
    ```

#### Check scondary mongoDB server status
```
    - Enter into secondary container then fo to mongo shell 
    docker exec -it factory_sec_1 /bin/bash
    mongo
    rs.secondaryOk()   // You can access secondary server db
    
```


### Check mongodb log file
primary
    `vi /var/log/mongodb/mongod.log`

Check in secondary

    docker logs -f factory_sec_1
    OR
    Go to container 
    i) docker exec -it factory_sec_1 /bin/bash
    ii) vi /data/db/mongod.log
    

### Remove replication set
db.system.replset.remove({})