如何从Heroku上的Flask MongoEngine连接到Mongolab?

时间:2014-12-09 15:48:36

标签: python mongodb heroku flask mongoengine

我已经在Python Flask框架中构建了一个网站,我现在要将其部署到Heroku。在我的网站中,我使用MongoDBMongoEngine ORM。在我的config.py文件中,我目前的mongoDB定义如下:

MONGODB_SETTINGS = {
    'db': 'mymongodb'
}

在我的__init__.py我然后加载配置文件:

app.config.from_object('config')
mongoDb = MongoEngine(app)

这在我的笔记本电脑上本地运行数据库时有效。按照建议here我运行heroku config | grep MONGOLAB_URI来获取uri。作为suggested here,我尝试在当前代码中粘贴uri(我输入了用户名和密码):

MONGODB_SETTINGS = {
    'host': 'mongodb://heroku_appxxxxxxx:xxxxxxxx@ds0123456.mongolab.com:12345/heroku_appxxxxx'
}

提交并推送到heroku后,我仍然会收到应用程序错误。我的日志给出TypeError: connect() takes at least 1 argument (0 given)(完整的堆栈跟踪见下文)。所以我认为MongoEngine没有从MONGODB_SETTINGS读取主机,但我不知道如何做到这一点。我检查了the official Python example for MongoLab哪个适用于PyMongo,但它对我没有任何帮助。

有人知道如何从MongoEngine连接到Mongolab吗?欢迎所有提示!

2014-12-09T15:18:17.157377+00:00 heroku[web.1]: State changed from starting to up
2014-12-09T15:18:17.467335+00:00 app[web.1]: [2014-12-09 15:18:17 +0000] [7] [ERROR] Exception in worker process:
2014-12-09T15:18:17.467341+00:00 app[web.1]: Traceback (most recent call last):
2014-12-09T15:18:17.467343+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
2014-12-09T15:18:17.467345+00:00 app[web.1]:     worker.init_process()
2014-12-09T15:18:17.467347+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 114, in init_process
2014-12-09T15:18:17.467348+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2014-12-09T15:18:17.467350+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 66, in wsgi
2014-12-09T15:18:17.467352+00:00 app[web.1]:     self.callable = self.load()
2014-12-09T15:18:17.467373+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
2014-12-09T15:18:17.467375+00:00 app[web.1]:     return self.load_wsgiapp()
2014-12-09T15:18:17.467376+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
2014-12-09T15:18:17.467378+00:00 app[web.1]:     return util.import_app(self.app_uri)
2014-12-09T15:18:17.467379+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 356, in import_app
2014-12-09T15:18:17.467381+00:00 app[web.1]:     __import__(module)
2014-12-09T15:18:17.467382+00:00 app[web.1]:   File "/app/app/__init__.py", line 19, in <module>
2014-12-09T15:18:17.467384+00:00 app[web.1]:     mongoDb = MongoEngine(app)  # Load document store (mongodb)
2014-12-09T15:18:17.467385+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask_mongoengine/__init__.py", line 33, in __init__
2014-12-09T15:18:17.467387+00:00 app[web.1]:     self.init_app(app)
2014-12-09T15:18:17.467388+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/flask_mongoengine/__init__.py", line 66, in init_app
2014-12-09T15:18:17.467389+00:00 app[web.1]:     self.connection = mongoengine.connect(**conn_settings)
2014-12-09T15:18:17.467391+00:00 app[web.1]: TypeError: connect() takes at least 1 argument (0 given)

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案;我还需要列出数据库名称。所以下面钉了它:

MONGODB_SETTINGS = {
    'db': 'heroku_appxxxxx',
    'host': 'mongodb://heroku_appxxxxxxx:xxxxxxxx@ds0123456.mongolab.com:12345/heroku_appxxxxx'
}