1、规划
服务IPPort
config0172.16.1.710021
config1172.16.1.710022
config2172.16.1.710023
shardsvr00172.16.1.710031
shardsvr01172.16.1.710032
shardsvr02172.16.1.710033
shardsvr10172.16.1.710041
shardsvr11172.16.1.710042
shardsvr12172.16.1.710043
mongos0172.16.1.737017
├── config0
│ ├── config
│ │ ├── config.conf
│ │ └── Files0.key
│ └── db
├── config1
│ ├── config
│ │ ├── config.conf
│ │ └── Files0.key
│ └── db
├── config2
│ ├── config
│ │ ├── config.conf
│ │ └── Files0.key
│ └── db
├── Files0.key
├── mongos0
│ ├── config
│ │ ├── config.conf
│ │ └── Files0.key
│ └── db
├── shardsvr0
│ ├── 0
│ │ ├── backup
│ │ ├── config
│ │ └── db
│ ├── 1
│ │ ├── backup
│ │ ├── config
│ │ └── db
│ └── 2
│ ├── backup
│ ├── config
│ └── db
└── shardsvr1
├── 0
│ ├── backup
│ ├── config
│ └── db
├── 1
│ ├── backup
│ ├── config
│ └── db
└── 2
├── backup
├── config
└── db
docker run --restart=always --privileged=true -p 10021:27019 -v /data/mongo_shard/config0/config:/etc/mongod -v /data/mongo_shard/config0/db:/data/db -d --name config0 mongo:4.0.0 -f /etc/mongod/config.conf --configsvr --replSet "rs-file-server-config-server" --bind_ip_all
docker run --restart=always --privileged=true -p 10022:27019 -v /data/mongo_shard/config1/config:/etc/mongod -v /data/mongo_shard/config1/db:/data/db -d --name config1 mongo:4.0.0 -f /etc/mongod/config.conf --configsvr --replSet "rs-file-server-config-server" --bind_ip_all
docker run --restart=always --privileged=true -p 10023:27019 -v /data/mongo_shard/config2/config:/etc/mongod -v /data/mongo_shard/config2/db:/data/db -d --name config2 mongo:4.0.0 -f /etc/mongod/config.conf --configsvr --replSet "rs-file-server-config-server" --bind_ip_all
#mongo --host 172.16.1.7 --port 10021
>rs.initiate({
_id: "rs-file-server-config-server",
configsvr: true,
members: [
{ _id : 0, host : "172.16.1.7:10021" },
{ _id : 1, host : "172.16.1.7:10022" },
{ _id : 2, host : "172.16.1.7:10023" }
]
});
>rs.status()
docker run --restart=always --privileged=true -p 10031:27018 -v /data/mongo_shard/shardsvr0/0/config:/etc/mongod -v /data/mongo_shard/shardsvr0/0/db:/data/db -d --name shard00 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs0" --bind_ip_all
docker run --restart=always --privileged=true -p 10032:27018 -v /data/mongo_shard/shardsvr0/1/config:/etc/mongod -v /data/mongo_shard/shardsvr0/1/db:/data/db -d --name shard01 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs0" --bind_ip_all
docker run --restart=always --privileged=true -p 10033:27018 -v /data/mongo_shard/shardsvr0/2/config:/etc/mongod -v /data/mongo_shard/shardsvr0/2/db:/data/db -d --name shard02 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs0" --bind_ip_all
#mongo --host 172.16.1.7 --port 10031
>rs.initiate({
_id: "rs0",
members: [
{ _id : 0, host : "172.16.1.7:10031" },
{ _id : 1, host : "172.16.1.7:10032" },
{ _id : 2, host : "172.16.1.7:10033" }
]
});
>rs.status()
docker run --restart=always --privileged=true -p 10041:27018 -v /data/mongo_shard/shardsvr1/0/config:/etc/mongod -v /data/mongo_shard/shardsvr1/0/db:/data/db -d --name shard10 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs1" --bind_ip_all
docker run --restart=always --privileged=true -p 10042:27018 -v /data/mongo_shard/shardsvr1/1/config:/etc/mongod -v /data/mongo_shard/shardsvr1/1/db:/data/db -d --name shard11 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs1" --bind_ip_all
docker run --restart=always --privileged=true -p 10043:27018 -v /data/mongo_shard/shardsvr1/2/config:/etc/mongod -v /data/mongo_shard/shardsvr1/2/db:/data/db -d --name shard12 mongo:4.0.0 -f /etc/mongod/config.conf --shardsvr --replSet "rs1" --bind_ip_all
#mongo --host 172.16.1.7 --port 10041
>rs.initiate({
_id: "rs1",
members: [
{ _id : 0, host : "172.16.1.7:10041" },
{ _id : 1, host : "172.16.1.7:10042" },
{ _id : 2, host : "172.16.1.7:10043" }
]
});
>rs.status()
docker run --restart=always --privileged=true -p 37017:27017 -v /data/mongo_shard/mongos0/config:/etc/mongod -v /data/mongo_shard/mongos0/db:/data/db -d --entrypoint mongos --name mongos0 mongo:4.0.0 -f /etc/mongod/config.conf --configdb rs-file-server-config-server/172.16.1.7:10021,172.16.1.7:10022,172.16.1.7:10023 --bind_ip_all
#mongo --host 172.16.1.7 --port 37017
>sh.addShard("rs0/172.16.1.7:10031,172.16.1.7:10032,172.16.1.7:10033")
>sh.addShard("rs1/172.16.1.7:10041,172.16.1.7:10042,172.16.1.7:10043")
>sh.status()
#mongo --host 172.16.1.7 --port 37017
use admin
db.runCommand( { enablesharding :"testdb"});
db.runCommand( { shardcollection : "testdb.testcoll",key : {id: "hashed"} } )
db.testcoll.stats()
到其它mongos服务器查看