Docs HomeMongoDB Manual

Change Hostnames in a Replica Set更改副本集中的主机名

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.但是,如果组织需要更改,则可能需要迁移部分或全部主机名。

Note

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字段的值,以避免混淆和复杂性。

Important

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.本程序使用上述假设

  1. For each secondary in the replica set, perform the following sequence of operations:对于复制副本集中的每个secondary,执行以下操作序列:

    1. Stop the secondary.停止辅助。
    2. Restart the secondary at the new location.在新位置重新启动辅助设备。
    3. Connect mongosh to the replica set's primary. In our example, the primary runs on port 27017 so you would issue the following command:mongosh连接到复制集的主服务器。在示例中,主运行在端口27017上,因此您将发出以下命令:

      mongosh --port 27017
    4. 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.有关更新配置文档的更多信息,请参阅示例

    5. 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.对集合中的每个非主要成员重复上述步骤。

  2. Connect mongosh to the primary and step down the primary using the rs.stepDown() method:mongosh连接到primary,并使用rs.stepDown()方法逐步降低primary:

    rs.stepDown()

    The replica set elects another member to the become primary.副本集选择另一个成员成为主要成员。

  3. When the step down succeeds, shut down the old primary.当逐步关闭成功时,关闭旧的主服务器。
  4. Start the mongod instance that will become the new primary in the new location.在新位置启动将成为新主实例的mongod实例。
  5. 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)
  6. Connect mongosh to the new primary.mongosh连接到新的初级。
  7. 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"
    }
    ]
    }

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集合具有findupdate权限操作。

To create a role that provides the necessary privileges:要创建提供必要权限的角色,请执行以下操作:

  1. 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
  2. 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: []
    } );
  3. 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" } ] );

Procedure过程

  1. Stop all members in the replica set.停止复制副本集中的所有成员。
  2. 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。使用类似于以下内容的命令:

    Warning

    Before 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)>
    Important

    To 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地址的节点将无法通过启动验证,也不会启动。

  3. For each member of the replica set, perform the following sequence of operations:对于复制副本集的每个成员,执行以下操作序列:

    1. Connect mongosh to the mongod running on the new, temporary port. mongosh连接到在新的临时端口上运行的mongodFor 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
    2. Edit the replica set configuration manually. 手动编辑复制副本集配置。The replica set configuration is the only document in the system.replset collection in the local 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.要更改主机名,请编辑副本集配置,为副本集的所有成员提供新的主机名和端口。

      1. Switch to the local database.切换到local数据库。

        use local
      2. 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" } )
      3. 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"
      4. Update the hostnames and ports in the system.replset collection:更新system.replset集合中的主机名和端口:

        db.system.replset.updateOne( { "_id": "rs0" }, { $set: cfg } )
      5. Verify the changes:验证更改:

        db.system.replset.find( {}, { "members.host": 1 } )
    3. Stop the mongod process on the member.停止成员上的mongod进程。
  4. 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:例如:

    Warning

    Before 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)>
  5. Connect to one of the mongod instances using mongosh. 使用mongosh连接到其中一个mongod实例。For example:例如:

    mongosh --port 27017
  6. 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"
    }
    ]
    }