Motor - Machine Actively Refusing Connect

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 the part where it refuses the connection.

cursor = await DB_NAME.find_one({'Field_To_Searchs' : search})

When I run the following, it runs successfully but whenever I utilize the discord bot it gives the error below:
database.py

Full Error Stack Trace

I’ve replied to your other post as I think solving that problem will lead to you solving this problem too, without any need for an SSH tunnel.

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.

Just to confirm my understanding:

If you run etexusemov.python - it works.

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.

server_id = message_link.split('/')[-3]
channel_id = message_link.split('/')[-2]
message_id = message_link.split('/')[-1]

search = f'{server_id}:{channel_id}:{message_id}'
cursor = await db.find_one()
pprint.pprint(cursor)

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.

secrets_template.json

utils.py

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.

Output of the following standalone:

print(db_info['username'], db_info['password'], ssh_info['username'], ssh_info['password'])
admin p@ssw0rd admin p@ssw0rd

Output from the same code being run on the discord bot:

admin p@ssw0rd admin p@ssw0rd

I did try to run the following on the Pi

sudo ufw allow from trusted_server_ip to any port 27017
sudo ufw enable (or start forget which)

Where the trusted server IP is the PC IP (public IP) in hopes it was genuinely refusing a connection

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.

With UFW Disabled, I am unable to connect. I’m unsure if I made that clear in my previous response.

Unable to Connect unless SSHd Post

Making a reply to prevent the thread from being locked just in case this isn’t resolved with that.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.