简体   繁体   English

pymongo.errors.ServerSelectionTimeoutError:db:27017:超时,超时:30s,

[英]pymongo.errors.ServerSelectionTimeoutError: db:27017: timed out, Timeout: 30s,

I am trying to run a Flask and MongoDb application and getting following error.我正在尝试运行 Flask 和 MongoDb 应用程序并收到以下错误。 Here are the logs that I could fetch using sudo docker logs <container-name>这是我可以使用sudo docker logs <container-name>获取的日志

    self._select_servers_loop(
  File "/usr/local/lib/python3.10/site-packages/pymongo/topology.py", line 227, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: db:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 626fc61ba785d70a66a1ea12, topology_type: Unknown, servers: [<ServerDescription ('db', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('db:27017: timed out')>]>

I get the above error in log when I try to access localhost.当我尝试访问本地主机时,我在日志中收到上述错误。 When I try to access localhost in browser I see connection was reset error and then I see the above logs being generated.当我尝试在浏览器中访问本地主机时,我看到connection was reset错误,然后我看到上面的日志正在生成。

Here is my app.py file这是我的 app.py 文件

from flask_restful import Api, Resource
import os
from pymongo import MongoClient

app = Flask(__name__)
api = Api(app)


client = MongoClient("mongodb://db:27017", port=27017, directConnection=True)
db = client.aNewDB
UserNum = db["UserNum"]

UserNum.insert_one({
    'num_of_users':0
})

class Visit(Resource):
    def get(self):
        prev_num = UserNum.find({})[0]['num_of_users']
        new_num = prev_num + 1
        UserNum.update_one({}, {"$set":{"num_of_users":new_num}})
        return str("Hello user" + str(new_num))

api.add_resource(Visit,"/")

if __name__=="__main__":
    port = int(os.environ.get('PORT',5000))
    app.run(host='0.0.0.0', port=port)

Here is my docker-compose.yml:这是我的 docker-compose.yml:

services:
  web:
    build: ./web
    ports:
      - "5000:5000"
    depends_on:
      - db
  db:
    build: ./db
    ports:
      - '27017-27019:27017-27019'

Any help much be appreciated.非常感谢任何帮助。

I was facing similar issue.我面临着类似的问题。 In my case I had connected mongoDB replica to my Local via tunneling, I could access it using the Compass application.在我的情况下,我已经通过隧道将 mongoDB 副本连接到我的本地,我可以使用 Compass 应用程序访问它。 But couldn't access it via Pymongo.但无法通过 Pymongo 访问它。 I was using Localhost:Port, user_id & password.我使用的是 Localhost:Port、user_id 和密码。 Later on just copied the connection string from compass and tried, It Worked!后来只是从指南针复制连接字符串并尝试,它工作!

client= MongoClient('mongodb://USER:PASSWORD@localhost:PORT/?authSource=admin&readPreference=secondary&directConnection=true&ssl=false') client= MongoClient('mongodb://USER:PASSWORD@localhost:PORT/?authSource=admin&readPreference=secondary&directConnection=true&ssl=false')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection denied, Timeout: 30s, - Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description: - pymongo pymongo.errors.ServerSelectionTimeoutError:hour must be in 0..23, Timeout: 30s, Topology Description: pymongo.errors.ServerSelectionTimeoutError - pymongo.errors.ServerSelectionTimeoutError 连接到 mongo 图集给出 pymongo.errors.ServerSelectionTimeoutError: localhost:27017 - Connecting to mongo atlas gives pymongo.errors.ServerSelectionTimeoutError: localhost:27017 pymongo.errors.ServerSelectionTimeoutError:本地主机:27017:[Errno 61]连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接被拒绝 - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused 烧瓶 - pymongo.errors.ServerSelectionTimeoutError - Flask - pymongo.errors.ServerSelectionTimeoutError pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] 连接拒绝 django 到 heroku - pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused django to heroku pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]无法建立连接,因为目标机器主动拒绝 - pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas - pymongo.errors.ServerSelectionTimeoutError: RepliceSetNoPrimary - MongoDB Atlas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM