Can't connect to a mongodb if it is secure

58 views Asked by At

I'm trying to connect my app to MongoDB using MONGO_URL: in MUP. The database is on an independent VPS. The VPS is behind a firewall and is connected to the app VPS via a private network. If I don't secure the database with a username and password I can get it to connect however if I create a user to allow readWrite commands to add security it wont connect. I get this error.

MongoError: failed to connect to server [private-network-ip:27017]

If I log into the database and use show dbs this is what appears:

admin         0.000GB
myspecial_db  0.000GB
local         0.000GB

I'm trying to access the myspecial_db.

MONGO_URL: mongodb://username:password@private-network-ip:27017/myspecial_db

What am I missing here?

2

There are 2 answers

2
Mikkel On

It sounds like you can connect to the server ok, meaning that access through the firewall is set up properly.

It is likely that authentication happens on the admin database, in which case you will need to add this to the end of your url:

?authSource=admin

You can also try

?authSource=myspecial_db

If security is your concern, I would also want to connect using SSL by adding this as well:

&ssl=true

This will ensure that your username/password is not sent in clear text, as well as encrypting all your data in transmission

0
bp123 On

The problem I had wasn't related to the MONGO_URL it was related to server setup. I had blocked all incoming traffic using a firewall and the open port was incorrect.