93 mongodb config_error ai_generated true

MongoServerError: 主机名不匹配:期望 'host1.example.com' 但发现 'host2.example.com'

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

ID: mongodb/replica-set-hostname-mismatch

其他格式: JSON · Markdown 中文 · English
88%修复率
85%置信度
1证据数
2023-09-20首次发现

版本兼容性

版本状态引入弃用备注
mongodb-4.2 active
mongodb-4.4 active
mongodb-5.0 active
mongodb-6.0 active
mongodb-7.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 ...`

无效尝试

常见但无效的做法:

  1. 25% 失败

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

  2. 20% 失败

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

  3. 10% 失败

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