shehabadel / Multi-User-Distributed-Text-Editor

The final project of Distributed Systems course (CSE354). A multi-user distributed text editor project which allows real-time collaboration between users on documents. It is more like a Google documents clone. Tech Stack: Node.js, React.js, Redis, Docker, Socket-IO, MongoDB ReplicaSets
5 stars 3 forks source link

Database Replication - MongoDB ReplicaSets #11

Open shehabadel opened 2 years ago

shehabadel commented 2 years ago
  1. Create mongos directory with folders db1, db2, and db3
  2. Run mongod --replSet Dist --dbpath mongos/db1 --port 27018
  3. Run mongod --replSet Dist --dbpath mongos/db2 --port 27019
  4. Run mongod --replSet Dist --dbpath mongos/db3 --port 27020
  5. In case replicaSet error or it is not initiliazed
  1. Run rs.status() to make sure it is working well.
shehabadel commented 2 years ago

sudo docker network create DistN

sudo docker run -d -p 30001:27018 --net DistN --name db1 mongo:latest --replSet Dist

The -d (detach) flag means the container will run in the background, separately to your shell process.

The container external port 30001 is bound back to the internal port of the container 27018 on your host. You’ll be able to connect to your Mongo instance on localhost:30001. If you want to change the port number, modify the first part of the -p flag, such as 9000:27017 to use localhost:9000.

shehabadel commented 2 years ago

Run the second and third containers using the following commands

We'll have to change the external ports and database names

sudo docker run -d -p 30002:27018 --net DistN --name db2 mongo:latest --replSet Dist sudo docker run -d -p 30003:27018 --net DistN --name db3 mongo:latest --replSet Dist

shehabadel commented 2 years ago

The three containers are running successfully 3-MakingSureTheThreeContainersRunning

shehabadel commented 2 years ago

In order to create the replicaSet we have to login to the first database in an interactive mode and launch the mongo shell sudo docker exec -it db1 mongo

Then create a configuration

rsconfig={_id:"Dist", members:[ {_id:0, host:"db1:27018"}, {_id:1, host:"db2:27018"}, {_id:2, host:"db3:27018"} ]}

shehabadel commented 2 years ago
  1. Create mongos directory with folders db1, db2, and db3
  2. Run mongod --replSet Dist --dbpath mongos/db1 --port 27018
  3. Run mongod --replSet Dist --dbpath mongos/db2 --port 27019
  4. Run mongod --replSet Dist --dbpath mongos/db3 --port 27020
  5. In case replicaSet error or it is not initiliazed
  • Run mongosh --port 27018
  • Write
rsconfig={_id:"Dist",
members:[
{_id:0, host:"localhost:27018"},
{_id:1, host:"localhost:27019"},
{_id:2, host:"localhost:27020"}
]}
  • Run rs.initiate(rsconfig)
  • It must be working now!
  1. Run rs.status() to make sure it is working well.

Use mongod -f <path to config file> --replSet Dist --dbpath mongos/db1 --port 27018

Yakan23 commented 2 years ago

FINAL UPDATE!

After installing docker on your machine Installation Guide

sudo docker network create DistN

sudo docker run -d -p 30001:27017 --net DistN --name db1 mongo:latest --replSet Dist

The -d (detach) flag means the container will run in the background, separately to your shell process.

The container external port 30001 is bound back to the internal port of the container 27017 on your host. You’ll be able to connect to your Mongo instance on localhost:30001. If you want to change the port number, modify the first part of the -p flag, such as 9000:27017 to use localhost:9000.

Run the second and third containers using the following commands

We'll have to change the external ports and database names

sudo docker run -d -p 30002:27017 --net DistN --name db2 mongo:latest --replSet Dist sudo docker run -d -p 30003:27017 --net DistN --name db3 mongo:latest --replSet Dist

In order to create the replicaSet we have to login to the first database in an interactive mode and launch the mongo shell sudo docker exec -it db1 mongo

Then create a configuration

rsconfig={_id:"Dist", members:[ {_id:0, host:"Enter your machine public IP:30001"}, {_id:1, host:"Enter your machine public IP:30002"}, {_id:2, host:"Enter your machine public IP:30003"} ]}

  • Run rs.initiate(rsconfig)
  • It must be working now!

Run rs.status() to make sure it is working well.