Skip to content

Instantly share code, notes, and snippets.

@anhdiepmmk
Last active January 30, 2020 12:43
Show Gist options
  • Save anhdiepmmk/c421692f4ee99b4cba63ca2d0b839613 to your computer and use it in GitHub Desktop.
Save anhdiepmmk/c421692f4ee99b4cba63ca2d0b839613 to your computer and use it in GitHub Desktop.
Thực hành tạo replica set trên mongo db sử dụng công cụ docker, docker compose
version: "3"
services:
mongoarbiter:
hostname: mongoarbiter
container_name: localmongoarbiter
image: mongo:4
expose:
- 27017
ports:
- 17016:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo1:
hostname: mongo1
container_name: localmongo1
image: mongo:4
expose:
- 27017
ports:
- 17017:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo2:
hostname: mongo2
container_name: localmongo2
image: mongo:4
expose:
- 27017
ports:
- 17018:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo3:
hostname: mongo3
container_name: localmongo3
image: mongo:4
expose:
- 27017
ports:
- 17019:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
version: '3.5'
networks:
default:
services:
mongo:
restart: unless-stopped
image: mongo:4
expose:
- 27016
- 27017
- 27018
- 27019
ports:
- "17016:27016"
- "17017:27017"
- "17018:27018"
- "17019:27019"
volumes:
- /home/data/mongo/:/data/db
entrypoint: >
/bin/bash -c "mkdir -p /var/log/mongo /data/db /data/db1 /data/db2 /data/arbiter &&
mongod --dbpath /data/arbiter --port 27016 --bind_ip_all --fork --logpath /var/log/mongo/mongod-arbiter.log --replSet "rs0" &&
mongod --dbpath /data/db --port 27017 --bind_ip_all --fork --logpath /var/log/mongo/mongod-27017.log --replSet "rs0" &&
mongod --dbpath /data/db1 --port 27018 --bind_ip_all --fork --logpath /var/log/mongo/mongod-27018.log --replSet "rs0" &&
mongod --dbpath /data/db2 --port 27019 --bind_ip_all --fork --logpath /var/log/mongo/mongod-27019.log --replSet "rs0" &&
sleep 15s &&
echo 'rs.initiate({
\"_id\": \"rs0\",
\"members\": [
{
\"_id\": 0,
\"host\": \"127.0.0.1:27017\"
},
{
\"_id\": 1,
\"host\": \"127.0.0.1:27018\"
},
{
\"_id\": 2,
\"host\": \"127.0.0.1:27019\"
}
]
})' | mongo &&
echo 'Thêm arbiter' &&
echo 'rs.addArb(\"127.0.0.1:27016\")' | mongo &&
echo 'Xem lại trạng thái' &&
echo 'rs.status()' | mongo &&
echo 'Xem lại config' &&
echo 'rs.conf()' | mongo &&
tail /var/log/mongo/* -f"
@anhdiepmmk
Copy link
Author

anhdiepmmk commented Jan 30, 2020

Cách 1

docker-compose -f mongodb.replica-set.yml up

Cách 2

Bước 1:

docker-compose -f mongodb.replica-set.v2.yml up

Bước 2:

docker exec -it localmongo1 bash

Bước 3:

mongo

Bước 4:

rs.initiate(
  {
    _id : 'rs0',
    members: [
      { _id : 0, host : "mongo1:27017" },
      { _id : 1, host : "mongo2:27017" },
      { _id : 2, host : "mongo3:27017" }
    ]
  }
)

Bước 5:

Thêm node trọng tài
rs.addArb("mongoarbiter:27017")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment