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

- **ID:** `mongodb/replica-set-hostname-mismatch`
- **领域:** mongodb
- **类别:** config_error
- **错误码:** `93`
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

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

## 版本兼容性

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

## 解决方案

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

## 无效尝试

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