0

I want to have access to my MongoDB database remotely. for example: with the MongoDB compass but I receive this error: connection <monitor> to <my_server_ip>:27017 closed

my MongoDB database is installed on a ubuntu VPS and the ubuntu version is 20.04 and I want to connect to my database with my windows computer which has MongoDB compass.

I followed along with this tutorial to configure remote access for MongoDB: https://www.digitalocean.com/community/tutorials/how-to-configure-remote-access-for-mongodb-on-ubuntu-20-04

I think there must be something wrong with the firewall or IP access.

I'm new to Linux and I've tried everything that I could but I couldn't figure it out

  • at first, my /etc/mongod.conf was like this:
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 136.243.244.202

the ip 136.243.244.202 is the ip address of the Linux VPS which the MongoDB is running on

  • then, I changed the bindIp to something like the code below and restarted the MongoDB with sudo systemctl restart mongod but again it didn't work:
bindIp: 0.0.0.0
  • I also tried to open port 27017 with sudo ufw allow 27017

in addition, when I run sudo ufw status, the result is like this:

root@moonfo:~# sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
27017                      ALLOW       Anywhere
22                         ALLOW       Anywhere
27017/tcp                  ALLOW       Anywhere
27017                      ALLOW       136.243.244.202
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)
22 (v6)                    ALLOW       Anywhere (v6)
27017/tcp (v6)             ALLOW       Anywhere (v6)

and when I run sudo lsof -i | grep mongo to see which port the MongoDB is listening to, the output is the response below:

mongod    69866    mongodb   10u  IPv4 1324230      0t0  TCP *:27017 (LISTEN)
mongod    69866    mongodb   46u  IPv4 1393764      0t0  TCP kvm.jupiter.7ho.st:27017->5.122.78.214:15066 (ESTABLISHED)

I'm new to Linux and everything that I did, couldn't solve my problem.

I just want to be connected to my MongoDB database from any computer, it doesn't matter which computer it is. and the connection url that I use to be connected in MongoDB compass is something like this:

mongodb://<db_username>:<db_username_password>@136.243.244.202:27017/admin

as I mentioned, the ip address of 136.243.244.202 is the ip address of the server which my database is running on. and the other thing is that my database is active and running on the VPS and I can connect to it locally but I can't connect to it remotely

  • It looks OK. What do you get from `curl --connect-timeout 5 --silent --show-error 136.243.244.202:27017`? I you get "*It looks like you are trying to access MongoDB over HTTP on the native driver port.*" then connection/firewall settings are fine. Did you set any connection restriction or did you enable TLS/SSL? – Wernfried Domscheit Mar 12 '23 at 09:49
  • I ran the command that you mentioned and I got the expected result that you said. so it seems that my connection/firewall works fine. actually, I have also a node.js app running on this server, and I used certbot with Let's encrypt to provide an SSL for my node.js app domain. Do you think it's making an issue because of providing an SSL for my node.js app domain? @WernfriedDomscheit – Mohammad Rezaii Mar 12 '23 at 12:21
  • When you use TLS/SSL (which is good), then you must use it also for connection, have a look at [TLS/SSL Configuration for Clients](https://www.mongodb.com/docs/manual/tutorial/configure-ssl-clients/). Note, this documentation only refers to command-line options, but you can set most of them also in [ConnectionString](https://www.mongodb.com/docs/manual/reference/connection-string/#std-label-connections-standard-connection-string-format) – Wernfried Domscheit Mar 12 '23 at 12:30
  • I read the documentation and it said that we need to add a query called `ssl=true` or `tls=true` to the connection string. I added that to the connection string like so: `mongodb://@136.243.244.202:27017/admin?ssl=true` but it still doesn't work. this is going to be crazy, nothing works for me! BTW, thanks for your help <3 @WernfriedDomscheit – Mohammad Rezaii Mar 12 '23 at 15:15
  • You need to specify the CA certificate or option `--tlsUseSystemCA` (if you work with the mongo shell), see https://stackoverflow.com/questions/41302023/how-security-in-mongodb-works-using-x-509-cert/75043317#75043317 – Wernfried Domscheit Mar 12 '23 at 15:37
  • I've enabled `Use System Certificate Authority` option in MongoDB Compass but it still doesn't work! actually, when I use `sudo lsof -i | grep mongo`, the output includes two lines but I showed one of them in my answer. now, I updated the answer and added the second line of output that the `sudo lsof -i | grep mongo` shows me. please have a look at that. maybe it's creating the issue @WernfriedDomscheit – Mohammad Rezaii Mar 12 '23 at 16:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252472/discussion-between-mohammad-rezaii-and-wernfried-domscheit). – Mohammad Rezaii Mar 12 '23 at 16:46

0 Answers0