MongoDB官方镜像
ServerA配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000
ServerB配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs1 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs1_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs1_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs2 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs2_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs2_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000
ServerC配置文件
version: '2'
services:
configsrv:
image: mongo
command: mongod --keyFile /data/configdb/mongodb-keyfile --oplogSize 1024 --replSet configrs --port 27017 --configsvr --wiredTigerCacheSizeGB 5
volumes:
- /data/configsrv_db:/data/configdb
ports:
- "27018:27017"
restart:
always
container_name:
configsrv
ulimits:
nofile:
soft: 300000
hard: 300000
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
rs1_arbiter:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 1024 --replSet rs1 --directoryperdb --port 27017 --shardsvr --wiredTigerCacheSizeGB 1 --nojournal --smallfiles
volumes:
- /data/rs1_arbiter_db:/data/db
ports:
- "27020:27017"
restart:
always
container_name:
rs1_arbiter
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000
ServerD配置文件
version: '2'
services:
rs2_node:
image: mongo
command: mongod --keyFile /data/db/mongodb-keyfile --oplogSize 10240 --replSet rs2 --directoryperdb --port 27017 --shardsvr
volumes:
- /data/rs2_node_db:/data/db
ports:
- "27019:27017"
restart:
always
container_name:
rs2_node
ulimits:
nofile:
soft: 300000
hard: 300000
router:
image: mongo
command: mongos --keyFile /data/db/mongodb-keyfile --configdb configrs/ServerA:27018,ServerB:27018,ServerC:27018
ports:
- "27017:27017"
volumes:
- /data/router_db:/data/db
restart:
always
container_name:
router
ulimits:
nofile:
soft: 300000
hard: 300000
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
重要:在初始化启动前需要去掉docker-compose.yml
配置文件中的--keyFile
参数
在ServerA、ServerB和ServerC三台服务器上运行命令:
docker-compose up -d configsrv
利用mongo连接到ServerA节点,输入以下命令创建管理用户:
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })
初始化ReplicaSet信息
rs.initiate(
{
_id: "configrs",
configsvr: true,
members: [
{ _id : 0, host : "ServerA:27018" },
{ _id : 1, host : "ServerB:27018" },
{ _id : 2, host : "ServerC:27018" }
]
}
)
重要:在初始化启动前需要去掉docker-compose.yml
配置文件中的--keyFile
参数
在ServerA和ServerB两台服务器上运行命令:docker-compose up -d rs1_node
在ServerC服务器上运行命令:docker-compose up -d rs1_arbiter
利用mongo连接到ServerA节点,创建管理用户
use admin
db.createUser({user: "mongoUserAdmin", pwd: "123456", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
db.createUser({user: "mongoRootAdmin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })
初始化ReplicaSet信息
rs.initiate(
{
_id : "rs1",
members: [
{ _id : 0, host : "ServerA:27019" },
{ _id : 1, host : "ServerB:27019" }
]
}
)
增加Arbiter节点
rs.addArb("ServerC:27020")
查看rs状态:rs.status()
与Shard1节点雷同,只需要修改对应的服务器IP
取消--keyFile
参数的注释,删掉上述创建的所有container
利用docker-compose
再次启动上面所有节点
使用命令docker-compose up -d router
在四台服务器上启动路由节点
使用mongo连接到任意一台服务器的router节点,然后执行以下命令将Shard节点加入到当前Cluster中
use admin
db.auth("" ,"" )
sh.addShard("rs1/ServerA:27019")
sh.addShard("rs2/ServerD:27019")
在对collection进行sharding之前一定要先对数据库启动sharding
sh.enableSharding("" )
sh.shardCollection( "." , { _id : "hashed" } )
官方资料:
Deploy a Sharded Cluster
Hashed Sharding