在httpd上通过Flask-PyMongo连接到MongoHQ URI时的ConnectionFailure

时间:2014-01-09 21:14:27

标签: python mongodb flask mod-wsgi pymongo

我有一个带有Mongo后端的Flask应用程序,可以在我的本地开发环境中完美运行。我将其移至AWS托管的RHEL实例,并通过httpd / mod_wsgi使Flask应用程序正常工作。但是,连接到MongoHQ上的数据库仍然会失败。

连接的代码是:

from flask import Flask
from flask.ext.pymongo import PyMongo

app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb://myusername:mypasswd@myhost.mongohq.com:myport/mydb'
mongo = PyMongo(app)

示例查询:

@app.route('/books')
def books()
    all_books = mongo.db.listings.distinct("bookinfo")
    return all_books

来自Apache的错误消息(为便于阅读而编辑):

mod_wsgi (pid=5116, process='myProcess', application=''): Loading WSGI script '/route/to/myapp.wsgi'. 
mod_wsgi (pid=5116): Target WSGI script '/route/to/myapp.wsgi' cannot be loaded as Python module. 
mod_wsgi (pid=5116): Exception occurred processing WSGI script '/route/to/myapp.wsgi'. 
Traceback (most recent call last): 
File "/route/to/myapp.wsgi", line 10, in <module>
     from myApp import app as application   
File "/route/to/myapp.py", line 8, in <module>
     mongo = PyMongo(app)
File "/route/to/my/venv/lib/python2.6/site-packages/flask_pymongo/__init__.py", line 98, in __init__
    self.init_app(app, config_prefix)
File "/route/to/my/venv/python2.6/site-packages/flask_pymongo/__init__.py", line 230, in init_app
     cx = connection_cls(*args, **kwargs)
File "/route/to/my/venv/python2.6/site-packages/pymongo/mongo_client.py", line 352, in __init__
     raise ConnectionFailure(str(e))
ConnectionFailure: could not connect to myhost.mongohq.com:myport: [Errno 13] Permission denied

我的想法/调查:

  1. 当我删除数据库连接时,应用运行正常:所以它不是 mod_wsgi,Apache或权限问题?
  2. 我可以通过Mongo shell和Python shell连接到mongo uri,但不能通过app连接:所以这不是防火墙问题?
  3. MongoHQ的超响应客户服务(严重的,它们很棒)认为这是Flask-PyMongo驱动程序的问题,但我不知道如何调试它。
  4. 任何帮助/提示/可能性将不胜感激。

1 个答案:

答案 0 :(得分:0)

SELinux是这个的根本原因。我已禁用防火墙,但从未通过编辑/ etc / sysconfig / selinux文件关闭SELinux。在这样做之后,应用程序仍然存在问题,但是这个日志记录代码帮助很大:

import logging
file_handler = logging.FileHandler(filename='/var/www/myapp/flaskerror.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)