#(Kaleb) Cannot connect to Mongodb in docker

10 messages · Page 1 of 1 (latest)

thin field
#

I am trying to make a Discord bot and have a Mongodb to store all the user data. I have never set up a db before but I think i have it all setup correctly and built with docker-compose.

For some reason I cannot seem to connect to the db using my code or using Compass (Mongodb gui).

Here is how I build it with docker-compose:


services:
  bot:
    build: .
    depends_on:
      - db
    ports:
      - "8080:80"
  db:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password
    volumes:
      - admin_bot_db:/data/db
    ports:
      - "28017:28017"

volumes:
  admin_bot_db:```


Here is my .env file:
```BOT_TOKEN=your_discord_token
MONGODB=mongodb://root:root@db:28017/bot_db #change this if you use a different URI
ADMIN_ROLE=your_role_id
USER_ROLE=your_role_id
BOT_CHANNEL=your_channel_id
ADMIN_CHANNEL=your_channel_id```

This is my code:
```import os
import discord
from discord.ext import commands
from pymongo import MongoClient
from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env.
async def load_db():
    try:
        # Connect to your MongoDB
        mongodb = os.getenv('MONGODB')
        print(f"Connecting to MongoDB at {mongodb}")
        client = MongoClient(mongodb)
        db = client.admin_bot_db
        return db
    except Exception as e:
        print(f"Error connecting to MongoDB: {e}")
        return None```
worn breachBOT
#

Hi I'm AutoThreadBot! Don't mind me, I'll just be adding the helper team to this thread so they can see it. A human will get to you soon.
You can block this bot if you don't want to see these messages, I won't mind.
<@&525394568410038282>

thin field
#

This is the error I get:

admin_bot-bot-1  | Traceback (most recent call last):
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/discord/client.py", line 441, in _run_event
admin_bot-bot-1  |     await coro(*args, **kwargs)
admin_bot-bot-1  |   File "/app/core/xp.py", line 130, in on_message
admin_bot-bot-1  |     user = await self.get_user(user_id)
admin_bot-bot-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
admin_bot-bot-1  |   File "/app/core/xp.py", line 17, in get_user
admin_bot-bot-1  |     user = await self.users.find_one({"_id": user_id})
admin_bot-bot-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/collection.py", line 1459, in find_one
admin_bot-bot-1  |     for result in cursor.limit(-1):
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/cursor.py", line 1248, in next
admin_bot-bot-1  |     if len(self.__data) or self._refresh():
admin_bot-bot-1  |                            ^^^^^^^^^^^^^^^
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/cursor.py", line 1139, in _refresh
admin_bot-bot-1  |     self.__session = self.__collection.database.client._ensure_session()
admin_bot-bot-1  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1740, in _ensure_session
admin_bot-bot-1  |     return self.__start_session(True, causal_consistency=False)
admin_bot-bot-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1685, in __start_session
admin_bot-bot-1  |     self._topology._check_implicit_session_support()
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support
admin_bot-bot-1  |     self._check_session_support()
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 554, in _check_session_support
admin_bot-bot-1  |     self._select_servers_loop(
admin_bot-bot-1  |   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 238, in _select_servers_loop
admin_bot-bot-1  |     raise ServerSelectionTimeoutError(
admin_bot-bot-1  | pymongo.errors.ServerSelectionTimeoutError: db:28017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 649292dbdf1461bf483e6eba, topology_type: Unknown, servers: [<ServerDescription ('db', 28017) server_type: Unknown, rtt: None, error=AutoReconnect('db:28017: [Errno 111] Connection refused')>]>```


Here is my github if you wanted to see the whole bot or try to run it yourself:
https://github.com/kalebbroo/admin_bot
GitHub

Admin bot for Discord with rank tracking. Contribute to kalebbroo/admin_bot development by creating an account on GitHub.