App Engine, pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed"

App Engine, pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed"

我正在使用 Python 3.7 和 Flask 1.0.2

I plugged my app to mongoDB Atlas, and all works fine in local

client = pymongo.MongoClient(connector)

connector是Atlas给我的标准连接串

connector = "mongodb://xxx:<PASSWORD>@xxcluster-shard-00-00-y0phk.gcp.mongodb.net:27017,xxcluster-shard-00-01-y0phk.gcp.mongodb.net:27017,xxxcluster-shard-00-02-y0phk.gcp.mongodb.net:27017/test?ssl=true&replicaSet=xxxCluster-shard-0&authSource=admin&retryWrites=true"

当我将我的应用程序部署到 Google App Engine 标准 Python3 运行时环境时,它不起作用。有人知道这个问题吗?

代码: Appengine 日志:

本地日志(在本地正常工作):

Appengine 错误:

pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed"

The problem was the Ip Whitelist, thus I have add via the vpc peering connection with gcp.

To do simple, we can add 0.0.0.0/0 to allow access from anywhere(but, be careful)

.

我建议您使用 VPC Peering feature of Atlas 在您的网络和 mongodb 集群的网络之间设置对等连接。更安全;)

确保遵循 Private only connection 的说明。为此,您需要通过添加 -pri.

来调整 URI

示例:

old_con = "mongodb://xxx:<PASSWORD>@xxcluster-shard-00-00-y0phk.gcp.mongodb.net:27017,xxcluster-shard-00-01-y0phk.gcp.mongodb.net:27017,xxxcluster-shard-00-02-y0phk.gcp.mongodb.net:27017/test?ssl=true&replicaSet=xxxCluster-shard-0&authSource=admin&retryWrites=true"
new_con = "mongodb://xxx:<PASSWORD>@xxcluster-shard-00-00-y0phk-pri.gcp.mongodb.net:27017,xxcluster-shard-00-01-y0phk-pri.gcp.mongodb.net:27017,xxxcluster-shard-00-02-y0phk-pri.gcp.mongodb.net:27017/test?ssl=true&replicaSet=xxxCluster-shard-0&authSource=admin&retryWrites=true"

就我而言,我有 ssl=False。希望这对某人有所帮助!

可能问题出在您的 MongoDB 白名单中的网络访问配置。 添加您的 IP 后,问题应该得到解决。 请记住,IP 地址必须是 IPv4。