“你还没有使用mongoengine与Flarows应用程序的弹性Benstalk WSGI部署定义默认连接”

时间:2018-05-27 10:13:30

标签: python flask elastic-beanstalk mod-wsgi mongoengine

我在第一次在Elastic Beanstalk上部署flask应用程序时遇到困难。它无法与mongodb建立连接,并显示错误“mongoengine.connection.MongoEngineConnectionError:您尚未定义默认连接”。

详情

应用程序使用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上(但是应用程序在Python 3.6上以及本地以及Elastic Beanstalk上运行并且具有相同的问题集)

当作为python应用程序(使用Apache ProxyPass配置)运行时,相同的应用程序在本地环境或EC2(Amazon Linux AMI)上运行良好。

以下是相关代码:

__初始化__。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 config由服务器上的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”]得到相同的结果,还有其他方法我应该设置数据库设置吗? 3. mongo engine / pymongo如何读取默认设置 4.我编写启动代码的方式是否存在任何其他问题。

请帮助您提供任何信息。试图查找到目前为止所有可能的信息,无法纠正这个问题。

1 个答案:

答案 0 :(得分: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://stackoverflow.com/a/419986/5616580以了解此行为。