# MongoDB 6 Single Replica Setup

> Set up MongoDB 6 single replica for API Maker deployment—fast and reliable.

Source: https://docs.apimaker.dev/v1/docs/blog/MongoDB_6_Start_database_with_single_replica.html

👉 We can use MongoDB transactions feature only when MongoDB started in replica mode or we can say cluster mode.

👉 Moreover 🚀[API Maker](https://www.apimaker.dev) needs MongoDB 6 to be running in replica mode.

So, to start MongoDB in replica mode follow below steps.

## 1.) Create docker-compose.yml file with below text

- Create some directory and stay in that directory and run all below commands.

```yaml
version: "3.8"
services:
    am_mongodb:
        image: mongo:6.0.23
        container_name: api_maker_mongodb_rs
        command: [ "--replSet", "rs0", "--bind_ip_all", "--port", "27017", "--keyFile", "/opt/keyfile" ]
        ports:
            - 27017:27017 # For separate port you can keep 27018:27017
        environment: # Not working without username and pass. So keep it.
            - MONGO_INITDB_ROOT_USERNAME=api_maker_user # username
            - MONGO_INITDB_ROOT_PASSWORD=aKXVLF7CZFNNvzWRZbdd # password
            - MONGO_REPLICA_SET_NAME=rs0
        healthcheck:
            test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'127.0.0.1:27017'}]}) }" | mongosh -u "api_maker_user" -p "aKXVLF7CZFNNvzWRZbdd" --port 27017 --quiet
            interval: 5s
            timeout: 30s
            start_period: 0s
            retries: 30
        volumes:
            - ./docker-data/am_mongodb/db:/data/db
            - ./docker-data/am_mongodb/configdb:/data/configdb
            - ./keyfile:/opt/keyfile

```

## 2.) Generate keyfile or use below

- Create keyfile in same directory using below commands or create file from below keyfile content.

MacOS

```bash
openssl rand -base64 741 > keyfile
chmod 600 keyfile
```

Linux

```bash
openssl rand -base64 756 > keyfile
chmod 600 keyfile
sudo chown 999 keyfile
sudo chgrp 999 keyfile
```

Windows

- We can use linux commands in git bash for that.

Keyfile content

```text
3ZlDTasYgr9tvXxcoZqeZkyWuhmPlssi1bLdVU+95+tintw007UY4THzRuV/yzMm
BOGl40q+JTJ6f8TXCjBwJT+nid9OoqoGAczoFqAjlAayuY4Qmv8Iv38XEJHAAx/U
WC/HTCOodCHo9YBBYQ68uSTVzx47Nyp/z+CR3b1f9rA0hX9zkm/H3B96Vu7lhkJ1
1wxpRlQ8udj2LrcZ0cAuMDCcuGDsa5Nydg3OEHs4PHzdIdDm+LB2YavsOGEyM422
vPXH7pX1VAag1Br1CMmJijegau83SLJZN875TC+sWelKjH7nb1i/nWqsabThpnIu
wpb+pl5vvJGxq21qZysz/8ZT7YgD6FMJoTMdFw6+MAXe0OiHlZUW5m6DPs9VL9wa
CJXyU97UULmxHTUO4+7MoxwoGL/59vspgytBOpXRrOlnAepbLDOwTK4jyz3cfx1P
6xqdctgDAuDLp3yqK/GovpWxTHaWlSaqgqMfiqZptwWUR08uhzx9SI6buqr1lWrz
SkFLQICOZ+Cjdb845/grloftBS1zDNflgLgY75Wzut8X6swEFebfyA3HRNOLh7pm
4fEGNTf3OnwAhDj18y943NfVyKlb57Tl33rAj0xDsb2ku+Ug71xOjkdrTtKczczD
rsVUWtyiMieN1oivO9Ss1e/bbHfTyUlqfNlXSo4wqVS6wqnPQOYX6JeSPowvYdf4
J1D/iSiPlqKiy28T/KUq+h04VMCCJUFW6a87yRczT1lp4F5bWzhzY6Of3ebok9hS
/KlYm4/uegDFMC+Rd/EWaagGwgL5KBHbvkgGz65uOxlTVJzGiZngKUrs8RmIlOXH
SynnwFHUP7jSUAedZjxlWkt9b7U5rSXAPnFhhW4QNoeccnrqcJcS2lQQA8wSD6ur
2jo73kLVAcesAAoZ8jNBZxQOO7y4/9m7v5zCj0VJ2BIrCtwqEYUtCuMH06JkecCU
DBm/m13f7WpUd2mvZk6ULc/D8n03
```

## 3.) Start docker container
```bash
docker-compose up -d
# Or with specific file
/usr/bin/docker-compose --project-name api_maker -f ~/config/docker-compose.yml up -d

# Stop docker container
/usr/bin/docker-compose --project-name api_maker -f ~/config/docker-compose.yml down
```

## 4.) Connection string sample to connect to above

- We can use MongoDB compass, NoSQLBooster, Navicat or Studio3T to connect to database.
- In case of not working, try removing `directConnection` query parameter.
```
mongodb://api_maker_user:aKXVLF7CZFNNvzWRZbdd@127.0.0.1:27017?authSource=admin&replicaSet=rs0&directConnection=true
```

### You can follow me on [Twitter](https://twitter.com/api_maker), [LinkedIn](https://www.linkedin.com/company/api-maker), [dev.to](https://dev.to/apimaker)
