docker- composition - built flask with mongoEngine,尝试连接,但返回p


pymongo.errors.ServerSelectionTimeoutError
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 6173037b75e69e32feeca143, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.7/site-packages/flask_restx/api.py", line 671, in error_router
return original_handler(f)
File "/usr/local/lib/python3.7/site-packages/flask_restx/api.py", line 669, in error_router
return self.handle_error(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.7/site-packages/flask_restx/api.py", line 671, in error_router
return original_handler(f)
File "/usr/local/lib/python3.7/site-packages/flask_restx/api.py", line 669, in error_router
return self.handle_error(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.7/site-packages/flask_restx/api.py", line 403, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask/views.py", line 83, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_restx/resource.py", line 49, in dispatch_request
resp = meth(*args, **kwargs)
File "/app/auth/controller.py", line 35, in post
return AuthService.login(login_data)
File "/app/auth/service.py", line 16, in login
user = User.objects(username='daas').first()
File "/usr/local/lib/python3.7/site-packages/mongoengine/queryset/manager.py", line 38, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 215, in _get_collection
db = cls._get_db()
File "/usr/local/lib/python3.7/site-packages/mongoengine/document.py", line 193, in _get_db
return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME))
File "/usr/local/lib/python3.7/site-packages/mongoengine/connection.py", line 364, in get_db
conn_settings["username"], conn_settings["password"], **auth_kwargs
File "/usr/local/lib/python3.7/site-packages/pymongo/database.py", line 1581, in authenticate
connect=True)
File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 810, in _cache_credentials
writable_preferred_server_selector)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 246, in select_server
address))
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 203, in select_servers
selector, server_timeout, address)
File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 220, in _select_servers_loop
(self._error_message(selector), timeout, self.description))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 6173037b75e69e32feeca143, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>

docker-compose文件
api:
build: api
image: azurecr.io/api
ports:
- "5000:5000"
environment:
- "ENV=prod"
- "DB_USERNAME=name"
- "DB_PASSWORD=password"
- "DB_HOST=mongodb"
depends_on:
- "mongodb"
mongodb:
image: mongo
container_name: mongodb
command: mongod
environment:
- "MONGO_INITDB_ROOT_USERNAME=name"
- "MONGO_INITDB_ROOT_PASSWORD=password"
volumes:
- dbdata:/data/db
volumes:
dbdata:

在代码的某个地方设置了MongoDB连接字符串。它没有像您期望的那样拾取DB_HOST(它默认为localhost)。

如果你可以编辑你的问题,添加在你正在做MongoDB连接的行,那么这将有助于。

相关内容

  • 没有找到相关文章

最新更新