Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

MongoDB – 6, Start database with single replica

๐Ÿ‘‰ We are able to use MongoDB transactions function solely when MongoDB began in duplicate mode or we will say cluster mode.

๐Ÿ‘‰ Furthermore ๐Ÿš€API Maker wants MongoDB 6 to be working in duplicate mode.

So, to start out MongoDB in duplicate mode comply with beneath steps.



1.) Create docker-compose.yml file with beneath textual content

  • Create some listing and keep in that listing and run all beneath instructions.
model: "3.7"

providers:
    mongodb:
        picture: mongo:6.0.2
        container_name: mongoDB
        atmosphere: # Not working with out username and go. So hold it.
            - MONGO_INITDB_ROOT_USERNAME=your_username # username
            - MONGO_INITDB_ROOT_PASSWORD=aKXVLF7CZFNNvzWRZbun # password
            - MONGO_REPLICA_SET_NAME=rs0
        volumes:
            - ~/docker-data/mongo-db/information:/information/db
            - ./:/decide/keyfile/
        ports:
            - 27017:27017 # Suggestion: Don't use totally different port
        entrypoint:
            - bash
            - -c
            - |
                chmod 400 /decide/keyfile/keyfile
                chown 999:999 /decide/keyfile/keyfile
                exec docker-entrypoint.sh $$@
        command: "mongod --bind_ip_all --keyFile /decide/keyfile/keyfile --replSet rs0"
Enter fullscreen mode

Exit fullscreen mode



2.) Generate keyfile

  • Create keyfile in identical listing utilizing beneath instructions.



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



Home windows

  • We are able to use linux instructions in git bash for that.



3.) Begin docker container

docker compose up -d
Enter fullscreen mode

Exit fullscreen mode



4.) Join terminal in container

# Listing 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 with mongodb

mongosh -u your_username -p aKXVLF7CZFNNvzWRZbun
Enter fullscreen mode

Exit fullscreen mode



6.) Begin duplicate set

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

Exit fullscreen mode



7.) Give full permissions to consumer

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 pattern to connect with above

  • We are able to use MongoDB compass, NoSQLBooster, Navicat or Studio3T to connect with database.
mongodb://your_username:aKXVLF7CZFNNvzWRZbun@127.0.0.1:27017/api_maker_dev?authSource=admin&replicaSet=rs0
Enter fullscreen mode

Exit fullscreen mode



9.) Me & My work

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?