Search code examples
pythonpython-3.xmongodbpymongo

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. I don't think I'm connecting it properly. I've seen a StackOverflow question just like this, but it still isn't working. I've replaced the password with my password and myFirstDatabase with the database name. 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'}

Solution

  • 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?

    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. 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")