Unable to connect to MongoDB Replica Set using pub

2019-03-06 00:31发布

问题:

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

回答1:

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:

Ensure that network traffic can pass between all members of the set and all clients in the network securely and efficiently.

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.



回答2:

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.



回答3:

Thanks guys for responses, It needed to configure replica with public Ips in rs.config