MongoEngine - 另一个用户已通过此数据库的身份验证。您必须先退出

MongoEngine - Another user is already authenticated to this database. You must logout first

任何人都可以解释为什么我收到错误 另一个用户已经通过此数据库的身份验证。使用 Flask MongoEngine?

连接到 MongoDB 时,您必须先注销
from mongoengine.connection import get_db
from flask import Flask, jsonify, abort
from flask_cors import CORS
from flask_mongoengine import MongoEngine
from flask_restful import Api

def init_db():
    return MongoEngine()

app = Flask(__name__)
CORS(app)
api = Api(app)
app.config.from_object('conf.settings')
db = init_db()
db.init_app(app)

@app.route('/health_check')
def on_health_check():
    try:
        db = get_db()
        db.command('dbstats')

        return jsonify(
            status=200
        )
    except Exception as e:
        logging.exception('on_health_check() exception -> {}'.format(str(e)))
        abort(500, 'Could not connect to database')


app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)

conf/settings.py:

MONGODB_SETTINGS = {
    'host': 'mongodb://username:password@mongo-rep-mongodb-replicaset-0.local:27017,mongo-rep-mongodb-replicaset-1.local:27017/db_name?replicaSet=whatever'
}

当我转到 http://localhost:5000/health_check 时,它总是抛出 Exception 和我上面描述的消息。

所以我今天 运行 遇到了同样的问题,但最终通过安装以前版本的 pymongo 解决了它,例如pip install pymongo==3.4.0 而不是最新版本3.7.0。可能有错误...

[root@vm-lw-basic-idc-monitor-p01 home]# pip show

Metadata-Version: 2.0 Name: pymongo Version: 3.7.0 Summary: Python driver for MongoDB <http://www.mongodb.org> Home-page: http://github.com/mongodb/mongo-python-driver Author: Bernie Hackett Author-email: bernie@mongodb.com Installer: pip License: Apache License, Version 2.0 Location: /usr/lib64/python2.7/site-packages Requires: Classifiers: Development Status :: 5 - Production/Stable Intended Audience :: Developers License :: OSI Approved :: Apache Software License Operating System :: MacOS :: MacOS X Operating System :: Microsoft :: Windows Operating System :: POSIX Programming Language :: Python :: 2 Programming Language :: Python :: 2.6 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: PyPy opic :: Database You are using pip version 8.1.2, however version 10.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

pip install pymongo-3.5

我卸载之前的pymongo-3.7就好了,安装原先的3.5就好用的。。。。

就我而言,我将 mongo 从标准集更改为副本集。 重新启动应用程序后,我所有的应用程序机器都开始抛出此错误。

Another user is already authenticated to this database. You must logout first

当我重新启动我的应用程序机器时。它运行良好。

很奇怪,但发在这里是因为它可以帮助别人。