mongoengine连接错误:“False is not a read preference”解决方法

问题出现:

Mongoengine是一个可以操作MongoDB数据库的对象-文档映射器(Object-Document Mapper)。出于工作需要,最近在使用MongoDB+Django实现一个数据录入系统小网站的过程中,出现通过Mongoengine无法从 Python 调用 MongoDB,在connect()数据库一步的时候出现下面错误:

>>> from mongoengine import connect

>>> connect('healthmanager')

Traceback (most recent call last):

  File "<console>", line 1, in <module>

  File "/usr/local/python2.7/lib/python2.7/site-packages/mongoengine/connection.py", line 164, in connect

    return get_connection(alias)

  File "/usr/local/python2.7/lib/python2.7/site-packages/mongoengine/connection.py", line 126, in get_connection

    raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))

ConnectionError: Cannot connect to database default :

False is not a read preference.

 

其原因在于,通过“pip install mongoengine”安装的mongoengine为0.9.0版本,同时该命令把pymongo-3.0.2的模块依赖也装好了,而pymongo-3.0.2跟mongoengine-0.9.0之间存在兼容问题,从而导致默认的数据库无法connect成功。

>>> import pymongo

>>> pymongo.version

'3.0.2'

另外,CentOS 6.5下用“pip install pymongo”安装的pymongo为3.0.2版本,而mongoengine作为pymongo的一个扩展包,是没有安装的。因此在使用mongoengine的时候,安装完mongoengine后,需要降低pymongo版本至3.0以下。、

 

解决方法:

=> pip install mongoengine    # 安装mongoengine

=> pip uninstall pymongo      # 卸载pymongo-3.0.2

=> pip install pymongo==2.8   # 安装pymongo-2.8

重新connect:

>>> from mongoengine import connect

>>> connect('healthmanager')

MongoClient('localhost', 27017)

 

ok,问题解决!!!

你可能感兴趣的:(Preference)