升级后无法连接到 MongoDB?

Can't connect to MongoDB after upgrading?

我正在使用 Flask 构建一个网站,其中我使用 MongoDB 和 MongoEngine ORM。为了重新开始,我现在升级了 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'

看起来你的默认 read_preference 值是 False,试试这个,添加到你的导入:

from pymongo import read_preferences

并将此添加到您的 mongo 配置字典中:

'read_preference': read_preferences.ReadPreference.PRIMARY

看起来你已经安装了 pymongo 3.0。

Mongoengine 尚不兼容。

您可以尝试通过

解决此问题
pip uninstall pymongo
pip install pymongo==2.8

如果使用 monogengine >= 3 and python >=3 and Ubuntu
from pymongo import read_preferences 在 /usr/local/lib/python3.5/dist-packages/mongoengine/

并在与 sudo gedit connection.py 相同的目录中打开终端 在 def register_connection 代替 'read_preference': read_preferences.ReadPreference.PRIMARY 在函数的参数中 而且效果很好