An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
November 25, 2022 10:46 am GMT
1.) Create
2.) Generate
Original Link: https://dev.to/mayur_dit/mongodb-6-start-database-with-single-replica-1g4m
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
- Create keyfile in same directory using below commands.
MacOS
openssl rand -base64 741 > keyfilechmod 600 keyfile
Linux
openssl rand -base64 756 > keyfilechmod 600 keyfilesudo chown 999 keyfilesudo chgrp 999 keyfile
Windows
- We can use linux commands in git bash for that.
3.) Start docker container
docker compose up -d
4.) Connect terminal in container
# List all containersdocker container ls#fill CONTAINER_ID from above commanddocker 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:[email protected]:27017/api_maker_dev?authSource=admin&replicaSet=rs0
9.) Me & My work
Original Link: https://dev.to/mayur_dit/mongodb-6-start-database-with-single-replica-1g4m
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To