MongoDB Atlas身份验证在Python上失败

问题描述:

我已将此Python应用程序部署在Heroku上,并且希望它连接到MongoDB Atlas集群.我使用我的字符串连接到集群,但是由于某些原因,我不断得到raise OperationFailure(msg % errmsg, code, response) pymongo.errors.OperationFailure: bad auth Authentication failed.,我检查了两次,并且用户名和密码均正确.知道为什么会这样吗?

I have deployed this Python app on Heroku and i want it to connect to a MongoDB Atlas cluster. I used my string to connect to the cluster, but for some reason i keep getting raise OperationFailure(msg % errmsg, code, response) pymongo.errors.OperationFailure: bad auth Authentication failed. I checked twice and both the user and the password are correct. Any idea on why this is happening?

from pymongo import MongoClient
import time
import random
import time
import datetime


client = MongoClient('mongodb+srv://USER:<MYPASSWORD>@test-2liju.mongodb.net/test?retryWrites=true')

db = client.one

mycol = client["tst"]


while True:


    test = int(random.randrange(-99999990,90000000,1))

    dic = {"num": test}

    result = db.tst.insert_one(dic)


    print(test)
    time.sleep(5)

愚蠢的错误,我必须键入MYPASSWORD而不是<MYPASSWORD>,而没有<>

Stupid error, i had to type MYPASSWORD instead of <MYPASSWORD>, without the <>