On this page本页内容
For most replica sets, the hostnames in the members[n].host
field never change. 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.
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
This document provides two separate procedures for changing the hostnames in the members[n].host
field. Use either of the following approaches:
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:
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.
mongo
shell connected to the replica set’s primary. In our example, the primary runs on port 27017
so you would issue the following command:
rs.reconfig()
to update the replica set configuration document with the new hostname.
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:
For more information on updating the configuration document, see Examples.
Repeat the above steps for each non-primary member of the set.
mongo
shell connected to the primary and step down the primary using the rs.stepDown()
method:
The replica set elects another member to the become primary.
mongod
instance that will become the new primary in the new location.For example, if the old primary was at position 0
and the new primary’s hostname is mongodb0.example.net:27017
, you would run:
mongo
shell connected to the new primary.rs.conf()
in the mongo
shell.
Your output should resemble:
This procedure uses the above assumptions.
The following procedure reads and updates the system.replset
collection in the local
database.
If your deployment enforces access control, the user performing the procedure must have find
and update
privilege actions on the system.replset
collection.
To create a role that provides the necessary privileges:
userAdminAnyDatabase
role. The following procedure uses the myUserAdmin
created in Enable Access Control.
system.replset
collection in the local
database:
"userPerformingRename"
in the admin
database.
--replSet
run-time option. 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:
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
mongo
shell connected to the mongod
running on the new, temporary port. For example, for a member running on a temporary port of 37017
, you would issue this command:
If running with access control, connect as a user with appropriate privileges. See Prerequisites.
system.replset
collection in the local
database. Edit the replica set configuration with the new hostnames and correct ports for all the members of the replica set. Consider the following sequence of commands to change the hostnames in a three-member set:
mongod
process on the member.mongod
instance in the normal way: use the usual port number and use the --replSet
option. Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
mongod
instances using the mongo
shell. rs.conf()
in the mongo
shell.
Your output should resemble: