Connection string
try {
const client = await MongoClient.connect(
`mongodb://192.168.66.139:30001,192.168.66.139:30002,192.168.66.139:30003/?replicaSet=agis-replica-set`,
{},
);
mongo 4.2
$ cat docker-compose.yml
version: "3.8"
services:
mongo1:
image: mongo:4.2
container_name: mongo1
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30001"]
volumes:
- ./data/mongo-1:/data/db
ports:
- 30001:30001
healthcheck:
test: test $(echo "rs.initiate({_id:'agis-replica-set',members:[{_id:0,host:\"mongo1:30001\"},{_id:1,host:\"mongo2:30002\"},{_id:2,host:\"mongo3:30003\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1
interval: 10s
start_period: 30s
mongo2:
image: mongo:4.2
container_name: mongo2
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30002"]
volumes:
- ./data/mongo-2:/data/db
ports:
- 30002:30002
mongo3:
image: mongo:4.2
container_name: mongo3
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30003"]
volumes:
- ./data/mongo-3:/data/db
ports:
- 30003:30003
agis@horse:/data/agis/db-rep/mongo4$ cat todo
docker exec -it mongo1 sh -c "mongo --port 30001"
docker exec -it mongo2 sh -c "mongo --port 30002"
docker exec -it mongo3 sh -c "mongo --port 30003"
rs.initiate(
{
s_id : 'agis-replica-set',
members: [
{ _id : 0, host : "mongo1:30001" },
{ _id : 1, host : "mongo2:30002" },
{ _id : 2, host : "mongo3:30003" }
]
}
)
mongo 6.0.3
$ cat docker-compose.yml
version: "3.8"
services:
mongo1:
image: mongo:6.0.3
container_name: mongo1
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30001"]
volumes:
- ./data/mongo-1:/data/db
ports:
- 30001:30001
mongo2:
image: mongo:6.0.3
container_name: mongo2
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30002"]
volumes:
- ./data/mongo-2:/data/db
ports:
- 30002:30002
mongo3:
image: mongo:6.0.3
container_name: mongo3
command: ["--replSet", "agis-replica-set", "--bind_ip_all", "--port", "30003"]
volumes:
- ./data/mongo-3:/data/db
ports:
- 30003:30003
networks:
default:
external:
name: agis-net
agis@horse:/data/agis/db-rep/mongo6$ cat todo
docker exec -it mongo1 sh -c "mongosh --port 30001"
docker exec -it mongo2 sh -c "mongosh --port 30002"
docker exec -it mongo3 sh -c "mongosh --port 30003"
rs.initiate(
{
_id : 'agis-replica-set',
members: [
{ _id : 0, host : "mongo1:30001" },
{ _id : 1, host : "mongo2:30002" },
{ _id : 2, host : "mongo3:30003" }
]
}
)