DEV Community

Cover image for MongoDB - 6, Start database with single replica
API Maker®
API Maker®

Posted on • Edited on

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"
Enter fullscreen mode Exit fullscreen mode

2.) Generate keyfile

  • Create keyfile in same directory using below commands.

MacOS

openssl rand -base64 741 > keyfile
chmod 600 keyfile
Enter fullscreen mode Exit fullscreen mode

Linux

openssl rand -base64 756 > keyfile
chmod 600 keyfile
sudo chown 999 keyfile
sudo chgrp 999 keyfile
Enter fullscreen mode Exit fullscreen mode

Windows

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

3.) Start docker container

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

4.) Connect terminal in container

# List all containers
docker container ls

#fill CONTAINER_ID from above command
docker exec -it CONTAINER_ID /bin/bash
Enter fullscreen mode Exit fullscreen mode

5.) Connect to mongodb

mongosh -u your_username -p aKXVLF7CZFNNvzWRZbun
Enter fullscreen mode Exit fullscreen mode

6.) Start replica set

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

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" },
        ],
    }
);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

9.) Me & My work

Top comments (0)