执行命令

  1. docker-compose -f docker-compose.yml up -d
  2. sh start.sh

docker-compose.yml 详细代码

  1. version: '2'
  2. services:
  3. shard1:
  4. image: mongo:4.0.5
  5. container_name: mongo_shard1
  6. command: mongod --shardsvr --directoryperdb --replSet shard1
  7. volumes:
  8. - /etc/localtime:/etc/localtime
  9. - /data/base/fates/mongo/shard1:/data/db
  10. privileged: true
  11. mem_limit: 16000000000
  12. networks:
  13. - mongo
  14. shard2:
  15. image: mongo:4.0.5
  16. container_name: mongo_shard2
  17. command: mongod --shardsvr --directoryperdb --replSet shard2
  18. volumes:
  19. - /etc/localtime:/etc/localtime
  20. - /data/base/fates/mongo/shard2:/data/db
  21. privileged: true
  22. mem_limit: 16000000000
  23. networks:
  24. - mongo
  25. shard3:
  26. image: mongo:4.0.5
  27. container_name: mongo_shard3
  28. command: mongod --shardsvr --directoryperdb --replSet shard3
  29. volumes:
  30. - /etc/localtime:/etc/localtime
  31. - /data/base/fates/mongo/shard3:/data/db
  32. privileged: true
  33. mem_limit: 16000000000
  34. networks:
  35. - mongo
  36. config1:
  37. image: mongo:4.0.5
  38. container_name: mongo_config1
  39. command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
  40. volumes:
  41. - /etc/localtime:/etc/localtime
  42. - /data/base/fates/mongo/config1:/data/configdb
  43. networks:
  44. - mongo
  45. config2:
  46. image: mongo:4.0.5
  47. container_name: mongo_config2
  48. command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
  49. volumes:
  50. - /etc/localtime:/etc/localtime
  51. - /data/base/fates/mongo/config2:/data/configdb
  52. networks:
  53. - mongo
  54. config3:
  55. image: mongo:4.0.5
  56. container_name: mongo_config3
  57. command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
  58. volumes:
  59. - /etc/localtime:/etc/localtime
  60. - /data/base/fates/mongo/config3:/data/configdb
  61. networks:
  62. - mongo
  63. mongos:
  64. image: mongo:4.0.5
  65. container_name: mongo_mongos
  66. command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017
  67. ports:
  68. - 27017:27017
  69. volumes:
  70. - /etc/localtime:/etc/localtime
  71. depends_on:
  72. - config1
  73. - config2
  74. - config3
  75. networks:
  76. - mongo
  77. networks:
  78. mongo:
  79. external: true

start.sh详细代码

  1. #!/bin/sh
  2. docker-compose up -d
  3. #睡眠两分钟,等待mongodb所有容器起来之后将它们配置加入分片
  4. sleep 30s
  5. docker-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"
  6. docker-compose -f docker-compose.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018"
  7. docker-compose -f docker-compose.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018"
  8. docker-compose -f docker-compose.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018"
  9. docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo"
  10. docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo"
  11. docker-compose -f docker-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"