Error: couldn't connect to server connection attempt failed: SocketException: Error connecting to caused by :: No connection could be made because the target machine actively refused it

Hello everyone! First I want to say that I’m new to this developer forum, so sorry if I seem a bit clueless.

I have an aws ubuntu ec2 instance which I have installed mongodb community edition using this guide. I have also made a user and set a password for them following parts from this guide as well. However, when I try to connect via the compass or from the command line on my local machine I am greeted with Error: couldn't connect to server connection attempt failed: SocketException: Error connecting to caused by :: No connection could be made because the target machine actively refused it.

The command I use looks like mongo --username myname --password --authenticationDatabase admin --host ipaddr --port 27017 after which I enter my password

When I connect via the compass I try mongodb://myname:password@ipaddr:27017

I also have run the sudo mongod --bind_ip_all command and added my ip address to the security group on aws

I honestly don’t know what to do now. Any help would be appreciated.

1 Like

Same here,

Security Groups open . Port 27017 for all IP’s.
But connection always refused remotelly.

Ok check if there’s some firewall service installed on your ec2 instance like: firewalld

Check list ports closed
sudo firewall-cmd --list-all

Add 27017 to firewalld service:
sudo firewall-cmd --zone=public --add-port=27017/tcp

Worked for me.
Regards.