├── scripts ├── init-shard01.js ├── init-shard02.js ├── init-shard03.js ├── init-configserver.js ├── init-router.js ├── wait-connection.sh └── init-sharding.js ├── readme.md ├── .gitignore ├── LICENSE ├── init.sh └── docker-compose.yml /scripts/init-shard01.js: -------------------------------------------------------------------------------- 1 | rs.initiate({ 2 | _id: 'rs-shard-01', 3 | version: 1, 4 | members: [ 5 | { _id: 0, host: 'shard01-a:27017' }, 6 | { _id: 1, host: 'shard01-b:27017' }, 7 | { _id: 2, host: 'shard01-c:27017' } 8 | ] 9 | }); 10 | -------------------------------------------------------------------------------- /scripts/init-shard02.js: -------------------------------------------------------------------------------- 1 | rs.initiate({ 2 | _id: 'rs-shard-02', 3 | version: 1, 4 | members: [ 5 | { _id: 0, host: 'shard02-a:27017' }, 6 | { _id: 1, host: 'shard02-b:27017' }, 7 | { _id: 2, host: 'shard02-c:27017' } 8 | ] 9 | }); 10 | -------------------------------------------------------------------------------- /scripts/init-shard03.js: -------------------------------------------------------------------------------- 1 | rs.initiate({ 2 | _id: 'rs-shard-03', 3 | version: 1, 4 | members: [ 5 | { _id: 0, host: 'shard03-a:27017' }, 6 | { _id: 1, host: 'shard03-b:27017' }, 7 | { _id: 2, host: 'shard03-c:27017' } 8 | ] 9 | }); 10 | -------------------------------------------------------------------------------- /scripts/init-configserver.js: -------------------------------------------------------------------------------- 1 | rs.initiate({ 2 | _id: 'rs-config-server', 3 | configsvr: true, 4 | version: 1, 5 | members: [ 6 | { _id: 0, host: 'configsvr01:27017' }, 7 | { _id: 1, host: 'configsvr02:27017' }, 8 | { _id: 2, host: 'configsvr03:27017' } 9 | ] 10 | }); 11 | -------------------------------------------------------------------------------- /scripts/init-router.js: -------------------------------------------------------------------------------- 1 | sh.addShard('rs-shard-01/shard01-a:27017'); 2 | sh.addShard('rs-shard-01/shard01-b:27017'); 3 | sh.addShard('rs-shard-01/shard01-c:27017'); 4 | sh.addShard('rs-shard-02/shard02-a:27017'); 5 | sh.addShard('rs-shard-02/shard02-b:27017'); 6 | sh.addShard('rs-shard-02/shard02-c:27017'); 7 | sh.addShard('rs-shard-03/shard03-a:27017'); 8 | sh.addShard('rs-shard-03/shard03-b:27017'); 9 | sh.addShard('rs-shard-03/shard03-c:27017'); 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Mongo Sharded Cluster with Docker Compose 2 | 3 | Forked from https://github.com/minhhungit/mongodb-cluster-docker-compose/ 4 | 5 | The following command runs the following procedures: 6 | 7 | - Starts mongodb cluster with docker-compose 8 | - Setups MongoDB config replica set 9 | - Setups MongoDB shard replicas 10 | - Setups MongoDB router 11 | - Sets up WildDuck sharding 12 | 13 | ``` 14 | $ ./init.sh 15 | ``` 16 | 17 | Next, edit WildDuck database config with the following options: 18 | 19 | ```toml 20 | mongo = "mongodb://127.0.0.1:27117/wildduck" 21 | gridfs = "attachments" 22 | users = "users" 23 | sender = "zonemta" 24 | ``` 25 | -------------------------------------------------------------------------------- /scripts/wait-connection.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | printf "Waiting for a mongo connection to $1..." 4 | 5 | max_attempts=100 6 | 7 | # Set a counter for the number of attempts 8 | attempt_num=1 9 | 10 | # Set a flag to indicate whether the command was successful 11 | success=false 12 | 13 | while [ $success = false ] && [ $attempt_num -le $max_attempts ]; do 14 | mongo --quiet --eval "printjson('connected')" >/dev/null 2>/dev/null 15 | if [ $? -eq 0 ]; then 16 | # The command was successful 17 | success=true 18 | echo "connected" 19 | else 20 | attempt_num=$(( attempt_num + 1 )) 21 | printf . 22 | sleep 1 23 | fi 24 | done 25 | 26 | -------------------------------------------------------------------------------- /scripts/init-sharding.js: -------------------------------------------------------------------------------- 1 | sh.enableSharding("wildduck"); 2 | // consider using mailbox:hashed for messages only with large shard chunk size 3 | sh.shardCollection("wildduck.messages", { mailbox: 1, uid: 1 }); 4 | sh.shardCollection("wildduck.archived", { user: 1, _id: 1 }); 5 | sh.shardCollection("wildduck.threads", { user: "hashed" }); 6 | sh.shardCollection("wildduck.authlog", { user: "hashed" }); 7 | 8 | sh.enableSharding("attachments"); 9 | // attachment _id is a sha256 hash of attachment contents 10 | sh.shardCollection("attachments.attachments.files", { _id: "hashed" }); 11 | sh.shardCollection("attachments.attachments.chunks", { files_id: "hashed" }); 12 | 13 | // storage _id is an ObjectId 14 | sh.shardCollection("attachments.storage.files", { _id: "hashed" }); 15 | sh.shardCollection("attachments.storage.chunks", { files_id: "hashed" }); 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | /client/.vs/DemoMongoCluster 6 | /client/DemoMongoClusterReader/bin/Debug/netcoreapp2.2 7 | /client/DemoMongoClusterReader/obj 8 | /client/DemoMongoClusterWriter/bin/Debug/netcoreapp2.2 9 | /client/DemoMongoClusterWriter/obj 10 | /client/MongoCluster.Messages/bin/Debug/netstandard2.0 11 | /client/MongoCluster.Messages/obj 12 | /client/DemoMongoClusterReader/bin/Release/netcoreapp2.2 13 | /client/DemoMongoClusterWriter/bin/Release/netcoreapp2.2 14 | /client/MongoCluster.Messages/bin/Release/netstandard2.0 15 | /client/.vs/ProjectEvaluation 16 | /client/DemoMongoClusterReader/bin/Debug/net5.0 17 | /client/DemoMongoClusterWriter/bin/Debug/net5.0 18 | /client/MongoCluster.Messages/bin/Debug/netstandard2.1 19 | docs 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -u 4 | 5 | echo "Running docker compose" 6 | docker-compose up -d 7 | 8 | 9 | # Mongo Config replica set (1 node) 10 | echo "Configuring MongoDB config replica" 11 | docker-compose exec configsvr01 sh -c "/scripts/wait-connection.sh configsvr01" 12 | docker-compose exec configsvr01 sh -c "mongo --quiet < /scripts/init-configserver.js" 13 | 14 | # Mongo shards (1 node each) 15 | echo "Configuring MongoDB shard replicas" 16 | 17 | docker-compose exec shard01-a sh -c "/scripts/wait-connection.sh shard01-a" 18 | docker-compose exec shard01-a sh -c "mongo --quiet < /scripts/init-shard01.js" 19 | 20 | docker-compose exec shard02-a sh -c "/scripts/wait-connection.sh shard02-a" 21 | docker-compose exec shard02-a sh -c "mongo --quiet < /scripts/init-shard02.js" 22 | 23 | docker-compose exec shard03-a sh -c "/scripts/wait-connection.sh shard03-a" 24 | docker-compose exec shard03-a sh -c "mongo --quiet < /scripts/init-shard03.js" 25 | 26 | # Initialize Mongo router 27 | echo "Configuring MongoDB router" 28 | 29 | docker-compose exec router01 sh -c "/scripts/wait-connection.sh router01" 30 | docker-compose exec router01 sh -c "mongo --quiet < /scripts/init-router.js" 31 | 32 | echo "Configuring MongoDB WildDuck sharding" 33 | docker-compose exec router01 sh -c "mongo --quiet < /scripts/init-sharding.js" 34 | 35 | echo "Checking sharding status" 36 | docker-compose exec router01 sh -c "mongo --eval 'sh.status()'" 37 | 38 | echo "All done" -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | ## Router 4 | router01: 5 | image: mongo:4.4.24 6 | container_name: router-01 7 | command: mongos --port 27017 --configdb rs-config-server/configsvr01:27017,configsvr02:27017,configsvr03:27017 --bind_ip_all 8 | ports: 9 | - 27117:27017 10 | volumes: 11 | - ./scripts:/scripts 12 | router02: 13 | image: mongo:4.4.24 14 | container_name: router-02 15 | command: mongos --port 27017 --configdb rs-config-server/configsvr01:27017,configsvr02:27017,configsvr03:27017 --bind_ip_all 16 | volumes: 17 | - ./scripts:/scripts 18 | ports: 19 | - 27118:27017 20 | links: 21 | - router01 22 | 23 | ## Config Servers 24 | configsvr01: 25 | image: mongo:4.4.24 26 | container_name: mongo-config-01 27 | command: mongod --port 27017 --configsvr --replSet rs-config-server 28 | volumes: 29 | - ./scripts:/scripts 30 | ports: 31 | - 27119:27017 32 | links: 33 | - shard01-a 34 | - shard02-a 35 | - shard03-a 36 | configsvr02: 37 | image: mongo:4.4.24 38 | container_name: mongo-config-02 39 | command: mongod --port 27017 --configsvr --replSet rs-config-server 40 | volumes: 41 | - ./scripts:/scripts 42 | ports: 43 | - 27120:27017 44 | links: 45 | - configsvr01 46 | configsvr03: 47 | image: mongo:4.4.24 48 | container_name: mongo-config-03 49 | command: mongod --port 27017 --configsvr --replSet rs-config-server 50 | volumes: 51 | - ./scripts:/scripts 52 | ports: 53 | - 27121:27017 54 | links: 55 | - configsvr02 56 | 57 | ## Shards 58 | ## Shards 01 59 | 60 | shard01-a: 61 | image: mongo:4.4.24 62 | container_name: shard-01-node-a 63 | command: mongod --port 27017 --shardsvr --replSet rs-shard-01 64 | volumes: 65 | - ./scripts:/scripts 66 | ports: 67 | - 27122:27017 68 | links: 69 | - shard01-b 70 | - shard01-c 71 | shard01-b: 72 | image: mongo:4.4.24 73 | container_name: shard-01-node-b 74 | command: mongod --port 27017 --shardsvr --replSet rs-shard-01 75 | volumes: 76 | - ./scripts:/scripts 77 | ports: 78 | - 27123:27017 79 | shard01-c: 80 | image: mongo:4.4.24 81 | container_name: shard-01-node-c 82 | command: mongod --port 27017 --shardsvr --replSet rs-shard-01 83 | volumes: 84 | - ./scripts:/scripts 85 | ports: 86 | - 27124:27017 87 | 88 | ## Shards 02 89 | shard02-a: 90 | image: mongo:4.4.24 91 | container_name: shard-02-node-a 92 | command: mongod --port 27017 --shardsvr --replSet rs-shard-02 93 | volumes: 94 | - ./scripts:/scripts 95 | ports: 96 | - 27125:27017 97 | links: 98 | - shard02-b 99 | - shard02-c 100 | shard02-b: 101 | image: mongo:4.4.24 102 | container_name: shard-02-node-b 103 | command: mongod --port 27017 --shardsvr --replSet rs-shard-02 104 | volumes: 105 | - ./scripts:/scripts 106 | ports: 107 | - 27126:27017 108 | shard02-c: 109 | image: mongo:4.4.24 110 | container_name: shard-02-node-c 111 | command: mongod --port 27017 --shardsvr --replSet rs-shard-02 112 | volumes: 113 | - ./scripts:/scripts 114 | ports: 115 | - 27127:27017 116 | 117 | ## Shards 03 118 | shard03-a: 119 | image: mongo:4.4.24 120 | container_name: shard-03-node-a 121 | command: mongod --port 27017 --shardsvr --replSet rs-shard-03 122 | volumes: 123 | - ./scripts:/scripts 124 | ports: 125 | - 27128:27017 126 | links: 127 | - shard03-b 128 | - shard03-c 129 | shard03-b: 130 | image: mongo:4.4.24 131 | container_name: shard-03-node-b 132 | command: mongod --port 27017 --shardsvr --replSet rs-shard-03 133 | volumes: 134 | - ./scripts:/scripts 135 | ports: 136 | - 27129:27017 137 | shard03-c: 138 | image: mongo:4.4.24 139 | container_name: shard-03-node-c 140 | command: mongod --port 27017 --shardsvr --replSet rs-shard-03 141 | volumes: 142 | - ./scripts:/scripts 143 | ports: 144 | - 27130:27017 145 | --------------------------------------------------------------------------------