我正在使用Flask建立一个网站,我在其中使用带有MongoEngine ORM的MongoDB。为了重新开始,我现在升级了我的ubuntu 14.04开发机器上的所有apt和pip包。不幸的是,这破坏了我与MongoDB的联系:
Traceback (most recent call last):
File "./run.py", line 4, in <module>
from app import app, socketio
File "/home/kr65/beta/app/__init__.py", line 21, in <module>
mongoDb = MongoEngine(app)
File "/usr/local/lib/python2.7/dist-packages/flask_mongoengine/__init__.py", line 33, in __init__
self.init_app(app)
File "/usr/local/lib/python2.7/dist-packages/flask_mongoengine/__init__.py", line 66, in init_app
self.connection = mongoengine.connect(**conn_settings)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py", line 164, in connect
return get_connection(alias)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py", line 126, in get_connection
raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
mongoengine.connection.ConnectionError: Cannot connect to database default :
False is not a read preference.
我检查了MongoDB是否已启动:
$ sudo service mongodb status
mongodb start/running, process 781
如果我可以进入交互式命令行:
$ mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
Tue Apr 14 09:14:10.267 [initandlisten]
Tue Apr 14 09:14:10.267 [initandlisten] ** WARNING: You are running in OpenVZ. This is known to be broken!!!
Tue Apr 14 09:14:10.267 [initandlisten]
>
我没有改变任何代码或密码或类似的东西。我重新启动并重新启动了mongoDB,但没有任何作用。我的设置是这样的:
MONGODB_SETTINGS = {
'db': 'mydatabasename'
}
我实例化了这样的连接(之前有效):
app = Flask(__name__)
app.config.from_object('config')
mongoDb = MongoEngine(app)
由于我没有真正改变任何事情,我有点不确定在哪里寻找解决方案。有没有人有任何提示我怎么解决这个问题?
[编辑] 有了@lapinkoira的提示我的MongoDB现在正确启动,但我现在在查询时得到以下错误。任何想法如何解决这个?
File "/home/kr65/beta/app/views/webviews.py", line 476, in getDoc
userDoc = UserDocument.objects(id=docId).first()
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 309, in first
result = queryset[0]
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 160, in __getitem__
return queryset._document._from_son(queryset._cursor[key],
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py", line 1410, in _cursor
**self._cursor_args)
File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 924, in find
return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'snapshot'
答案 0 :(得分:6)
看起来你已经安装了pymongo 3.0。
Mongoengine还不兼容它。
您可以尝试通过
解决此问题pip uninstall pymongo
pip install pymongo==2.8
答案 1 :(得分:3)
看起来您的默认read_preference值为False,请尝试此操作,添加到您的导入:
from pymongo import read_preferences
这是你的mongo配置字典:
'read_preference': read_preferences.ReadPreference.PRIMARY
答案 2 :(得分:0)
如果使用monogengine&gt; = 3和python&gt; = 3和Ubuntu
写
/usr/local/lib/python3.5/dist-packages/mongoengine /
from pymongo import read_preferences
并在sudo gedit connection.py
的同一目录中打开终端
并在 def register_connection 中
更换
'read_preference': read_preferences.ReadPreference.PRIMARY
在函数的参数中
它就像魅力