无法用基本示例连接MongoDB

问题描述 投票:0回答:1

我正在使用 MongoDB 网站上的示例。这是示例的链接。 https://www.mongodb.com/docs/drivers/pymongo/

这是我的 app.py 文件中的代码。

from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi

# Replace the placeholder with your Atlas connection string
uri = "<connection string>"

# Set the Stable API version when creating a new client
client = MongoClient(uri, server_api=ServerApi('1'))
                          
# Send a ping to confirm a successful connection
try:
    client.admin.command('ping')
    print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
    print(e)

我已将

"connection string"
替换为我的 MongoDB 帐户中的连接字符串。我已将用户名和密码更改为我帐户中的凭据。 这是我收到的错误消息:

bad auth : authentication failed, full error: {'ok': 0, 'errmsg': 'bad auth : 
authentication failed', 'code': 8000, 'codeName': 'AtlasError'}

有什么建议可以解决这个问题吗?

我希望代码在运行 python app.py 后运行

python mongodb mongoose pymongo
1个回答
0
投票

我猜您使用了错误的密码。您应该使用有权访问数据库的用户的密码。我还没有足够的声誉来评论帖子,所以我从错误代码假设您正在使用 Mongo 云。

在 Atlas 中打开您的项目,然后单击左侧面板上的

Database Access
。您将看到用户列表。您可以单击
EDIT
旁边的
User Name
按钮,然后单击
Edit Password
按钮。复制密码并保存更改。然后,您可以在您的
"<connection string>"
中使用此密码以及此
User Name

或者,您可以通过单击右上角的

ADD NEW UDATABASE USER
按钮添加新用户。

作为参考,您可以查看这个答案

© www.soinside.com 2019 - 2024. All rights reserved.