I have configured a three-node replica set in MongoDB; replication is in sync.
I am able to connect to each server individually, and using each server's private IP address I can to connect to the replica set like this:
mongo --host rs0/10.X.X.X:27017,10.X.X.X:27017,10.X.X.X:27017 -u root -p abc123 --authenticationDatabase admin
But when I try to connect to the replica set using the servers' public IP addresses, like this:
mongo --host rs0/52.X.X.X:27017,52.X.X.X:27017,52.X.X.X:27017 -u root -p abc123 --authenticationDatabase admin
I get an error: "No primary detected for set rs0".
When you connect to the replica set, you should use the same addresses (ip addresses or host names) as the nodes use for each other in the replica set configuration. For replica set deployment, you need to:
In practice, it is best not to configure your replica set using public ip addresses; it is more secure to use the private ip addresses, which helps ensure that your database can only receive connections from authorised locations.
this might be one of the reason.
You should add all 3 servers public IP in to the bind ip values in all 3 servers /etc/mongod.conf
if your are using AWS, you need to allow all this IP for inbound access to another server.
Thanks guys for responses, It needed to configure replica with public Ips in rs.config
来源:https://stackoverflow.com/questions/44513233/unable-to-connect-to-mongodb-replica-set-using-public-ip-addresses