gpt4 book ai didi

python - 无法对 PyMongo 数据库进行身份验证

转载 作者:可可西里 更新时间:2023-11-01 09:37:23 25 4
gpt4 key购买 nike

这是我用来尝试验证的代码:

# MongoDB connection
connection = Connection(f.config['MONGODB_HOST'], f.config['MONGODB_PORT'])
db = connection['MONGODB_DB']

# Try authenticating. This will only work in production. In development,
# MONGODB_USER and MONGODB_PASSWORD will raise KeyErrors.
try:
db.authenticate(f.config['MONGODB_USER'], f.config['MONGODB_PASSWORD'])
except KeyError:
f.logger.debug('KeyError: Not authenticating.')

# Temporary. This is just for testing purposes.
users = db.users
user = users.find_one({'username': 'BrewerOnRails'})

我的配置是从一个名为 ProductionConfig 的对象加载的:

class Config(object):
'''Default configuration object.'''
DEBUG = False
TESTING = False
PORT = int(os.environ.get('PORT', 5000))

class ProductionConfig(Config):
'''Configuration object specific to production environments.'''
REDIS_URL = os.environ.get('REDISTOGO_URL')
if REDIS_URL:
url = urlparse.urlparse(REDIS_URL)
REDIS_HOST = url.hostname
REDIS_PORT = url.port
REDIS_PASSWORD = url.password

MONGOLAB_URI = os.environ.get('MONGOLAB_URI')
if MONGOLAB_URI:
url = urlparse.urlparse(MONGOLAB_URI)
MONGODB_USER = url.username
MONGODB_PASSWORD = url.password
MONGODB_HOST = url.hostname
MONGODB_PORT = url.port
MONGODB_DB = url.path[1:]

这是我不断收到的错误:

pymongo.errors.OperationFailure: database error: unauthorized db:MONGODB_DB lock type:-1 client:10.117.107.165

PyMongo 的 _unpack_response 引发了 OperationFailure 异常helpers.py 文件中的函数,特别是第 103 和 104 行。

最佳答案

这在我看来是错误的:

db = connection['MONGODB_DB']

也许你想要

db = connection[config['MONGODB_DB']]

关于python - 无法对 PyMongo 数据库进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12644826/

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