执行命令
docker-compose -f docker-compose.yml up -dsh start.sh
docker-compose.yml 详细代码
version: '2'services:shard1:image: mongo:4.0.5container_name: mongo_shard1command: mongod --shardsvr --directoryperdb --replSet shard1volumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/shard1:/data/dbprivileged: truemem_limit: 16000000000networks:- mongoshard2:image: mongo:4.0.5container_name: mongo_shard2command: mongod --shardsvr --directoryperdb --replSet shard2volumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/shard2:/data/dbprivileged: truemem_limit: 16000000000networks:- mongoshard3:image: mongo:4.0.5container_name: mongo_shard3command: mongod --shardsvr --directoryperdb --replSet shard3volumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/shard3:/data/dbprivileged: truemem_limit: 16000000000networks:- mongoconfig1:image: mongo:4.0.5container_name: mongo_config1command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfilesvolumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/config1:/data/configdbnetworks:- mongoconfig2:image: mongo:4.0.5container_name: mongo_config2command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfilesvolumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/config2:/data/configdbnetworks:- mongoconfig3:image: mongo:4.0.5container_name: mongo_config3command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfilesvolumes:- /etc/localtime:/etc/localtime- /data/base/fates/mongo/config3:/data/configdbnetworks:- mongomongos:image: mongo:4.0.5container_name: mongo_mongoscommand: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017ports:- 27017:27017volumes:- /etc/localtime:/etc/localtimedepends_on:- config1- config2- config3networks:- mongonetworks:mongo:external: true
start.sh详细代码
#!/bin/shdocker-compose up -d#睡眠两分钟,等待mongodb所有容器起来之后将它们配置加入分片sleep 30sdocker-compose -f docker-compose.yaml exec config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019"docker-compose -f docker-compose.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018"docker-compose -f docker-compose.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018"docker-compose -f docker-compose.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018"docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo"docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo"docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"
