1. 程式人生 > 實用技巧 >docker搭建mongo shard

docker搭建mongo shard

基本引數

mongod

* --wiredTigerCacheSizeGB arg      maximum amount of memory to allocate for cache; defaults to 1/2 of physical RAM 
*  --shardsvr                            declare this is a shard db of a cluster; default port 27018
*  --configsvr                           declare this is a config db of a cluster; default port 27019; default dir /data/configdb
*  --replSet arg 
*  --oplogSize arg    
* --bind_ip_all                         bind to all ip addresses
*  --oplogSize arg                   size to use (in MB) for replication op 
mongos

* --configdb 

shardsvr,default 27018

docker run --network host --name shard1 --entrypoint mongod -d  mongo:latest --bind_ip_all --shardsvr --replSet shard --port 27018

docker run --network host --name shard2 --entrypoint mongod -d  mongo:latest --bind_ip_all --shardsvr --replSet shard --port 37018

docker run --network host --name shard3 --entrypoint mongod -d  mongo:latest --bind_ip_all --shardsvr --replSet shard --port 47018

replset shard 

rs.initiate(
   {
      _id: "shard",
      version: 1,
      members: [
         { _id: 0, host : "127.0.0.1:27018", priority:300},
         { _id: 1, host : "127.0.0.1:37018" , priority:200},
         { _id: 2, host : "127.0.0.1:47018" , priority:100}
      ]
   }
)

configsvr,default 27019

docker run --network host --name config1 --entrypoint mongod -d  mongo:latest --bind_ip_all --configsvr --replSet config --port 27019

docker run --network host --name config2 --entrypoint mongod -d  mongo:latest --bind_ip_all --configsvr --replSet config --port 37019

docker run --network host --name config3 --entrypoint mongod -d  mongo:latest --bind_ip_all --configsvr --replSet config --port 47019

replset config 

rs.initiate(
   {
      _id: "config",
      version: 1,
      members: [
         { _id: 0, host : "127.0.0.1:27019" ,priority:300},
         { _id: 1, host : "127.0.0.1:37019" ,priority:200},
         { _id: 2, host : "127.0.0.1:47019",priority:100 }
      ]
   }
)

mongos,default 27017

docker run --network host --name mongos --entrypoint mongos -d  mongo:latest --bind_ip_all --configdb config/127.0.0.1:27019,127.0.0.1:37019,127.0.0.1:47019


sh.addShard("shard/127.0.0.1:27018,127.0.0.1:37018,127.0.0.1:47018")
sh.addShardTag("shard", "myshard")


sh.enableSharding("test")