{"id":20554978,"url":"https://github.com/ab70/mongodb-replicaset-docker","last_synced_at":"2026-04-29T21:36:08.714Z","repository":{"id":220765595,"uuid":"752535265","full_name":"ab70/mongodb-replicaset-docker","owner":"ab70","description":null,"archived":false,"fork":false,"pushed_at":"2025-08-25T07:04:14.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-09T07:44:02.647Z","etag":null,"topics":["docker","mongodb","replica-set"],"latest_commit_sha":null,"homepage":"https://medium.com/@nurulabrar2369/setup-mongodb-replica-set-using-docker-1e8f304b2a55","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ab70.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-02-04T05:52:24.000Z","updated_at":"2025-08-25T07:04:17.000Z","dependencies_parsed_at":"2024-02-04T18:46:16.224Z","dependency_job_id":"9a40fe8b-57ec-4a0d-940c-3fb14075633c","html_url":"https://github.com/ab70/mongodb-replicaset-docker","commit_stats":null,"previous_names":["ab70/mongodb-replicaset","ab70/mongodb-replicaset-docker"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ab70/mongodb-replicaset-docker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ab70%2Fmongodb-replicaset-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ab70%2Fmongodb-replicaset-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ab70%2Fmongodb-replicaset-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ab70%2Fmongodb-replicaset-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ab70","download_url":"https://codeload.github.com/ab70/mongodb-replicaset-docker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ab70%2Fmongodb-replicaset-docker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32445542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T20:22:27.477Z","status":"ssl_error","status_checked_at":"2026-04-29T20:22:26.507Z","response_time":110,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","mongodb","replica-set"],"created_at":"2024-11-16T03:15:09.190Z","updated_at":"2026-04-29T21:36:08.697Z","avatar_url":"https://github.com/ab70.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Install Docker on your machine\n```\ncurl -fsSL https://get.docker.com -o get-docker.sh\nsh get-docker.sh\n```\n## Verify installations\n```\ndocker --version\ndocker compose version\n```\n\n## mongodb-replicaset\n## 1. Run setup.sh\n```\nbash setup.sh\n```\n## 2. Start Docker containers\n```\ndocker compose down\ndocker compose up -d\n```\n## 3. Execute commands inside mongo1 container\n```\ndocker exec -it mongo1 bash\n```\n## 4. Connect to MongoDB shell\n```\nmongosh -u root -p example\n```\n\n### 4.1 If authentication is not enabled by the docker compose then you have to initialize manulally\n### 4.1.1 Connect to shell without authentication\n```\nmongosh\n```\n### 4.1.2 Switch to admin db (it will also creat ethe db)\n```\ndb.createUser({\n  user: \"root\",\n  pwd: \"example\",\n  roles: [{ role: \"root\", db: \"admin\" }]\n})\n\n```\n#### Now it will create root user (This step can also be done after rs.initiste, sometimes rs.initiate wont allow without authentication)\n\n## 5. Initialize replica set\n```\nrs.initiate(\n  {\n    _id: \"rs0\",\n    version: 1,\n    members: [\n      { _id: 0, host: \"mongo1:27017\", priority: 3 },\n      { _id: 1, host: \"mongo2:27018\", priority: 2 },\n      { _id: 2, host: \"mongo3:27019\", priority: 1 }\n    ]\n  }\n) \n```\n\n## 6. Check replica set status\n```\nrs.status()\n```\n\n## Connect using compass\n## Url\n```\nmongodb://root:example@localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0\u0026authSource=admin\n```\nIf youu can't connect and it keeps loading then for Linux :\n```\nsudo nano /etc/hosts\n```\nAdd or modify 127.0.0.1 localhost , then add container name for example:\n```\n127.0.0.1   localhost   mongo1\n```\nThen save \u0026 exit. Now with the url it is accessable.\n\n## Automate Full Deployment\n#### This will work if if have initialized the whole process once (Initially it fails for delay issue which can vary from matchine to matchine).\n```\nsudo bash deploy.sh\n```\n#### Or just run the dcker once its been initialized, no need to initiate whole process again, just run\n```\nsudo docker compose up -d\n```\n\n#### To remove all volume at once\n```\ndocker volume rm $(docker volume ls -q)\n\n```\n## Backup db (manual)\n\n#### To backup a db if a url is present: \n```\n#It will backup the whole db in that current irectory(full cluster)\nmongodump --uri \"mongodb+srv://username:password@cluster0.mongodb.net\" --out .\n```\n\n#### To restore a db if url is present:\n```\n# This will restore with the url frombackup directtory(current):(this will merge data without deleting previous data)\nmongorestore --uri \"mongodb+srv://username:password@cluster0.mongodb.net\" .\n\n# This will restore with the url frombackup directtory(current):(this will replace or delete previous data and keep current only)\nmongorestore --uri \"mongodb+srv://username:password@cluster0.mongodb.net\" --drop .\n \n```\n### To restore a cluster from inside the docker of that db\n```\n#Copy the db to the dockers a directory /tmp\ndocker cp /home/abrar/niamCluster mongo1:/tmp\n\n#Get into the db docker\ndocker exec -it mongo1\n\n#Inside the docker go to that /tmp folder and the cluster directory\ncd /tmp/foldername\n\n#now from here sync DB using this\nmongorestore --username root --password example --authenticationDatabase admin .\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fab70%2Fmongodb-replicaset-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fab70%2Fmongodb-replicaset-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fab70%2Fmongodb-replicaset-docker/lists"}