Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database Replication - MongoDB ReplicaSets #11

Open
shehabadel opened this issue Jun 23, 2022 · 6 comments
Open

Database Replication - MongoDB ReplicaSets #11

shehabadel opened this issue Jun 23, 2022 · 6 comments

Comments

@shehabadel
Copy link
Owner

  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.
@shehabadel
Copy link
Owner Author

shehabadel commented Jun 23, 2022

sudo docker network create DistN

  • Creates a network called 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.

  • Bound it to the network we created DistN
  • Giving the container a name db1
  • Creating a replicaSet Dist

@shehabadel
Copy link
Owner Author

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
Copy link
Owner Author

The three containers are running successfully
3-MakingSureTheThreeContainersRunning

@shehabadel
Copy link
Owner Author

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
Copy link
Owner Author

  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
Copy link
Collaborator

Yakan23 commented Jun 26, 2022

FINAL UPDATE!

After installing docker on your machine Installation Guide

sudo docker network create DistN

  • Creates a network called 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.

  • Bound it to the network we created DistN
  • Giving the container a name db1
  • Creating a replicaSet Dist

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.

@Yakan23 Yakan23 closed this as completed Jun 26, 2022
@Yakan23 Yakan23 reopened this Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants