Provides the ability to execute a single command to build and run a MongoDB Sharded Cluster on a local workstation with each MongoDB component (mongod
, mongos
) running in a separate Docker container. Uses a Docker Compose project to launch the sharded MongoDB cluster containerised deployment consisting of 11 separate containers for:
- 6
mongod
processes for the 2 shard replica sets - 3
mongod
processes for the configdb replica set - 2
mongos
router processes
All the containers are visible to each other on the same internal network. Once running, the MongoDB cluster is accessible directly from your workstation via the localhost forwarded ports 27107 & 27108 , which connect to each of the two mongos processes, respectively.
The first time you execute the command to build and run the containers, it takes a few minutes to download all the base Docker images. The containers will normally come up in less than 5 seconds when the command is executed the second and subsequent times.
- Your workstation is running a recent version of Linux, Windows or Mac OS X
- Docker is already installed on your workstation
- Docker Compose is already installed on your workstation
- The MongoDB Shell is already installed on your workstation for to you to issue commands to the running database cluster from your workstation (alternatively use the MongoDB Compass graphical tool to connect to the cluster)
- Launch a command line terminal in the base of folder of this project and execute the following command to build and start all the containers in the Docker Compose project:
docker-compose up --build -d
- Connect to the running MongoDB cluster from the MongoDB Shell (the shell will attempt to connect to the first of the two
mongos
endpoints) and then issue the command to print the states of the sharded cluster:
mongosh --port 27030
sh.status()
Note: Use port 27031 instead, above, if you want to connect to the second mongos
endpoint.
mongosh --port 27020 --authenticationDatabase admin --eval '
db = db.getSiblingDB("admin");
db.createUser({
user: "pathleader-system",
pwd: "pathleader",
roles: [
{ db: "admin", role: "readWrite" },
{ db: "admin", role: "dbAdmin" },
{ db: "admin", role: "userAdminAnyDatabase" },
{ db: "admin", role: "readWriteAnyDatabase" },
{ db: "admin", role: "dbOwner" },
{ db: "admin", role: "clusterAdmin" }
],
mechanisms: ["SCRAM-SHA-1"]
})
'
- To show all the running docker containers for this Docker Compose project, run:
docker-compose ps
- To show the container logs for one of the
mongos
routers, run:
docker-compose logs mongos-router0
- To execute a terminal session directly in one of the
mongos
containers, to then invoke the MongoDB Shell directly accessing the localmongos
process, run:
docker-compose exec mongos-router0 /bin/bash
mongosh
- To execute a terminal session directly in one of the
mongod
containers and then view themongod
process logs, run:
docker-compose exec shard0-replica0 /bin/bash
cat /data/db/mongod.log
- To shutdown and remove all the Docker Compose project's running containers (ready for you to rebuild and run again), run:
docker-compose down