# 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`
- **Domain:** database
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 5.0.x | active | — | — |
| MongoDB 6.0.x | active | — | — |
| MongoDB 7.0.x | active | — | — |

## Workarounds

1. **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().** (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().
   ```
2. **Reconfigure replica set using: cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);** (85% success)
   ```
   Reconfigure replica set using: cfg = rs.conf(); cfg.members[0].host = 'correctHostname:27017'; rs.reconfig(cfg);
   ```

## Dead Ends

- **Changing the hostname in /etc/hostname without updating /etc/hosts** — MongoDB uses both hostname resolution and network interfaces; only updating one file leaves inconsistency. (70% fail)
- **Using IP addresses in replSetInitiate configuration** — IP addresses can change after reboot or network reconfiguration, causing replica set to fail on restart. (80% fail)
