首页  :: 新随笔  :: 管理

MongoDB Sharding Cluster部署(docker方式)

Posted on 2022-01-07 16:52  高&玉  阅读(650)  评论(0编辑  收藏  举报

1 环境介绍

MongoDB集群由三部分组成:

  • mongos:应用程序的路由接口,通过mongos应用程序与整个集群是透明的,一般每个应用服务器对应一个实例,可以跟应用部署到一台服务器上。它主要读取或缓存配置服务器中元数据,提供查询路由到每个分片的功能。
  • configure servers:建议采用副本集,存储集群的元数据。很重要,影响集群的使用。
  • shard:建议采用副本集,提供数据冗余和高可用,主要存储业务数据。

2 部署MongoDB集群

2.1 准备工作

部署环境:当前环境由3台mongos、3台configure servers、3个分片集群构成。

类型 角色 默认端口 映射端口
mongos mongos0 27017 37011
mongos1 27017 37012
mongos2 27017 37013
configure servers configsvr0 27019 37021
configsvr1 27019 37022
configsvr2 27019 37023
shard0 shardsvr00 27018 37031
shardsvr01  27018 37032
shardsvr02  27018 37033
shard1 shardsvr10  27018 37041
shardsvr11  27018 37042
shardsvr12  27018 370423
shard2 shardsvr20  27018 37051
shardsvr21  27018 37052
shardsvr22  27018 37053

 

宿主机安装MongoDB Shell,后面方便管理。

 

拉取MongoDB for Docker镜像

[root]# docker pull mongo:latest

 

生成keyfile文件

[root]# mkdir -p /etc/mongodb
[root]# openssl rand -base64 765 > /etc/mongodb/key.file
[root]# chmod 400 /etc/mongodb/key.file

2.2 部署configure servers

2.2.1 生成配置文件

生成configure servers配置文件

[root]# vi /etc/mongodb/config.conf 
systemLog:
  destination: file
  path: "/data/configdb/config.log"
  logAppend: true
storage:
  dbPath: /data/configdb
  wiredTiger:                                                                                                                   
    engineConfig:                                                                                                               
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/configdb/key.file
 # authorization: enabled
replication:
  replSetName: rs_configsvr
sharding:
  clusterRole: configsvr

2.2.2 创建configure servers

创建configure servers(端口映射、文件映射是为了后期调试方便)

[root]# docker run -d --name pantao_configsvr0 -p 37021:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf
[root]# docker run -d --name pantao_configsvr1 -p 37022:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf
[root]# docker run -d --name pantao_configsvr2 -p 37023:27019 -v /etc/mongodb/config.conf:/data/configdb/config.conf -v /etc/mongodb/key.file:/data/configdb/key.file mongo --config /data/configdb/config.conf

2.2.3 创建configure servers集群

configure servers集群初始化

[root]# docker exec -it pantao_configsvr0 bash
[root]# mongo --port 27019
> rs.initiate(
  {
    _id: "rs_configsvr",
    configsvr: true,
    members: [
      { _id : 0, host : "10.150.1.107:37021" },
      { _id : 1, host : "10.150.1.107:37022" },
      { _id : 2, host : "10.150.1.107:37023" }
    ]
  }
)
输出信息:
{
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(1641429399, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"lastCommittedOpTime" : Timestamp(1641429399, 1)
}

2.3 部署shard

2.3.1 生成配置文件

生成shard(rs_shardsvr0)配置文件

[root]# vi /etc/mongodb/shard0.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr0
sharding:
  clusterRole: shardsvr

 

生成shard(rs_shardsvr1)配置文件

[root]# vi /etc/mongodb/shard1.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr1
sharding:
  clusterRole: shardsvr

 

生成shard(rs_shardsvr2)配置文件

[root]# vi /etc/mongodb/shard2.conf
systemLog:
  destination: file
  path: "/data/db/shards.log"
  logAppend: true
storage:
  dbPath: /data/db
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.25
net:
  bindIpAll: true
#security:
 # keyFile: /data/db/key.file
  #authorization: enabled
replication:
  replSetName: rs_shardsvr2
sharding:
  clusterRole: shardsvr

2.3.2 创建shard

创建shard(副本集rs_shardsvr0)

[root]# docker run -d --name pantao_shardsvr00 -p 37031:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf
[root]# docker run -d --name pantao_shardsvr01 -p 37032:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf
[root]# docker run -d --name pantao_shardsvr02 -p 37033:27018 -v /etc/mongodb/shard0.conf:/data/db/shard0.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard0.conf

 

创建shard(副本集rs_shardsvr1)

[root]# docker run -d --name pantao_shardsvr10 -p 37041:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf
[root]# docker run -d --name pantao_shardsvr11 -p 37042:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf
[root]# docker run -d --name pantao_shardsvr12 -p 37043:27018 -v /etc/mongodb/shard1.conf:/data/db/shard1.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard1.conf

 

创建sahrd(副本集rs_shardsvr2)

[root]# docker run -d --name pantao_shardsvr20 -p 37051:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf
[root]# docker run -d --name pantao_shardsvr21 -p 37052:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf
[root]# docker run -d --name pantao_shardsvr22 -p 37053:27018 -v /etc/mongodb/shard2.conf:/data/db/shard2.conf -v /etc/mongodb/key.file:/data/db/key.file mongo --config /data/db/shard2.conf

2.3.3 创建副本集

副本集rs_shardsvr0

[root]# docker exec -it pantao_shardsvr00 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr0",
    members: [
      { _id : 0, host : "10.150.1.107:37031" },
      { _id : 1, host : "10.150.1.107:37032" },
      { _id : 2, host : "10.150.1.107:37033" }
    ]
  }
)

 

副本集rs_shardsvr1

[root]# docker exec -it pantao_shardsvr10 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr1",
    members: [
      { _id : 0, host : "10.150.1.107:37041" },
      { _id : 1, host : "10.150.1.107:37042" },
      { _id : 2, host : "10.150.1.107:37043" }
    ]
  }
)

 

副本集rs_shardsvr2

[root]# docker exec -it pantao_shardsvr20 bash
[root]# mongo --port 27018
> rs.initiate(
  {
    _id : "rs_shardsvr2",
    members: [
      { _id : 0, host : "10.150.1.107:37051" },
      { _id : 1, host : "10.150.1.107:37052" },
      { _id : 2, host : "10.150.1.107:37053" }
    ]
  }
)

2.4 部署mongos

2.4.1 生成配置文件

生成mongos配置文件

[root]# vi /etc/mongodb/mongos.conf
systemLog:
  destination: file
  path: "/data/configdb/mongos.log"
  logAppend: true
net:
  bindIpAll: true
#security:
#  keyFile: /data/configdb/key.file
sharding:
  configDB: rs_configsvr/10.150.1.107:37021,10.150.1.107:37022,10.150.1.107:37023

2.4.2 创建mongos

[root]# docker run -d --name pantao_mongos0 -p 37011:27017 -v /etc/mongodb/mongos.conf:/data/configdb/mongos.conf -v /etc/mongodb/key.file:/data/configdb/key.file --entrypoint "mongos" mongo --config /data/configdb/mongos.conf
[root]# docker run -d --name pantao_mongos1 -p 37012:27017 -v /etc/mongodb/mongos.conf:/data/configdb/mongos.conf -v /etc/mongodb/key.file:/data/configdb/key.file --entrypoint "mongos" mongo --config /data/configdb/mongos.conf
[root]# docker run -d --name pantao_mongos2 -p 37013:27017 -v /etc/mongodb/mongos.conf:/data/configdb/mongos.conf -v /etc/mongodb/key.file:/data/configdb/key.file --entrypoint "mongos" mongo --config /data/configdb/mongos.conf

2.4.3 创建mongos集群

连接某个mongos,添加副本集到集群

[root]# docker exec -it pantao_mongos0 bash
[root]# mongo --port 27017

添加rs_shardsvr0副本集到mongos集群

mongos> sh.addShard("rs_shardsvr0/10.150.1.107:37031,10.150.1.107:37032,10.150.1.107:37033")
输出信息:
{
	"shardAdded" : "rs_shardsvr0",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1641438766, 3),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1641438766, 3)
}

添加rs_shardsvr1副本集到mongos集群

mongos> sh.addShard("rs_shardsvr1/10.150.1.107:37041,10.150.1.107:37042,10.150.1.107:37043")
输出信息:
{
	"shardAdded" : "rs_shardsvr1",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1641524757, 3),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1641524757, 3)
}

添加rs_shardsvr2副本集到mongos集群

mongos> sh.addShard("rs_shardsvr2/10.150.1.107:37051,10.150.1.107:37052,10.150.1.107:37053")
输出信息:
{
	"shardAdded" : "rs_shardsvr2",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1641524788, 4),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1641524788, 4)
}

2.5 查看集群

查看集群状态

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("61d70b85156918111e93774d")
  }
  shards:
        {  "_id" : "rs_shardsvr0",  "host" : "rs_shardsvr0/10.150.1.107:37031,10.150.1.107:37032,10.150.1.107:37033",  "state" : 1,  "topologyTime" : Timestamp(1641522923, 1) }
        {  "_id" : "rs_shardsvr1",  "host" : "rs_shardsvr1/10.150.1.107:37041,10.150.1.107:37042,10.150.1.107:37043",  "state" : 1,  "topologyTime" : Timestamp(1641524757, 1) }
        {  "_id" : "rs_shardsvr2",  "host" : "rs_shardsvr2/10.150.1.107:37051,10.150.1.107:37052,10.150.1.107:37053",  "state" : 1,  "topologyTime" : Timestamp(1641524788, 2) }
  active mongoses:
        "5.0.5" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled: yes
        Currently running: no
        Failed balancer rounds in last 5 attempts: 0
        Migration results for the last 24 hours: 
                36 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rs_shardsvr0	988
                                rs_shardsvr1	22
                                rs_shardsvr2	14
                        too many chunks to print, use verbose if you want to force print

 

查看是否分片集群

mongos> db.runCommand({ isdbgrid:1})
{
	"isdbgrid" : 1,
	"hostname" : "ae373393d31e",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1642129200, 3),
		"signature" : {
			"hash" : BinData(0,"Y0WGkWZbxmmAfoubXekSbw86/4E="),
			"keyId" : NumberLong("7050116407530356759")
		}
	},
	"operationTime" : Timestamp(1642129200, 3)

 

列出集群分片信息

mongos> db.runCommand({listshards:1})
{
	"shards" : [
		{
			"_id" : "rs_shardsvr0",
			"host" : "rs_shardsvr0/10.150.1.107:37031,10.150.1.107:37032,10.150.1.107:37033",
			"state" : 1,
			"topologyTime" : Timestamp(1641522923, 1)
		},
		{
			"_id" : "rs_shardsvr1",
			"host" : "rs_shardsvr1/10.150.1.107:37041,10.150.1.107:37042,10.150.1.107:37043",
			"state" : 1,
			"topologyTime" : Timestamp(1641524757, 1)
		},
		{
			"_id" : "rs_shardsvr2",
			"host" : "rs_shardsvr2/10.150.1.107:37051,10.150.1.107:37052,10.150.1.107:37053",
			"state" : 1,
			"topologyTime" : Timestamp(1641524788, 2)
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1642129217, 1),
		"signature" : {
			"hash" : BinData(0,"7DnU4vLSrERCZOmO2aX/Q5Lr8RI="),
			"keyId" : NumberLong("7050116407530356759")
		}
	},
	"operationTime" : Timestamp(1642129217, 1)
}

 

列出开启分片的数据库

admin> use config
config> db.databases.find({partitioned:true}).pretty()
{
	"_id" : "damocles",
	"primary" : "rs_shardsvr1",
	"partitioned" : true,
	"version" : {
		"uuid" : UUID("81405f21-0fcf-491b-9878-20b8da940ba8"),
		"timestamp" : Timestamp(1641530077, 1),
		"lastMod" : 1
	}
}

 

列出开启分片的数据库

config> db.databases.find().pretty()
{
	"_id" : "damocles",
	"primary" : "rs_shardsvr1",
	"partitioned" : true,
	"version" : {
		"uuid" : UUID("81405f21-0fcf-491b-9878-20b8da940ba8"),
		"timestamp" : Timestamp(1641530077, 1),
		"lastMod" : 1
	}
}

 

查看集合是否已开启分片

mongos> db.collections.stats().sharded
true

3 开启认证登录

mongos、Config server、shard端创建超级管理员,方便后期管理。

 3.1 mongos端

mongos集群创建超级管理员账号

[root]# docker exec -it pantao_mongos0 bash
[root]# mongo --port 27017
mongos> use admin
mongos> db.createUser(
   {
     user: "admin",
     pwd: "Gaoyu@029",
     roles: [ { role: "root", db: "admin" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

3.2 Config Server

Config Server集群创建超级管理员账号

[root]# docker exec -it pantao_configsvr0 bash
[root]# mongo --port 27019
mongos> use admin
mongos> db.createUser(
   {
     user: "admin",
     pwd: "Gaoyu@029",
     roles: [ { role: "root", db: "admin" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

 3.3 shard端

rs_shardsvr0副本集

[root]# docker exec -it pantao_shardsvr00 bash
[root]# mongo --port 27017
mongos> use admin
mongos> db.createUser(
   {
     user: "admin",
     pwd: "Gaoyu@029",
     roles: [ { role: "root", db: "admin" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

 

rs_shardsvr1副本集

[root]# docker exec -it pantao_shardsvr10 bash
[root]# mongo --port 27017
mongos> use admin
mongos> db.createUser(
   {
     user: "admin",
     pwd: "Gaoyu@029",
     roles: [ { role: "root", db: "admin" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

 

rs_shardsvr2副本集

[root]# docker exec -it pantao_shardsvr20 bash
[root]# mongo --port 27017
mongos> use admin
mongos> db.createUser(
   {
     user: "admin",
     pwd: "Gaoyu@029",
     roles: [ { role: "root", db: "admin" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

 3.4 关闭容器

[root]# docker stop pantao_mongos0
[root]# docker stop pantao_mongos1
[root]# docker stop pantao_mongos2
[root]# docker stop pantao_configsvr0
[root]# docker stop pantao_configsvr1
[root]# docker stop pantao_configsvr2
[root]# docker stop pantao_shardsvr00
[root]# docker stop pantao_shardsvr01
[root]# docker stop pantao_shardsvr02
[root]# docker stop pantao_shardsvr10
[root]# docker stop pantao_shardsvr11
[root]# docker stop pantao_shardsvr12
[root]# docker stop pantao_shardsvr20
[root]# docker stop pantao_shardsvr21
[root]# docker stop pantao_shardsvr22

 3.5 修改配置文件

取消配置文件中"security"的注释部分 

配置文件:

  • shard2.conf:用于副本集rs_shardsvr2
  • shard1.conf:用于副本集rs_shardsvr1
  • shard0.conf:用于副本集rs_shardsvr0
  • mongos.conf:用于mongos
  • config.conf:用于configure servers

3.6 启动容器

[root]# docker start pantao_configsvr0
[root]# docker start pantao_configsvr1
[root]# docker start pantao_configsvr2
[root]# docker start pantao_shardsvr00
[root]# docker start pantao_shardsvr01
[root]# docker start pantao_shardsvr02
[root]# docker start pantao_shardsvr10
[root]# docker start pantao_shardsvr11
[root]# docker start pantao_shardsvr12
[root]# docker start pantao_shardsvr20
[root]# docker start pantao_shardsvr21
[root]# docker start pantao_shardsvr22
[root]# docker start pantao_mongos0
[root]# docker start pantao_mongos1
[root]# docker start pantao_mongos2

3.7 权限验证

认证登录前会

[root]# docker exec -it pantao-mongos0 bash
[root]# mongo --port 27017
mongos> show users
uncaught exception: Error: not authorized on test to execute command { usersInfo: 1.0, lsid: { id: UUID("c7ac5f76-6959-4136-9960-0a50254d0f77") }, $clusterTime: { clusterTime: Timestamp(1641526208, 1), signature: { hash: BinData(0, 9598072F495067CCE08A740B958F8EECC50131FE), keyId: 7050116407530356759 } }, $db: "test" } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1635:15
shellHelper.show@src/mongo/shell/utils.js:933:9
shellHelper@src/mongo/shell/utils.js:838:15
@(shellhelp2):1:1

 

认证权限后

mongos> db.auth("root","Gaoyu@029")
1

mongos> show users
{
	"_id" : "test.root",
	"userId" : UUID("875e5c98-6aa3-4a7f-a932-086b1446daa0"),
	"user" : "root",
	"db" : "test",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

4 集群测试

连接mongos

[root]# docker exec -it pantao_mongos0 bash
[root]# mongo --port 2701

 

创建damocles库

mongos> use damocles
switched to db damocles

 

创建普通用户(damocles)

mongos> db.createUser(
   {
     user: "damocles",
     pwd: "damocles",
     roles: [ { role: "readWrite", db: "damocles" } ]
   }
   )
输出信息:
Successfully added user: {
	"user" : "damocles",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "damocles"
		}
	]
}

 

对damocles库启用分片

mongos> sh.enableSharding("damocles")
输出信息:
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1641463399, 4),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1641463399, 2)
}

 

对damocles.order表_id字段进行哈希分片

mongos> sh.shardCollection("damocles.order", {"_id": "hashed" })
输出信息:
{
	"collectionsharded" : "damocles.order",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1641467410, 39),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1641467410, 33)
}

 

插入10000条测试数据

mongos> use damocles
switched to db damocles
mongos> db.auth("damocles","damocles")	#认证登录
1

mongos> for (i = 1; i <= 10000; i=i+1){db.order.insert({'price': 1})}
WriteResult({ "nInserted" : 1 })

mongos> db.order.find().count()
10000

 

到shard副本集查看数据分布情况

rs_shardsvr0:PRIMARY> db.order.find().count()
3315

rs_shardsvr1:PRIMARY> db.order.find().count()
3318

rs_shardsvr2:PRIMARY> db.order.find().count()
3367

5 增删分片

5.1 删除分片

1. 确认balancer是否开启

mongos> sh.getBalancerState()
true

2. 查看集群状态,删除的分片id为rs_shardsvr2,同时为primary分片

3.  从集群中移除分片rs_shardsvr2,如果提示红框内的信息则表明分片primary存在未启动分片键的集合

4. 再次查看集群状态,分片rs_shardsvr2的状态为draining

5.  检查分片rs_shardsvr2上是否有未分片的集合,未启用分片键的集合存储在分片primary

mongos> db.t1.stats().sharded
false

 6. 将未分片的集合迁移到其它分片上,也可理解为切换分片primary到其他分片上 

mongos> use admin
switched to db admin
mongos> db.runCommand( {movePrimary: "damocles",to: "rs_shardsvr0"} )
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1645971821, 13),
		"signature" : {
			"hash" : BinData(0,"sGyfk/b2/PMTjZasXog7hDn+RKo="),
			"keyId" : NumberLong("7052984389122129943")
		}
	},
	"operationTime" : Timestamp(1645971821, 11)
}

 7.  再次移除分片rs_shardsvr2

mongos> db.runCommand( {removeShard: "rs_shardsvr2"} )
{
	"msg" : "removeshard completed successfully",
	"state" : "completed",
	"shard" : "rs_shardsvr2",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1645972273, 3),
		"signature" : {
			"hash" : BinData(0,"+0EzC0SZ+121I6oPaJiUsaf59Wk="),
			"keyId" : NumberLong("7052984389122129943")
		}
	},
	"operationTime" : Timestamp(1645972273, 3)
}

8. 再次查看集群状态,分片rs_shardsvr2成功移除

5.2 添加分片

如果要添加新分片,首先新分片要调试好复制集,然后再把该分片添加到集群中。

1. 查看集群状态

2. 添加rs_shardsvr2分片到集群中,添加前确保rs_shardsvr2无多于的集合

mongos> sh.addShard("rs_shardsvr2/10.150.57.13:37051,10.150.57.13:37052,10.150.57.13:37053");
{
	"shardAdded" : "rs_shardsvr2",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1645973032, 5),
		"signature" : {
			"hash" : BinData(0,"W4LgDOzDmmw7CyO7Dn7DAT8adoI="),
			"keyId" : NumberLong("7052984389122129943")
		}
	},
	"operationTime" : Timestamp(1645973032, 5)
}

3. 再次查看集群状态

等待几分钟,各分片的chunks数量会均衡。