93 mongodb config_error ai_generated true

MongoServerError: hostname mismatch: expected 'host1.example.com' but found 'host2.example.com'

ID: mongodb/replica-set-hostname-mismatch

Also available as: JSON · Markdown · 中文
88%Fix Rate
85%Confidence
1Evidence
2023-09-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
mongodb-4.2 active
mongodb-4.4 active
mongodb-5.0 active
mongodb-6.0 active
mongodb-7.0 active

Root Cause

Replica set member hostnames in the configuration do not match the actual hostnames resolved by DNS or /etc/hosts, causing authentication or membership verification failures.

generic

中文

副本集成员配置中的主机名与 DNS 或 /etc/hosts 解析的实际主机名不匹配,导致身份验证或成员资格验证失败。

Official Documentation

https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-for-testing/

Workarounds

  1. 90% success Update the replica set configuration to match the actual hostnames using rs.reconfig(). Example: `cfg = rs.conf(); cfg.members[0].host = 'actual-hostname:27017'; rs.reconfig(cfg);`
    Update the replica set configuration to match the actual hostnames using rs.reconfig(). Example: `cfg = rs.conf(); cfg.members[0].host = 'actual-hostname:27017'; rs.reconfig(cfg);`
  2. 85% success Add entries to /etc/hosts on all replica set members to map the expected hostnames to the correct IP addresses. Example: `echo '192.168.1.10 host1.example.com' >> /etc/hosts`.
    Add entries to /etc/hosts on all replica set members to map the expected hostnames to the correct IP addresses. Example: `echo '192.168.1.10 host1.example.com' >> /etc/hosts`.
  3. 80% success If using Docker, ensure container names match the hostnames in the replica set config and that they are on the same network. Example: `docker run --name host1 --network mynet ...`
    If using Docker, ensure container names match the hostnames in the replica set config and that they are on the same network. Example: `docker run --name host1 --network mynet ...`

中文步骤

  1. Update the replica set configuration to match the actual hostnames using rs.reconfig(). Example: `cfg = rs.conf(); cfg.members[0].host = 'actual-hostname:27017'; rs.reconfig(cfg);`
  2. Add entries to /etc/hosts on all replica set members to map the expected hostnames to the correct IP addresses. Example: `echo '192.168.1.10 host1.example.com' >> /etc/hosts`.
  3. If using Docker, ensure container names match the hostnames in the replica set config and that they are on the same network. Example: `docker run --name host1 --network mynet ...`

Dead Ends

Common approaches that don't work:

  1. 25% fail

    The replica set configuration is stored in the local database and requires reconfiguration via rs.reconfig(), not a file change.

  2. 20% fail

    Using 'localhost' prevents other members from connecting; each member must use a reachable hostname.

  3. 10% fail

    This corrupts the replica set state and requires a full re-initialization, causing data loss.