database
config_error
ai_generated
true
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
85%Fix Rate
87%Confidence
1Evidence
2024-01-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| MongoDB 5.0.x | active | — | — | — |
| MongoDB 6.0.x | active | — | — | — |
| MongoDB 7.0.x | active | — | — | — |
Root Cause
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中文
副本集初始化期间使用的主机名与 MongoDB 内部解析的主机名不匹配,通常由于 DNS 或 /etc/hosts 配置错误。
Official Documentation
https://www.mongodb.com/docs/manual/reference/method/rs.initiate/Workarounds
-
90% success Ensure all replica set members use FQDN that matches DNS or /etc/hosts: edit /etc/hosts to add '127.0.0.1 localhost hostname.domain.com' and use hostname.domain.com in rs.initiate().
Ensure all replica set members use FQDN that matches DNS or /etc/hosts: edit /etc/hosts to add '127.0.0.1 localhost hostname.domain.com' and use hostname.domain.com in rs.initiate().
-
85% success Reconfigure replica set using: cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);
Reconfigure replica set using: cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);
中文步骤
确保所有副本集成员使用与 DNS 或 /etc/hosts 匹配的 FQDN:编辑 /etc/hosts 添加 '127.0.0.1 localhost hostname.domain.com',并在 rs.initiate() 中使用 hostname.domain.com。
通过以下方式重新配置副本集:cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);
Dead Ends
Common approaches that don't work:
-
Changing the hostname in /etc/hostname without updating /etc/hosts
70% fail
MongoDB uses both hostname resolution and network interfaces; only updating one file leaves inconsistency.
-
Using IP addresses in replSetInitiate configuration
80% fail
IP addresses can change after reboot or network reconfiguration, causing replica set to fail on restart.