"You have not defined a default connection" 使用 Elastic Beanstalk WSGI 使用 mongoengine 部署 Flask 应用程序
"You have not defined a default connection" with Elastic Benstalk WSGI Deployment of Flask application with mongoengine
我第一次在 Elastic Beanstalk 上部署 Flask 应用程序时遇到了困难。它无法建立与 mongodb 的连接,并出现错误 "mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection"。
详情
应用程序使用 flask,Connexion 用于 API 配置,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 上(但应用程序 运行s 在 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
wsgi.conf apache mod_wsgi ElasticBeanstalk 实用程序在服务器上创建的配置:
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-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" 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"] 得到相同的结果,还有其他方法可以设置 DB 设置吗?
3. mongo engine/pymongo 如何读取默认设置
4. 是不是我写的启动代码有其他问题导致了这个问题
请提供您能提供的任何信息。目前已尝试查找所有可能的信息,无法解决问题。
我认为您的问题是由您构建入口脚本的方式引起的。在下面定义 if 语句意味着您必须 运行 __init__.py
直接执行 main
function
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
所以负责连接数据库的线路没有被调用。
请参阅此答案 以了解此行为。
我第一次在 Elastic Beanstalk 上部署 Flask 应用程序时遇到了困难。它无法建立与 mongodb 的连接,并出现错误 "mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection"。
详情
应用程序使用 flask,Connexion 用于 API 配置,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 上(但应用程序 运行s 在 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
wsgi.conf apache mod_wsgi ElasticBeanstalk 实用程序在服务器上创建的配置:
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-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" 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"] 得到相同的结果,还有其他方法可以设置 DB 设置吗? 3. mongo engine/pymongo 如何读取默认设置 4. 是不是我写的启动代码有其他问题导致了这个问题
请提供您能提供的任何信息。目前已尝试查找所有可能的信息,无法解决问题。
我认为您的问题是由您构建入口脚本的方式引起的。在下面定义 if 语句意味着您必须 运行 __init__.py
直接执行 main
function
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
所以负责连接数据库的线路没有被调用。 请参阅此答案 以了解此行为。