database config_error ai_generated true

MongoServerError:副本集成员 127.0.0.1:27017 解析为 192.168.1.1:27017,但 replSetInitiate 期望主机名为 'localhost'

MongoServerError: Replica set member 127.0.0.1:27017 resolves to 192.168.1.1:27017 but replSetInitiate expects hostname 'localhost'

ID: database/mongodb-replica-set-hostname-mismatch

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

版本兼容性

版本状态引入弃用备注
MongoDB 5.0.x active
MongoDB 6.0.x active
MongoDB 7.0.x active

根因分析

副本集初始化期间使用的主机名与 MongoDB 内部解析的主机名不匹配,通常由于 DNS 或 /etc/hosts 配置错误。

English

The hostname used during replica set initialization does not match the hostname that MongoDB resolves internally, often due to DNS or /etc/hosts misconfiguration.

generic

官方文档

https://www.mongodb.com/docs/manual/reference/method/rs.initiate/

解决方案

  1. 确保所有副本集成员使用与 DNS 或 /etc/hosts 匹配的 FQDN:编辑 /etc/hosts 添加 '127.0.0.1 localhost hostname.domain.com',并在 rs.initiate() 中使用 hostname.domain.com。
  2. 通过以下方式重新配置副本集:cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);

无效尝试

常见但无效的做法:

  1. Changing the hostname in /etc/hostname without updating /etc/hosts 70% 失败

    MongoDB uses both hostname resolution and network interfaces; only updating one file leaves inconsistency.

  2. Using IP addresses in replSetInitiate configuration 80% 失败

    IP addresses can change after reboot or network reconfiguration, causing replica set to fail on restart.