Open shehabadel opened 2 years ago
sudo docker network create DistN
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.
DistN
db1
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:27018 --net DistN --name db2 mongo:latest --replSet Dist
sudo docker run -d -p 30003:27018 --net DistN --name db3 mongo:latest --replSet Dist
The three containers are running successfully
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"} ]}
- Create
mongos
directory with foldersdb1
,db2
, anddb3
- Run
mongod --replSet Dist --dbpath mongos/db1 --port 27018
- Run
mongod --replSet Dist --dbpath mongos/db2 --port 27019
- Run
mongod --replSet Dist --dbpath mongos/db3 --port 27020
- 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!
- Run
rs.status()
to make sure it is working well.
Use mongod -f <path to config file> --replSet Dist --dbpath mongos/db1 --port 27018
FINAL UPDATE!
After installing docker on your machine Installation Guide
sudo docker network create DistN
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.
DistN
db1
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.
mongos
directory with foldersdb1
,db2
, anddb3
mongod --replSet Dist --dbpath mongos/db1 --port 27018
mongod --replSet Dist --dbpath mongos/db2 --port 27019
mongod --replSet Dist --dbpath mongos/db3 --port 27020
mongosh --port 27018
rs.initiate(rsconfig)
rs.status()
to make sure it is working well.