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

- **ID:** `mongodb/replica-set-hostname-mismatch`
- **Domain:** mongodb
- **Category:** config_error
- **Error Code:** `93`
- **Verification:** ai_generated
- **Fix Rate:** 88%

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## Workarounds

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);`** (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);`
   ```
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`.** (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`.
   ```
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 ...`** (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 ...`
   ```

## Dead Ends

- **** — The replica set configuration is stored in the local database and requires reconfiguration via rs.reconfig(), not a file change. (25% fail)
- **** — Using 'localhost' prevents other members from connecting; each member must use a reachable hostname. (20% fail)
- **** — This corrupts the replica set state and requires a full re-initialization, causing data loss. (10% fail)
