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 connect them together.
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 = 0.0.0.0 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(‘45.55.10.237:27017/my-app’);
ufw status - MongoDb: To Action From
22 ALLOW Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere 27017 ALLOW Anywhere Anywhere ALLOW 162.243.155.248 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 0.0.0.0:27017 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.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 45.55.10.237 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 0.0.0.0:22 0.0.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
OR
ssh
-L 4321:localhost:27017
-i ~/.ssh/my_secure_key
ssh_user@mongo_db_droplet_host_or_ip
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.
What am I missing? Please help.
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!
Heya,
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:
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.
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.localhost:4321 as your MongoDB URI.mongodb://localhost:4321/your_database_name
Troubleshooting:
If you’re facing connection denied issues, it could be due to several reasons:
ufw rules on the MongoDB droplet and ensure SSH connections are allowed.mongod (the MongoDB process) is actually running on the MongoDB droplet.A More Secure Approach: Instead of allowing MongoDB connections from anywhere, it’s better to tighten security:
bindIp: 127.0.0.1 in your mongod.conf file.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.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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.