Change Hostnames in a Replica Set更改副本集中的主机名
On this page本页内容
For most replica sets, the hostnames in the 对于大多数复制副本集,members[n].host
field never change. members[n].host
字段中的主机名永远不会更改。However, if organizational needs change, you might need to migrate some or all host names.但是,如果组织需要更改,则可能需要迁移部分或全部主机名。
Always use resolvable hostnames for the value of the 始终使用可解析主机名作为副本集配置中members[n].host
field in the replica set configuration to avoid confusion and complexity.members[n].host
字段的值,以避免混淆和复杂性。
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. 若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是IP地址来跨拆分的网络范围配置群集。从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。
Overview概述
This document provides two separate procedures for changing the hostnames in the 本文档提供了两个单独的过程,用于更改members[n].host
field. members[n].host
字段中的主机名。Use either of the following approaches:使用以下任一方法:
Change hostnames without disrupting availability.在不中断可用性的情况下更改主机名。This approach ensures your applications will always be able to read and write data to the replica set, but the approach can take a long time and may incur downtime at the application layer.这种方法可确保您的应用程序始终能够向副本集读取和写入数据,但这种方法可能需要很长时间,并且可能会导致应用程序层停机。If you use the first procedure, you must configure your applications to connect to the replica set at both the old and new locations, which often requires a restart and reconfiguration at the application layer and which may affect the availability of your applications.如果使用第一个过程,则必须将应用程序配置为连接到旧位置和新位置的复制副本集,这通常需要在应用程序层重新启动和重新配置,这可能会影响应用程序的可用性。Re-configuring applications is beyond the scope of this document.重新配置应用程序超出了本文档的范围。Stop all members running on the old hostnames at once. This approach has a shorter maintenance window, but the replica set will be unavailable during the operation.立即停止所有在旧主机名上运行的成员。这种方法的维护窗口较短,但在操作过程中副本集将不可用。
Assumptions假设
Given a replica set with three members:给定一个包含三个成员的复制集:
database0.example.com:27017
(the primary)database1.example.com:27017
database2.example.com:27017
And with the following 并使用以下rs.conf()
output:rs.conf()
输出:
{
"_id" : "rs",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "database0.example.com:27017"
},
{
"_id" : 1,
"host" : "database1.example.com:27017"
},
{
"_id" : 2,
"host" : "database2.example.com:27017"
}
]
}
The following procedures change the members' hostnames as follows:以下过程将按如下方式更改成员的主机名:
mongodb0.example.net:27017
(the primary)mongodb1.example.net:27017
mongodb2.example.net:27017
Use the most appropriate procedure for your deployment.使用最适合您部署的过程。
Change Hostnames while Maintaining Replica Set Availability在保持副本集可用性的同时更改主机名
This procedure uses the above assumptions.本程序使用上述假设。
For each secondary in the replica set, perform the following sequence of operations:对于复制副本集中的每个secondary,执行以下操作序列:Stop the secondary.停止辅助。Restart the secondary at the new location.在新位置重新启动辅助设备。Connect将mongosh
to the replica set's primary. In our example, the primary runs on port27017
so you would issue the following command:mongosh
连接到复制集的主服务器。在示例中,主运行在端口27017上,因此您将发出以下命令:mongosh --port 27017
Use使用rs.reconfig()
to update the replica set configuration document with the new hostname.rs.reconfig()
使用新主机名更新副本集配置文档。For example, the following sequence of commands updates the hostname for the secondary at the array index例如,以下命令序列会更新副本集配置文档中1
of themembers
array (i.e.members[1]
) in the replica set configuration document:members
数组(即members[1]
)的数组索引1
处的辅助主机名:cfg = rs.conf()
cfg.members[1].host = "mongodb1.example.net:27017"
rs.reconfig(cfg)For more information on updating the configuration document, see Examples.有关更新配置文档的更多信息,请参阅示例。Make sure your client applications are able to access the set at the new location and that the secondary has a chance to catch up with the other members of the set.确保您的客户端应用程序能够访问新位置的集合,并且辅助应用程序有机会赶上集合的其他成员。Repeat the above steps for each non-primary member of the set.对集合中的每个非主要成员重复上述步骤。
Connect将mongosh
to the primary and step down the primary using thers.stepDown()
method:mongosh
连接到primary,并使用rs.stepDown()
方法逐步降低primary:rs.stepDown()
The replica set elects another member to the become primary.副本集选择另一个成员成为主要成员。When the step down succeeds, shut down the old primary.当逐步关闭成功时,关闭旧的主服务器。Start the在新位置启动将成为新主实例的mongod
instance that will become the new primary in the new location.mongod
实例。Connect to the current primary, which was just elected, and update the replica set configuration document with the hostname of the node that is to become the new primary.连接到刚刚选择的当前主节点,并使用要成为新主节点的节点的主机名更新副本集配置文档。For example, if the old primary was at position例如,如果旧主设备位于位置0
and the new primary's hostname ismongodb0.example.net:27017
, you would run:0
,而新主设备的主机名是mongodb0.example.net:27017
,则您将运行:cfg = rs.conf()
cfg.members[0].host = "mongodb0.example.net:27017"
rs.reconfig(cfg)Connect将mongosh
to the new primary.mongosh
连接到新的初级。To confirm the new configuration, call要确认新配置,请在rs.conf()
inmongosh
.mongosh
中调用rs.conf()
。Your output should resemble:您的输出应该类似于:{
"_id" : "rs",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017"
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017"
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017"
}
]
}
Change All Hostnames at the Same Time同时更改所有主机名
This procedure uses the above assumptions.本程序使用上述假设。
Prerequisites先决条件
The following procedure reads and updates the 以下过程读取并更新system.replset
collection in the local
database.local
数据库中的system.replset
集合。
If your deployment enforces access control, the user performing the procedure must have 如果部署强制执行访问控制,则执行该过程的用户必须对find
and update
privilege actions on the system.replset
collection.system.replset
集合具有find
和update
权限操作。
To create a role that provides the necessary privileges:要创建提供必要权限的角色,请执行以下操作:
Log in as a user with privileges to manage users and roles, such as a user with以具有管理用户和角色权限的用户身份登录,例如具有userAdminAnyDatabase
role.userAdminAnyDatabase
角色的用户。The following procedure uses the以下过程使用在启用访问控制中创建的myUserAdmin
created in Enable Access Control.myUserAdmin
。mongosh --port 27017 -u myUserAdmin --authenticationDatabase 'admin' -p
Create a user role that provides the necessary privileges on the创建一个用户角色,为system.replset
collection in thelocal
database:local
数据库中的system.replset
集合提供必要的权限:db.adminCommand( {
createRole: "systemreplsetRole",
privileges: [
{ resource: { db: "local", collection: "system.replset" }, actions: ["find","update"] }
],
roles: []
} );Grant the role to the user who will be performing the rename procedure.将角色授予将执行重命名过程的用户。For example, the following assumes an existing user例如,以下假设"userPerformingRename"
in theadmin
database.admin
数据库中有一个现有用户"userPerformingRename"
。use admin
db.grantRolesToUser( "userPerformingRename", [ { role: "systemreplsetRole", db: "admin" } ] );
Procedure过程
Stop all members in the replica set.停止复制副本集中的所有成员。Restart each member on a different port and without using the在不同的端口上重新启动每个成员,而不使用--replSet
run-time option.--replSet
运行时选项。Changing the port number during maintenance prevents clients from connecting to this host while you perform maintenance.在维护期间更改端口号可防止客户端在执行维护时连接到此主机。Use the member's usual使用成员常用的--dbpath
, which in this example is/data/db1
. Use a command that resembles the following:--dbpath
,在本例中为/data/db1
。使用类似于以下内容的命令:WarningBefore you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access.在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist.有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。mongod --dbpath /data/db1/ --port 37017 --bind_ip localhost,<hostname(s)|ip address(es)>
ImportantTo avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.若要避免由于IP地址更改而导致配置更新,请使用DNS主机名而不是IP地址。在配置复制集成员或分片集群成员时,使用DNS主机名而不是IP地址尤为重要。Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.使用主机名而不是IP地址来跨拆分的网络范围配置群集。从MongoDB 5.0开始,只配置了IP地址的节点将无法通过启动验证,也不会启动。For each member of the replica set, perform the following sequence of operations:对于复制副本集的每个成员,执行以下操作序列:Connect将mongosh
to themongod
running on the new, temporary port.mongosh
连接到在新的临时端口上运行的mongod
。For example, for a member running on a temporary port of例如,对于在37017
, you would issue this command:37017
的临时端口上运行的成员,您将发出以下命令:mongosh --port 37017
If running with access control, connect as a user with appropriate privileges.如果使用访问控制运行,请以具有适当权限的用户身份进行连接。See Prerequisites.请参阅先决条件。mongosh --port 37017 -u userPerformingRename --authenticationDatabase=admin -p
Edit the replica set configuration manually.手动编辑复制副本集配置。The replica set configuration is the only document in the副本集配置是system.replset
collection in thelocal
database.local
数据库中system.replset
集合中的唯一文档。To change the hostnames, edit the replica set configuration to provide the new hostnames and ports for all members of the replica set.要更改主机名,请编辑副本集配置,为副本集的所有成员提供新的主机名和端口。Switch to the切换到local
database.local
数据库。use local
Create a JavaScript variable for the configuration document.为配置文档创建一个JavaScript变量。Modify the value of the修改_id
field to match your replica set._id
字段的值以匹配您的复制副本集。cfg = db.system.replset.findOne( { "_id": "rs0" } )
Provide new hostnames and ports for each member of the replica set. Modify the hostnames and ports to match your replica set.为复制副本集的每个成员提供新的主机名和端口。修改主机名和端口以匹配您的复制副本集。cfg.members[0].host = "mongodb0.example.net:27017"
cfg.members[1].host = "mongodb1.example.net:27017"
cfg.members[2].host = "mongodb2.example.net:27017"Update the hostnames and ports in the更新system.replset
collection:system.replset
集合中的主机名和端口:db.system.replset.updateOne( { "_id": "rs0" }, { $set: cfg } )
Verify the changes:验证更改:db.system.replset.find( {}, { "members.host": 1 } )
Stop the停止成员上的mongod
process on the member.mongod
进程。
After re-configuring all members of the set, start each在重新配置集合的所有成员后,以正常方式启动每个mongod
instance in the normal way: use the usual port number and use the--replSet
option.mongod
实例:使用通常的端口号并使用--replSet
选项。For example:例如:WarningBefore you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access.在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist.有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。mongod --dbpath /data/db1/ --port 27017 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>
Connect to one of the使用mongod
instances usingmongosh
.mongosh
连接到其中一个mongod
实例。For example:例如:mongosh --port 27017
To confirm the new configuration, call要确认新配置,请在rs.conf()
inmongosh
.mongosh
中调用rs.conf()
。Your output should resemble:您的输出应该类似于:{
"_id" : "rs0",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017"
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017"
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017"
}
]
}