MongoDB - 6, Start database with single replica
Tags: mongod, devops, apimaker, linux
Published: 2022-11-25 10:46:36

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

👉 Moreover 🚀[API Maker](https://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.7"

services:
    mongodb:
        image: mongo:6.0.2
        container_name: mongoDB
        environment: # Not working without username and pass. So keep it.
            - MONGO_INITDB_ROOT_USERNAME=your_username # username
            - MONGO_INITDB_ROOT_PASSWORD=aKXVLF7CZFNNvzWRZbun # password
            - MONGO_REPLICA_SET_NAME=rs0
        volumes:
            - ~/docker-data/mongo-db/data:/data/db
            - ./:/opt/keyfile/
        ports:
            - 27017:27017 # Suggestion: Do not use different port
        entrypoint:
            - bash
            - -c
            - |
                chmod 400 /opt/keyfile/keyfile
                chown 999:999 /opt/keyfile/keyfile
                exec docker-entrypoint.sh $$@
        command: "mongod --bind_ip_all --keyFile /opt/keyfile/keyfile --replSet rs0"
```

## 2.) Generate `keyfile`
- Create keyfile in same directory using below commands.

#### 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.


## 3.) Start docker container
```bash
docker compose up -d
```

## 4.) Connect terminal in container
```bash
# List all containers
docker container ls

#fill CONTAINER_ID from above command
docker exec -it CONTAINER_ID /bin/bash
```

## 5.) Connect to mongodb
```bash
mongosh -u your_username -p aKXVLF7CZFNNvzWRZbun
```

## 6.) Start replica set
```javascript
rs.initiate({
    _id : "rs0",
    members: [
        { _id: 0, host: "127.0.0.1:27017" }
    ]
});
```

## 7.) Give full permissions to user
```javascript
db.getSiblingDB("admin").updateUser(
    "your_username",
    {
        customData: {},
        roles: [
            { "role": "root", "db": "admin" },
            { "role": "readAnyDatabase", "db": "admin" },
            { "role": "readWriteAnyDatabase", "db": "admin" },
            { "role": "userAdminAnyDatabase", "db": "admin" },
            { "role": "dbAdminAnyDatabase", "db": "admin" },

            { "role": "backup", "db": "admin" },
            { "role": "restore", "db": "admin" },

            { "role": "clusterAdmin", "db": "admin" },
            { "role": "clusterManager", "db": "admin" },
            { "role": "clusterMonitor", "db": "admin" },
            { "role": "hostManager", "db": "admin" },

            { "role": "dbAdmin", "db": "admin" },
            { "role": "dbOwner", "db": "admin" },
            { "role": "userAdmin", "db": "admin" },
        ],
    }
);
```


## 8.) Connection string sample to connect to above
- We can use MongoDB compass, NoSQLBooster, Navicat or Studio3T to connect to database.

```
mongodb://your_username:aKXVLF7CZFNNvzWRZbun@127.0.0.1:27017/your_database_name?authSource=admin&replicaSet=rs0&directConnection=true
```

## 9.) Me & My work
- My recent work: 🚀 [API Maker](https://apimaker.dev)
- You can follow me on [Twitter](https://twitter.com/mayur_dit2)
- [LinkedIn](https://www.linkedin.com/in/mayurkumar-jagdishkumar-patel/)
- [Mostodon](https://fosstodon.org/@mayurpatel)