GithubHelp home page GithubHelp logo

mongo-compose-replset's Introduction

Start MongoDB replica set with a single click

This is powered by two files: docker-compose.yml and rs-init.sh.

rs-init.sh

This script is run by the container mongo-rs-initiator defined in docker-compose.yml. It's job is to wait until the 3 MongoDB instances are ready and then initiate the replica set. After that it is stopped.

Quick start

Under the root directory, run the following

docker compose up -d

where -d means run in detach mode (run in background).

A data directory will be created with 3 sub-directories. Each of them corresponds to a MongoDB instance. Restarting the stack will not remove the data as long as the data directory is at the root level of the folder.

Verify the replica set

There are several ways to verify that the replica set has been initialised successfully.

1. docker compose logs

Running docker compose logs -f mongo-rs-initiator can show you the logs of the initiator container.

If you run this command right after starting the stack (by docker compose up -d), you may see nothing. That's because the initiator container starts only when the 3 MongoDB instances are up and healthy.

Once you see some output, it means it has started working. When it's finished, it exits automatically and stops. Your replica set should be successfully initiated.

2. Dive into Mongo

You could just wait for a while, or until you see there is output from the initiator container. Then, you can go into the container and check the replica set status.

# Host
$ docker compose exec -it mongo1 mongosh

You should then see the mongosh prompt.

# mongosh
my-replica [direct: secondary] test>

If you press Enter a few times, you should see the prompt changes from secondary to primary.

# mongosh
my-replica [direct: primary] test> rs.status()

Running rs.status() will show the current status. You should first see the ok field is 1. Then, check that there are total of 3 members in the members field.

If you have the time, go ahead and see the status of mongo2 and mongo3. They should all be secondary.

Congratulations! You have started a MongoDB cluster.

Reference

Great thanks to the following authors.

  1. https://blog.tericcabrel.com/mongodb-replica-set-docker-compose/
  2. https://stackoverflow.com/a/73933474/6625422

mongo-compose-replset's People

Contributors

henryfung3a27 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.