gpt4 book ai didi

python - 升级后无法连接到 MongoDB?

转载 作者:可可西里 更新时间:2023-11-01 09:56:49 26 4
gpt4 key购买 nike

我正在使用 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'

最佳答案

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

Mongoengine 还不兼容。

你可以尝试通过

来解决这个问题
pip uninstall pymongo
pip install pymongo==2.8

关于python - 升级后无法连接到 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29621527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com