I have a Flask application with a Mongo backend that works fine in my local environment. I moved it to an RHEL instance hosted on AWS and got a Flask application running via httpd / mod_wsgi. However, the database connection on MongoHQ continues to fail.
Code for connection:
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)
Request example:
@app.route('/books')
def books()
all_books = mongo.db.listings.distinct("bookinfo")
return all_books
Error message from Apache (edited for readability):
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
My thoughts / investigation:
- The application works fine when I delete the database connection: so is it not mod_wsgi, Apache or permission?
- I can connect to the mongo-uri through the Mongo shell and the Python shell, but not through the application: so this is not a firewall problem?
- MongoHQ ultraresponsive (, ) , Flask-PyMongo, , .
// .