The error message suggests your replica set config includes a localhost address, which was likely added by the default config in rs.initiate()
if you didn't explicitly specify localhost anywhere.
You can check the output of rs.conf()
to review the current replicate set configuration. Since you want to specify a public hostname I would include your desired config for rs.initiate()
rather than relying on the defaults when you initiate the replica set. I'd also recommend following the tutorials in the MongoDB documentation for your server version, as third party tutorials may be outdated or miss important details.
If you want to correct your existing configuration, you could copy the the current replica set config as reported by rs.conf()
, edit the hostname or IP address, and then pass the updated configuration document to rs.reconfig()
.
NOTE: before binding to external IPs, please review the MongoDB Security Checklist and ensure you have configured appropriate security measures like access control, authentication, network encryption (TLS/SSL), and firewall rules. Ideally you would have a VPN/VPC for private communication between your replica set members rather than directly using public networks.