I’m developing a Discord bot, and I can utilize the database outside of the bot. Still, whenever I use the same code for the database on the bot, the DB actively refuses the connection.
I cannot connect to it without being SSH’d, even though they are on the same network but on different machines, which I have a post for if anybody has any ideas regarding that. database.py
This is an unrelated problem to the other post, as I’m able to use the following code when it is started, but I’m unable to go through the bot using the same code with the test stuff commented.
After I set bindIpAll: true in the mongod.service file, as mentioned in my other post, I get the exact same error as before only when using the Discord bot. However, whenever I run the following code as a standalone file, it works perfectly fine.
I can’t comment on the bot code, because I haven’t seen it. Let’s try to narrow down my understanding of the problem with these two files you’ve shared.
If you run yuferojoya.python (the code you say is related to the bot code) does that raise the error? What happens if you uncomment the test() definition and the asyncio.run line (but leave the rest unchanged)?
Whenever I run yuferojoya.python, with the asyncio.run and test function uncommented, it runs just fine. The only thing the bot is doing, for now at least, is just a command for testing purposes which has the exact same search function as Standalone.
For further clarification, we can call “etexusemov.python” the standalone version and “yuferojoya.python” the bot version to prevent the need from constantly hyperlinking the two.
The nextcord command works as expected (meaning it errors out on Discord since there’s no interaction response). The following is the only running code for the command at this time. Well, there’s more, but it’s commented out or not being hit due to “cursor = await db.find_one()” erroring out before it can be hit.
I made a ticket to the official nextcord server, hopefully they may have some suggestions, in the meantime, any other suggestions or what not is greatly appreciated.
I’m 95% certain that the problem is that you’re running the standalone vs bot scripts with different config.
1,2c1,2
< # from run import util
< from util import Util
---
> from run import util
> # from util import Util
I think if you print out the config within each of the scripts you’ll see that something fundamental is wrong in the bot’s config, either the ssh or database sections.
But fundamentally, the first thing you should fix is your mongod installation so you don’t need to SSH tunnel to the server in order to connect to it. It’s unnecessary, and it’s making your client setup more complex than it needs to be.
The config file is just a JSON file containing all the secrets. Here is a template for the JSON file being utilized. They are different because there is a utility file and folder with secrets, colors, or config files. With the Discord bot, the utility function is defined within the run.py file the same way in the database.py file.
I agree I should fix the Mongo installation regarding the required SSH tunnel. Should I make another post separate from the two already made for that? However, adding support for requiring an SSH tunnel doesn’t hurt.
Edit (I didn’t see the edit button until now):
I forgot to mention that, in case of any confusion, they are referencing the same files in the same way, just at different points/different files. This bot is literally barebones; it does nothing but start the bot and test access to the DB.
If you’re running the same code on the same machine, and getting different results, then it’s the config.
The two sample files you’ve provided are similar enough that they’re effectively the same. One’s working, and the other’s not, so I’m convinced it’s the config until you print out the values being passed to AsyncIOMotorClient and prove me wrong.
I modified the code not to utilize anything except to print the value since the printed values would’ve been usernames and passwords, so I had to modify them before printing them.
Ah, sorry, I didn’t mean for you to post any of the config here, obviously that’s sensitive info. Thank you for checking out the config.
You’re not getting far enough for the username and password to be sent to the server - you’re not successfully making a connection to the server - so the problem is either the IP or the port of (I’m guessing) the machine you’re SSHing to.
Given that both computers are on the same network, and I assume that network has a firewall securing the network, you should be able to safely run sudo ufw disable , at least temporarily on the database server, to see if that allows you to connect to MongoDB with mongosh
It was only enabled recently, within the past four days at most, but I just disabled it, and it’s giving the same response.
At least in this instance, I feel that part of the issue is the ssh_server.local_bind_port for the port is the issue as that is constantly changing, but it’s required to utilize an SSH connection.