Question

How to setup connection to a open mongo server

I was following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-configure-remote-access-for-mongodb-on-ubuntu-20-04-de and now at the end I get this msg:

nc: connect to MONGOSERVERIP port 27017 (tcp) failed: Connection refused

Need help with that (ALl in the Tuto works fine only this at the end)

Show comments

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 7, 2022
Accepted Answer

Hi there,

Do you have a firewall like ufw or csf for example? If this is the case you need to make sure that you either allow the IP that you are connecting from via the firewall or you open the 27017 port for incoming connections.

To get your IP you can visit this site here:

https://ifconfig.io/

Also to verify that the Mongo service is listening on 0.0.0.0 you can run:

netstat -plant | grep 27017

Let me know how it goes.

Best,

Bobby

Hey as I said I followed this tutorial: https://www.digitalocean.com/community/tutorials/how-to-configure-remote-access-for-mongodb-on-ubuntu-20-04-de And they use ufw for the firewall thing. Thats my current config:

root@vmd89202:~# ufw status Status: active

To                         Action      From
--                         ------      ----
Apache Full                ALLOW       Anywhere
27017                      ALLOW       MYTRUSTEDIP
OpenSSH                    ALLOW       Anywhere
Anywhere                   ALLOW       MYTRUSTEDIP
Apache Full (v6)           ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
Bobby Iliev
Site Moderator
Site Moderator badge
June 9, 2022

Hello,

Do you by any chance have another firewall like a Cloud firewall for example?

Would you mind also sharing the output of the netstat command from the last comment too?

Thats the netstat output:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51582         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46594         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46596         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:58516         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51668         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51678         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:46580         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:51586         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51676         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:50322         127.0.0.1:27017         ESTABLISHED 1936475/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51674         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:58520         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:46602         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51668         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:38506         127.0.0.1:27017         ESTABLISHED 6873/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:38506         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46598         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:51582         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:38390         127.0.0.1:27017         ESTABLISHED 6873/node
tcp        0      0 127.0.0.1:46596         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46590         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51664         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51670         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51674         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46594         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:58548         127.0.0.1:27017         ESTABLISHED 1956090/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51574         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46590         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46640         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:46586         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51680         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51574         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:50324         127.0.0.1:27017         ESTABLISHED 1936475/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:58548         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:38390         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:46580         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51592         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51584         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51586         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:38392         127.0.0.1:27017         ESTABLISHED 307874/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46592         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:58516         127.0.0.1:27017         ESTABLISHED 1956090/node
tcp        0      0 127.0.0.1:58522         127.0.0.1:27017         ESTABLISHED 1956090/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51672         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51666         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51678         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46608         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:39046         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:51592         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:50320         127.0.0.1:27017         ESTABLISHED 1936475/node
tcp        0      0 127.0.0.1:51584         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:50322         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46600         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:38392         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:46604         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:50318         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:50324         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51670         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:50318         127.0.0.1:27017         ESTABLISHED 1936475/node
tcp        0      0 127.0.0.1:39046         127.0.0.1:27017         ESTABLISHED 307874/node
tcp        0      0 127.0.0.1:46586         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:51664         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:51660         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:46592         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:51672         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:51676         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51660         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46640         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51666         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:46604         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46608         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:51680         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:51578         127.0.0.1:27017         ESTABLISHED 1803226/node
tcp        0      0 127.0.0.1:46602         127.0.0.1:27017         ESTABLISHED 1771336/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:51578         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:58520         127.0.0.1:27017         ESTABLISHED 1956090/node
tcp        0      0 127.0.0.1:27017         127.0.0.1:46600         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:46598         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:58522         ESTABLISHED 1678909/mongod
tcp        0      0 127.0.0.1:27017         127.0.0.1:50320         ESTABLISHED 1678909/mongod

And no I dont use the other firewall

Bobby Iliev
Site Moderator
Site Moderator badge
June 10, 2022

Hello,

According to the first line from the output:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1678909/mongod

Your service is listening on 127.0.0.1:27017 which means that it will only be accessible from the server itself. You need to change the bind address from 127.0.0.1 to 0.0.0.0 or add your server IP in the list in order to be able to access the service externally as described in step 2 from the tutorial that you’ve shared.

After that change, you need to restart the service in order for the change to take effect.

Let me know how it goes.

I already did that in my mongod.conf but I found something I have a mongodb.conf too and there is a bind_ip = 127.0.0.1 part idk what this is for a config

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.