带有 mongoengine 的 Flask 应用程序的 Elastic Beanstalk WSGI 部署的“您尚未定义默认连接”
Posted
技术标签:
【中文标题】带有 mongoengine 的 Flask 应用程序的 Elastic Beanstalk WSGI 部署的“您尚未定义默认连接”【英文标题】:"You have not defined a default connection" with Elastic Benstalk WSGI Deployment of Flask application with mongoengine 【发布时间】:2018-11-06 03:30:03 【问题描述】:我第一次在 Elastic Beanstalk 上部署烧瓶应用程序时遇到了困难。它无法建立与 mongodb 的连接,并出现错误“mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection”。
详情
应用程序使用烧瓶,用于 API 配置的 Connexion,mongoengine 作为 mongodb 胶水。
connexion==1.1.15
Flask==1.0.1
mongoengine==0.15.0
pymongo==3.6.1
PyJWT==1.6.1
此处显示的示例部署在 Python 3.4 上(但应用程序在 Python 3.6 以及本地以及 Elastic Beanstalk 上运行,但存在相同的问题)
当作为 python 应用程序(使用 Apache ProxyPass 配置)运行时,相同的应用程序在本地环境或 EC2 (Amazon Linux AMI) 上运行良好。
以下是相关代码:
__init__.py
import logging.config
import connexion
import mongoengine as me
from api_server import encoder
app = connexion.App(__name__, specification_dir='./config/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('api_config.yaml', arguments='title': 'My App')
logging.config.fileConfig("./config/logging.conf")
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
def main():
db = me.connect(alias="default", host="mongodb+srv://*[DB URL]*
if __name__ == '__main__':
main()
application.py
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/opt/python/current/app/')
from api_server import app as application
目录结构
Base Dir(Elastic Benstalk Root dir)
-- application.py
-- api_server
-----__init__.py
-----config
--------app_config.ymal
由服务器上的 ElasticBeanstalk 实用程序创建的 wsgi.conf apache mod_wsgi 配置:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app/>
Require all granted
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%GROUP \
python-home=/opt/python/run/venv/ \
python-path=/opt/python/current/app:/opt/python/run/venv/lib64/python3.4/site-packages:/opt/python/run/venv/lib/python3.4/site-packages user=wsgi group=wsgi \
home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>
LogFormat "%h (%X-Forwarded-Fori) %l %u %t \"%r\" %>s %b \"%Refereri\" \"%User-Agenti\"" combined
应用程序初始化并提供基本请求响应,但只有当它需要连接到 mongodb 时,才会抛出以下错误:
[Sun May 27 09:37:30.839005 2018] [:error] [pid 16181] Traceback (most recent call last):
[Sun May 27 09:37:30.839064 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 2292, in wsgi_app
[Sun May 27 09:37:30.839125 2018] [:error] [pid 16181] response = self.full_dispatch_request()
[Sun May 27 09:37:30.839174 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1815, in full_dispatch_request
[Sun May 27 09:37:30.839227 2018] [:error] [pid 16181] rv = self.handle_user_exception(e)
[Sun May 27 09:37:30.839276 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1718, in handle_user_exception
[Sun May 27 09:37:30.839325 2018] [:error] [pid 16181] reraise(exc_type, exc_value, tb)
[Sun May 27 09:37:30.839378 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/_compat.py", line 35, in reraise
[Sun May 27 09:37:30.839426 2018] [:error] [pid 16181] raise value
[Sun May 27 09:37:30.839472 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1813, in full_dispatch_request
[Sun May 27 09:37:30.839513 2018] [:error] [pid 16181] rv = self.dispatch_request()
[Sun May 27 09:37:30.839544 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1799, in dispatch_request
[Sun May 27 09:37:30.839574 2018] [:error] [pid 16181] return self.view_functions[rule.endpoint](**req.view_args)
[Sun May 27 09:37:30.839604 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 66, in wrapper
[Sun May 27 09:37:30.839633 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839661 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 122, in wrapper
[Sun May 27 09:37:30.839708 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839739 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 293, in wrapper
[Sun May 27 09:37:30.839769 2018] [:error] [pid 16181] return function(request)
[Sun May 27 09:37:30.839798 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 42, in wrapper
[Sun May 27 09:37:30.839827 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839856 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/parameter.py", line 218, in wrapper
[Sun May 27 09:37:30.839885 2018] [:error] [pid 16181] return function(**kwargs)
[Sun May 27 09:37:30.839913 2018] [:error] [pid 16181] File "/opt/python/current/app/api_server/auth/auth_service.py", line 21, in wrapper
[Sun May 27 09:37:30.839942 2018] [:error] [pid 16181] queryObj= CampaignOwner.objects(campaignOwnerId=g.campaign_owner_id)
[Sun May 27 09:37:30.839971 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
[Sun May 27 09:37:30.840000 2018] [:error] [pid 16181] queryset = queryset_class(owner, owner._get_collection())
[Sun May 27 09:37:30.840029 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 191, in _get_collection
[Sun May 27 09:37:30.840058 2018] [:error] [pid 16181] db = cls._get_db()
[Sun May 27 09:37:30.840086 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 180, in _get_db
[Sun May 27 09:37:30.840115 2018] [:error] [pid 16181] return get_db(cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME))
[Sun May 27 09:37:30.840144 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 226, in get_db
[Sun May 27 09:37:30.840173 2018] [:error] [pid 16181] conn = get_connection(alias)
[Sun May 27 09:37:30.840201 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 146, in get_connection
[Sun May 27 09:37:30.840230 2018] [:error] [pid 16181] raise MongoEngineConnectionError(msg)
[Sun May 27 09:37:30.840261 2018] [:error] [pid 16181] mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection
需要帮助找出问题所在。 1. WSGI 配置有什么遗漏吗? 2.尝试使用flask-mongoengine,app.config [“MONGODB_SETTING”],结果相同,我应该设置数据库设置吗? 3. mongo engine/pymongo如何读取默认设置 4. 我写的启动代码的方式是否还有其他问题导致了这个。
请尽可能提供帮助。尝试查找到目前为止所有可能的信息,无法纠正问题。
【问题讨论】:
这个链接应该能帮到你***.com/questions/21818610/… 这基本上是针对 Django 的,但我确实尝试过,但没有太多成功 【参考方案1】:我认为您的问题是由您构建条目脚本的方式引起的。拥有下面定义的 if 语句意味着您必须直接运行 __init__.py
才能执行 main
函数
def main():
db = me.connect(alias="default", host="mongodb+srv://*[DB URL]*")
if __name__ == '__main__':
main()
但是,在您的application.py
中,您只是在导入此模块
from api_server import app as application
因此没有调用负责连接到数据库的行。 请参阅此答案 https://***.com/a/419986/5616580 以了解此行为。
【讨论】:
谢谢,您对应用程序入口点的结构是正确的,这是问题所在。主要问题是配置的初始化不是在方法中而是在加载类 init.py 时完成的。我将它移到一个单独的方法中,以便在 main() 中调用。以上是关于带有 mongoengine 的 Flask 应用程序的 Elastic Beanstalk WSGI 部署的“您尚未定义默认连接”的主要内容,如果未能解决你的问题,请参考以下文章
使用 Flask 和 MongoEngine 开发 Tumblelog 应用程序时出错
Flask,Pymongo和Mongoengine - ImportError:无法导入名称'app'
python-flask 框架使用 flask_mongoengine