一、使用rs.remove()删除成员
1.Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using the mongo shell and the db.shutdownServer() method.
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
3.Use rs.remove() in either of the following forms to remove the member:
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
3.Use rs.remove() in either of the following forms to remove the member:
rs.remove("mongod3.example.net:27017")
rs.remove("mongod3.example.net")
rs.remove("mongod3.example.net")
二、使用rs.reconfig()删除成员
1.Shut down the mongod instance for the member you wish to remove. To shut down the instance, connect using the mongo shell and the db.shutdownServer() method.
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
3.Issue the rs.conf() method to view the current configuration document and determine the position in the members array of the member to remove:
2.Connect to the replica set’s current primary. To determine the current primary, use db.isMaster() while connected to any member of the replica set.
3.Issue the rs.conf() method to view the current configuration document and determine the position in the members array of the member to remove:
例如:
{
"_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"
}
]
}
"_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"
}
]
}
将当前配置文档分配给变量:cfg = rs.conf()
修改cfg变量以删除成员:cfg.members.splice(2,1)
通过发布以下新配置来覆盖副本集配置文档:rs.reconfig(cfg)
rs.conf()
{
"_id" : "rs",
"version" : 8,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
}
]
}
修改cfg变量以删除成员:cfg.members.splice(2,1)
通过发布以下新配置来覆盖副本集配置文档:rs.reconfig(cfg)
rs.conf()
{
"_id" : "rs",
"version" : 8,
"members" : [
{
"_id" : 0,
"host" : "mongod_A.example.net:27017"
},
{
"_id" : 1,
"host" : "mongod_B.example.net:27017"
}
]
}
本文介绍两种从MongoDB副本集中删除成员的方法:使用rs.remove()直接移除成员及使用rs.reconfig()重新配置副本集以排除指定成员。文章详细展示了操作步骤与示例。
1176

被折叠的 条评论
为什么被折叠?



