【发布时间】:2021-09-24 00:17:57
【问题描述】:
我正在尝试将 MongoDB 连接到我的程序,但是每当我尝试将帖子添加到数据库中时,它都会给我一个错误。我认为我没有正确连接它。我见过这样的 StackOverflow 问题,但它仍然无法正常工作。我用我的密码替换了密码,用数据库名称替换了 myFirstDatabase。谁能告诉我怎么了?
import discord
from discord.ext import commands
import asyncio
import pymongo
from pymongo import MongoClient
client = commands.Bot(command_prefix="+")
cluster = pymongo.MongoClient("mongodb+srv://Hysan:myPassword@cluster0.dvbsy.mongodb.net/test?retryWrites=true&w=majority")
db = cluster["test"]
collection = db["test"]
post = {"_id": 0, "name": "username", "score": 5}
collection.insert_one(post)
@client.command()
async def add(ctx, time):
print(time)
client.run('token :)')
我不断收到此错误:
Traceback (most recent call last):
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 1394, in _get_socket
sock_info = self.sockets.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/phant/Programs/main.py", line 14, in <module>
collection.insert_one(post)
File "D:\Python\Python38-32\lib\site-packages\pymongo\collection.py", line 705, in insert_one
self._insert(document,
File "D:\Python\Python38-32\lib\site-packages\pymongo\collection.py", line 620, in _insert
return self._insert_one(
File "D:\Python\Python38-32\lib\site-packages\pymongo\collection.py", line 609, in _insert_one
self.__database.client._retryable_write(
File "D:\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1552, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "D:\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1438, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "D:\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1462, in _retry_internal
with self._get_socket(server, session) as sock_info:
File "D:\Python\Python38-32\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "D:\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1308, in _get_socket
with server.get_socket(
File "D:\Python\Python38-32\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 1331, in get_socket
sock_info = self._get_socket(all_credentials)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 1397, in _get_socket
sock_info = self.connect(all_credentials)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 1297, in connect
sock_info.check_auth(all_credentials)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 820, in check_auth
self.authenticate(credentials)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 837, in authenticate
auth.authenticate(credentials, self)
File "D:\Python\Python38-32\lib\site-packages\pymongo\auth.py", line 672, in authenticate
auth_func(credentials, sock_info)
File "D:\Python\Python38-32\lib\site-packages\pymongo\auth.py", line 590, in _authenticate_default
return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
File "D:\Python\Python38-32\lib\site-packages\pymongo\auth.py", line 333, in _authenticate_scram
res = sock_info.command(source, cmd)
File "D:\Python\Python38-32\lib\site-packages\pymongo\pool.py", line 710, in command
return command(self, dbname, spec, secondary_ok,
File "D:\Python\Python38-32\lib\site-packages\pymongo\network.py", line 158, in command
helpers._check_command_response(
File "D:\Python\Python38-32\lib\site-packages\pymongo\helpers.py", line 167, in _check_command_response
raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}
【问题讨论】:
标签: python python-3.x mongodb pymongo