Cannot connect Angular app to MongoDb. Two separate droplets.

The whole thing is driving me nuts a already. Do not get me wrong I checked all possible answers and tutorials here available, but my case seems to be “super unique”.

So here it is: Two droplets: a - runs my Angular 2 app, b - runs my MongoDb (one click install).

I just cannot marry them.

Tried to play with /etc/mongod.conf, in particular: port = 27017, bindIp = my_Angular_droplet_ip

No luck. Angular shell writes - Mongoose Disconnected.

port = 27017, bindIp = or commented (I know it’s not good)

Different No luck. Angular shell writes - Mongoose Connected. But I cannot CRUD anything from the actual app - get an net::ERR_CONNECTION_REFUSED in the terminal.

In my app.js, connection looks like this: mongoose.connect(‘’);

ufw status - MongoDb: To Action From

22 ALLOW Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere 27017 ALLOW Anywhere Anywhere ALLOW 22 (v6) ALLOW Anywhere (v6) 80 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6) 27017 (v6) ALLOW Anywhere (v6)

netstat -tln MongoDb: Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0* LISTEN tcp 0 0* LISTEN tcp6 0 0 :::22 :::* LISTEN

ufw status - Angular: To Action From

22 ALLOW Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere Anywhere ALLOW 22 (v6) ALLOW Anywhere (v6) 80 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6)

netstat -tln Angular: Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0* LISTEN tcp6 0 0 :::22 :::* LISTEN

Tried another (preferable) option - SSH Tunnel. No luck at all. according to two tutorials I should write either: ssh -L 4321:localhost:27017 user@your.ip.address -f -N mongo --port 4321


-L 4321:localhost:27017
-i ~/.ssh/my_secure_key

which is essentially the same but I get - connection dined in both cases. Non of those tutorials say at what side (Angular or MongoDb) I should run these commands. My though was - Angular. So I run them in Angular shell - nope.

I understand that I am missing something. Please help.

Submit an answer

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.

Site Moderator
Site Moderator badge
September 28, 2023


Connecting an Angular application to MongoDB that’s running on two separate droplets requires careful configuration, especially when the MongoDB instance is exposed to the public internet.

The ufw and netstat outputs you provided show that MongoDB is indeed listening on all network interfaces and you have set up the firewall to allow connections from any IP.

However, exposing MongoDB directly to the public internet is not advisable due to security concerns. The preferable option you mentioned, creating an SSH tunnel, is a good choice as it provides a secure connection between the two droplets.

Here are the steps and explanations:

  1. SSH Key Authentication: First, ensure that you can SSH from the Angular droplet to the MongoDB droplet using SSH key authentication. This will eliminate the need for a password when setting up the SSH tunnel.

  2. Setup SSH Tunnel: Run the SSH tunnel command on the Angular droplet, as your Angular app will be the client connecting to the MongoDB server. The command is:

ssh -L 4321:localhost:27017 user@mongo_droplet_ip -f -N

Here’s a breakdown of what this does:

  • -L 4321:localhost:27017 maps port 4321 on the Angular droplet to port 27017 (MongoDB’s port) on the MongoDB droplet.
  • -f puts SSH in the background.
  • -N ensures no commands are executed.
  1. Connect from Angular app: In your Angular application (or rather, on the server-side part that your Angular app communicates with, such as a Node.js backend), when you connect to MongoDB, use localhost:4321 as your MongoDB URI.
  1. Troubleshooting:

    If you’re facing connection denied issues, it could be due to several reasons:

    • The SSH key might not be set up correctly between the two droplets.
    • The MongoDB droplet might not allow SSH connections from the Angular droplet. Double-check the ufw rules on the MongoDB droplet and ensure SSH connections are allowed.
    • Ensure that mongod (the MongoDB process) is actually running on the MongoDB droplet.
  2. A More Secure Approach: Instead of allowing MongoDB connections from anywhere, it’s better to tighten security:

    • On the MongoDB droplet, bind MongoDB to localhost only by setting bindIp: in your mongod.conf file.
    • Change the ufw rules on the MongoDB droplet to deny all incoming connections on port 27017. This way, even if someone discovers your MongoDB port, they won’t be able to access it directly.
  3. Additional Considerations: If your Angular app is talking directly to MongoDB, you might want to reconsider this design. Typically, you’d have a backend (like Node.js with Express) that your Angular app communicates with, and then this backend interacts with the database. This provides a layer of security and abstraction.

Lastly, always ensure your MongoDB instance is secure, especially if it’s exposed to the internet. Use strong authentication, regularly update and patch your software, and consider encrypting sensitive data.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel