|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# This script coordinates a MongoDB sharded cluster. |
| 4 | +# The implementation follows https://www.mongodb.com/docs/manual/tutorial/deploy-shard-cluster/. |
| 5 | +# |
| 6 | + |
| 7 | +set -Eeuo pipefail |
| 8 | + |
| 9 | +echo "[sharded-mongo/docker-entrypoint.sh] forking config server..." |
| 10 | + |
| 11 | +mongod \ |
| 12 | + --configsvr \ |
| 13 | + --replSet config \ |
| 14 | + --dbpath /var/lib/sharded-mongo/mongod-config \ |
| 15 | + --bind_ip_all \ |
| 16 | + --ipv6 \ |
| 17 | + --port 27019 \ |
| 18 | + --fork \ |
| 19 | + --logpath /var/log/mongod-config.log |
| 20 | + |
| 21 | +if [ ! -e /var/lib/sharded-mongo/mongod-config.initialized ]; then |
| 22 | + echo "[sharded-mongo/docker-entrypoint.sh] initializing config server..." |
| 23 | + mongosh --eval 'rs.initiate({_id: "config", configsvr: true, members: [{ _id : 0, host : "localhost:27019" }]})' localhost:27019 |
| 24 | + touch /var/lib/sharded-mongo/mongod-config.initialized |
| 25 | +fi |
| 26 | + |
| 27 | +echo "[sharded-mongo/docker-entrypoint.sh] forking shard server..." |
| 28 | + |
| 29 | +mongod \ |
| 30 | + --shardsvr \ |
| 31 | + --replSet shard \ |
| 32 | + --dbpath /var/lib/sharded-mongo/mongod-shard \ |
| 33 | + --bind_ip_all \ |
| 34 | + --ipv6 \ |
| 35 | + --port 27018 \ |
| 36 | + --fork \ |
| 37 | + --logpath /var/log/mongod-shard.log |
| 38 | + |
| 39 | +if [ ! -e /var/lib/sharded-mongo/mongod-shard.initialized ]; then |
| 40 | + echo "[sharded-mongo/docker-entrypoint.sh] initializing shard server..." |
| 41 | + mongosh --eval 'rs.initiate({_id: "shard", members: [{ _id : 0, host : "localhost:27018" }]})' localhost:27018 |
| 42 | + touch /var/lib/sharded-mongo/mongod-shard.initialized |
| 43 | +fi |
| 44 | + |
| 45 | +echo "[sharded-mongo/docker-entrypoint.sh] forking mongos server..." |
| 46 | + |
| 47 | +cat <<CONF >/etc/mongos.conf |
| 48 | +net: |
| 49 | + bindIpAll: true |
| 50 | + ipv6: true |
| 51 | + port: 27017 |
| 52 | +sharding: |
| 53 | + configDB: config/localhost:27019 |
| 54 | +CONF |
| 55 | + |
| 56 | +mongos \ |
| 57 | + --config /etc/mongos.conf \ |
| 58 | + --fork \ |
| 59 | + --logpath /var/log/mongos.log |
| 60 | + |
| 61 | +if [ ! -e /var/lib/sharded-mongo/mongos.initialized ]; then |
| 62 | + echo "[sharded-mongo/docker-entrypoint.sh] initializing mongos server..." |
| 63 | + mongosh --eval 'sh.addShard("shard/localhost:27018")' localhost:27017 |
| 64 | + touch /var/lib/sharded-mongo/mongos.initialized |
| 65 | + |
| 66 | + for f in /docker-entrypoint-initdb.d/*; do |
| 67 | + case "$f" in |
| 68 | + *.sh) echo "$0: running $f"; . "$f" ;; |
| 69 | + *.js) echo "$0: running $f"; mongosh localhost:27017 "$f"; echo ;; |
| 70 | + *) echo "$0: ignoring $f" ;; |
| 71 | + esac |
| 72 | + done |
| 73 | +fi |
| 74 | + |
| 75 | +echo "[sharded-mongo/docker-entrypoint.sh] shutting mongos server down..." |
| 76 | + |
| 77 | +mongosh --eval 'db.shutdownServer()' localhost:27017/admin || true |
| 78 | + |
| 79 | +while pgrep mongos > /dev/null; do |
| 80 | + echo "[sharded-mongo/docker-entrypoint.sh] mongos is shutting down. sleep 1 second..." |
| 81 | + sleep 1 |
| 82 | +done |
| 83 | + |
| 84 | +exec "$@" |
0 commit comments