Skip to content

Instantly share code, notes, and snippets.

@msramdan
Forked from khannedy/docker-compose.yaml
Created April 14, 2023 03:55
Show Gist options
  • Save msramdan/c606886a59170b3988253b3ffaa5e33a to your computer and use it in GitHub Desktop.
Save msramdan/c606886a59170b3988253b3ffaa5e33a to your computer and use it in GitHub Desktop.
Mongo Sharding
version: "3.5"
services:
mongo-config-server1:
image: mongo:4.2
container_name: mongo-config-server1
command: mongod --configsvr --replSet rnd-config-replica-set --port 27019
mongo-config-server2:
image: mongo:4.2
container_name: mongo-config-server2
command: mongod --configsvr --replSet rnd-config-replica-set --port 27019
mongo-config-server3:
image: mongo:4.2
container_name: mongo-config-server3
command: mongod --configsvr --replSet rnd-config-replica-set --port 27019
mongo-first-shard1:
image: mongo:4.2
container_name: mongo-first-shard1
command: mongod --shardsvr --replSet rnd-first-shard-replica-set --port 27018
mongo-first-shard2:
image: mongo:4.2
container_name: mongo-first-shard2
command: mongod --shardsvr --replSet rnd-first-shard-replica-set --port 27018
mongo-first-shard3:
image: mongo:4.2
container_name: mongo-first-shard3
command: mongod --shardsvr --replSet rnd-first-shard-replica-set --port 27018
mongo-second-shard1:
image: mongo:4.2
container_name: mongo-second-shard1
command: mongod --shardsvr --replSet rnd-second-shard-replica-set --port 27018
mongo-second-shard2:
image: mongo:4.2
container_name: mongo-second-shard2
command: mongod --shardsvr --replSet rnd-second-shard-replica-set --port 27018
mongo-second-shard3:
image: mongo:4.2
container_name: mongo-second-shard3
command: mongod --shardsvr --replSet rnd-second-shard-replica-set --port 27018
mongo-third-shard1:
image: mongo:4.2
container_name: mongo-third-shard1
command: mongod --shardsvr --replSet rnd-third-shard-replica-set --port 27018
mongo-third-shard2:
image: mongo:4.2
container_name: mongo-third-shard2
command: mongod --shardsvr --replSet rnd-third-shard-replica-set --port 27018
mongo-third-shard3:
image: mongo:4.2
container_name: mongo-third-shard3
command: mongod --shardsvr --replSet rnd-third-shard-replica-set --port 27018
mongo-client:
image: mongo:4.2
container_name: mongo-client
command: mongos --configdb rnd-config-replica-set/mongo-config-server1:27019,mongo-config-server2:27019,mongo-config-server3:27019 --port 27017 --bind_ip_all
ports:
- 27017:27017
// docker compose -f docker-compose.yaml up -d
// docker container exec -it mongo-config-server1 /bin/sh
// mongo --host localhost --port 27019
rs.initiate(
{
_id: "rnd-config-replica-set",
configsvr: true,
members: [
{
_id: 0,
host: "mongo-config-server1:27019"
},
{
_id: 1,
host: "mongo-config-server2:27019"
},
{
_id: 2,
host: "mongo-config-server3:27019"
}
]
}
)
// docker container exec -it mongo-first-shard1 /bin/sh
// mongo --host localhost --port 27018
rs.initiate(
{
_id: "rnd-first-shard-replica-set",
members: [
{_id: 0, host: "mongo-first-shard1:27018"},
{_id: 1, host: "mongo-first-shard2:27018"},
{_id: 2, host: "mongo-first-shard3:27018"}
]
}
)
// docker container exec -it mongo-second-shard1 /bin/sh
// mongo --host localhost --port 27018
rs.initiate(
{
_id: "rnd-second-shard-replica-set",
members: [
{_id: 0, host: "mongo-second-shard1:27018"},
{_id: 1, host: "mongo-second-shard2:27018"},
{_id: 2, host: "mongo-second-shard3:27018"}
]
}
)
// docker container exec -it mongo-third-shard1 /bin/sh
// mongo --host localhost --port 27018
rs.initiate(
{
_id: "rnd-third-shard-replica-set",
members: [
{_id: 0, host: "mongo-third-shard1:27018"},
{_id: 1, host: "mongo-third-shard2:27018"},
{_id: 2, host: "mongo-third-shard3:27018"}
]
}
)
// docker container exec -it mongo-client /bin/sh
// mongo --host localhost --port 27017
sh.addShard("rnd-first-shard-replica-set/mongo-first-shard1:27018,mongo-first-shard2:27018,mongo-first-shard3:27018")
sh.addShard("rnd-second-shard-replica-set/mongo-second-shard1:27018,mongo-second-shard2:27018,mongo-second-shard3:27018")
sh.addShard("rnd-third-shard-replica-set/mongo-third-shard1:27018,mongo-third-shard2:27018,mongo-third-shard3:27018")
sh.enableSharding("belajar")
sh.shardCollection("belajar.orders", {"userId": "hashed"})
// docker container exec -it mongo-first-shard1 /bin/sh
// mongo --host localhost --port 27018
// docker container exec -it mongo-second-shard1 /bin/sh
// mongo --host localhost --port 27018
// docker container exec -it mongo-third-shard1 /bin/sh
// mongo --host localhost --port 27018
// docker container stop mongo-third-shard1
// docker container stop mongo-third-shard2
// docker container stop mongo-third-shard3
// docker container start mongo-third-shard1 mongo-third-shard2 mongo-third-shard3
// docker compose -f docker-compose.yaml down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment