简体   繁体   English

为什么我的 MongoDB 没有连接到我的 python 程序?

[英]Why isn't my MongoDB connecting to my python program?

I am trying to connect MongoDB to my program, but whenever I try adding a post into the database it gives me an error.我正在尝试将 MongoDB 连接到我的程序,但是每当我尝试将帖子添加到数据库中时,它都会给我一个错误。 I don't think I'm connecting it properly.我认为我没有正确连接它。 I've seen a StackOverflow question just like this, but it still isn't working.我见过这样的 StackOverflow 问题,但它仍然无法正常工作。 I've replaced the password with my password and myFirstDatabase with the database name.我用我的密码替换了密码,用数据库名称替换了 myFirstDatabase。 Can someone tell me what's wrong?有人可以告诉我有什么问题吗?

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 :)')

I keep getting this error:我不断收到此错误:

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'}

Your problems is bad authentication:您的问题是身份验证错误:

pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}

Are you using your username and password from Atlas as credentials?您是否使用 Atlas 的用户名和密码作为凭据?

You need to create a database user to grant an application or user, access to databases and collections in your clusters in this Atlas project.您需要创建一个数据库用户来授予应用程序或用户访问此 Atlas 项目中集群中的数据库和集合的权限。 Go to Database Access and add a new database user.转到数据库访问并添加一个新的数据库用户。 Use that user and password here:在此处使用该用户名和密码:

cluster = pymongo.MongoClient("mongodb+srv://Hysan:myPassword@cluster0.dvbsy.mongodb.net/test?retryWrites=true&w=majority")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM