Build mongo uri using self hosted database

Hello everyone.

I build a self hosted instance of mongodb in AWS EC2 with username and password configs.
It work correclty with connection in compass or in my app using the uri like this:

mongodb://username:password@11.22.33.44:27017/db?authSource=admin

When I fetch 11.22.33.44:27017, I get this message:

It looks like you are trying to access MongoDB over HTTP on the native driver port.

I want to use an uri using a subdomain like the uri provided by mongo atlas.
something like this

mongodb://username:password@mongodb.subdomain.mydomain.com/db

then deny all access to port via IP address 11.22.33.44:27017.

I want only acces to mongo instance via one uri with domain name.

Thanks

This is expected. MongoDB will expect TLS handshake or mongodb wire protocol.

Doing it like Atlas is more complex as it uses the DNS Seed List connection URI mongodb+srv:// which would require the configuration of DNS SRV and TXT records. See connection string.

That is never going to work. DNS always resolves to an ip address. It is better to allow access only from the IPs you permit to connect.

Be sure to enable TLS as well. Connecting without TLS leaves the traffic vulnerable to capture and eavesdropping.

1 Like