Skip to content

MongoDB - 6, Start database with single replica

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

👉 Moreover 🚀API Maker 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.
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 or use below

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

MacOS

openssl rand -base64 741 > keyfile
chmod 600 keyfile

Linux

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

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

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 up -d

4.) Connect terminal in container

# List all containers
docker container ls

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

5.) Connect to mongodb

mongosh -u your_username -p aKXVLF7CZFNNvzWRZbun

6.) Start replica set

rs.initiate({
    _id : "rs0",
    members: [
        { _id: 0, host: "127.0.0.1:27017" }
    ]
});

7.) Give full permissions to user

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
    

You can follow me on Twitter, LinkedIn, dev.to