Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

python - Google App Engine using a VPC connector does not connect with MongoDB

I am getting the following error when I deploy a Python application on Google App Engine with VPC connector and MongoDB with replica set.

MongoDB is on Google Compute Engine.

pymongo.errors.ServerSelectionTimeoutError: 
Could not reach any servers in [('mongodb-v44', 27017)]. 
Replica set is configured with internal hostnames or IPs?, 
Timeout: 30s, Topology Description: <TopologyDescription id:  
5ff5a4121251453cdcc1ff41, topology_type: ReplicaSetNoPrimary,  
servers: [<ServerDescription ('mongodb-v44', 27017)  
server_type: Unknown,  
rtt: None, error=AutoReconnect('mongodb-v44:27017: 
a [Errno -2] Name or service not known')>]>"

Python code:

mongo_connection = mongoengine.connect(
    db=os.getenv('DB_NAME'),
    host=os.getenv('DB_HOST_URL'),
    port=int(os.getenv('DB_PORT')),
    username=os.getenv('DB_USERNAME'),
    password=os.getenv('DB_PASSWORD'),
    authentication_source='admin',
    replicaset=os.getenv('REPLICA_SET'),
    read_preference=ReadPreference.PRIMARY_PREFERRED
)

try:
    info = mongo_connection.server_info()  # Forces a call.
except Exception:
    raise Exception("mongo server is down.")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure that the member's hostname in the replica set are accessible from the application.

Steps:

  1. check the rs.config()

  2. check the members hostname cfg.members[0].host = FQDN

  3. make sure FQDN is accessible from the application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...