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. 使用主机名而不是IP地址来跨拆分网络范围配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅使用IP地址配置的节点将无法通过启动验证,无法启动。
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.重新配置应用程序超出了本文档的范围。
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.使用最适合您的部署过程。
This procedure uses the above assumptions.本程序使用上述假设。
For each secondary in the replica set, perform the following sequence of operations:对于副本集中的每个secondary,执行以下操作序列:
Connect 将mongosh
to the replica set's primary. mongosh
连接到副本集的主副本。In our example, the primary runs on port 在示例中,主服务器在端口27017
so you would issue the following command: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 the members
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 the rs.stepDown()
method:mongosh
连接到主服务器,然后使用rs.stepDown()
方法降级主服务器:
rs.stepDown()
The replica set elects another member to the become primary.副本集选择另一个成员成为主要成员。
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 is mongodb0.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)
mongosh
to the new primary.mongosh
连接到新的主服务器。To confirm the new configuration, call 要确认新配置,请在rs.conf()
in mongosh
.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" } ] }
This procedure uses the above assumptions.本程序使用上述假设。
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 the local
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 the admin
database.admin
数据库中有一个现有用户"userPerformingRename"
。
use admin db.grantRolesToUser( "userPerformingRename", [ { role: "systemreplsetRole", db: "admin" } ] );
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
. --dbpath
,在本例中为/data/db1
。Use a command that resembles the following:使用类似于以下内容的命令:
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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)>
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. 使用主机名而不是IP地址来跨拆分网络范围配置群集。Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.从MongDB 5.0开始,仅使用IP地址配置的节点将无法通过启动验证,无法启动。
For each member of the replica set, perform the following sequence of operations:对于副本集的每个成员,执行以下操作序列:
Connect 将mongosh
to the mongod
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 the local
database.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 } )
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:例如:
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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 using mongosh
. mongosh
连接到其中一个mongod
实例。For Example:例如:
mongosh --port 27017
To confirm the new configuration, call 要确认新配置,请在rs.conf()
in mongosh
.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" } ] }