本章内容:
- 使用rs.remove()删除成员
- 使用rs.reconfig()删除成员
要删除副本集的成员,请使用以下任一过程。
一、使用rs.remove()删除成员
1.关闭要删除的成员的mongod实例。
请先用mongo shell连接到该实例,然后用db.shutdownServer()方法关闭实例。
2.连接到副本集的当前 primary副本。
要确定当前的主数据库,请在连接到副本集的任何成员时使用db.isMaster()。
3.以下面两种形式之一使用rs.remove()删除该成员:
rs.remove("mongod3.example.net:27017")
rs.remove("mongod3.example.net")
如果副本集需要选择新的主数据库,则MongoDB可能会短暂断开shell连接。然后,在这种情况下,shell会自动重新连接。即使命令成功执行,Shell也可能显示DBClientCursor::init call() failed错误信息。
二、使用rs.reconfig()删除成员
要删除成员,还可以通过手动编辑副本集配置文档完成。
1.关闭要删除的成员的mongod实例。
请先用mongo shell连接到该实例,然后用db.shutdownServer()方法关闭实例。
2.连接到副本集的当前 primary副本。
要确定当前的主数据库,请在连接到副本集的任何成员时使用db.isMaster()
3.通过运行rs.conf()方法查看当前配置文档,并确定要删除的成员在member数组中的位置:
例如:mongod_C.example.net在以下配置文件中的位置是2:
{
"_id" : "rs",
"version" : 7,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
},
{
"_id" : 2,
"host" : "mongod_C.example.net:27017"
}
]
}
4.将当前配置文档分配给变量cfg:
cfg = rs.conf()
5.修改cfg对象以删除成员。
例如:要删除mongod_C.example.net:27017,请使用以下JavaScript操作:
cfg.members.splice(2,1)
6.通过发出以下命令,用新配置覆盖副本集配置文档:
rs.reconfig(cfg)
7.要确认新配置,请发出rs.conf()。
对于上面的示例,输出为:
{
"_id" : "rs",
"version" : 8,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
}
]
}
上一篇:副本集添加成员