Unable to connect to mongodb hosted locally on our server through Compass

Hello All,

I’m using Mongodb local for a MERN stack application which is hosted on our server . However when Im trying to view it in compass its throwing ECONN Refused error and I’m not sure what to do in this case.Can someone help me on how can I access it through compass?

It looks like mongod is not running on your server. Are you windows, linux or mac?

Share the connection string you are using.

A screenshot of Compass might also help.

Can you connect with mongosh?

Hello steevej,

The mongodb is running on ubuntu 20.04 and When I see the status using service mongodb status its says:
mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor prese>
Active: active (running) since Tue 2022-01-11

My connection string is : mongodb://serverip:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false

serverip in the connection string is basically instead of localhost Im inputting server id when Im trying to connect through compass on my machine,FYI.

In your MongoDB configuration file, is the bindIp address set to include the IP address of the machine? Most likely this only has 127.0.0.1 which means it only listens on the localhost address.

net:
  port: 27017
  bindIp: 127.0.0.1

If you see something like the above, you would need to add the other IP address after the 127.0.0.1 address separated by a comma.

Yes I did added the ipaddress of my machine

Are you trying to connect locally from the machine running the mongod process or from a remote server?

If you’re trying to connect remotely, does the machine running the mongod process have a firewall in place that is blocking the traffic?

I’m trying to connect remotely, Basically a db hosted on prod server and I’m sure there is a firewall but not quite sure if its blocking me anyway I’m on the same internal network as my server

The IP address of your machine or the IP address of the server?

Did you restarted mongod after adding the address?

The status you shared indicate that mongod has been running since 2022-01-11. Did you add the address back then or just now?

Share the configuration file.

I forgot to restart and I just retstarted now but unfortunately Im unable to get it back started its says it exited with status code 48 and here is the error from the logs

Prefix: “/run/mongodb” } }, storage: { dbPath: “/var/lib/mongodb”, journal: { enabled: true } }, systemLog: { destination: “file”, logAppend: true, path: “/var/log/mongodb/mongodb.log” } }
2022-09-12T11:58:36.717-0300 E STORAGE [initandlisten] Failed to set up listener: SocketException: Cannot assign requested address
2022-09-12T11:58:36.718-0300 I CONTROL [initandlisten] now exiting
2022-09-12T11:58:36.718-0300 I CONTROL [initandlisten] shutting down with code:48

It fixed by chaning the BindIp in config to 127.0.0.1
however I cant access through my remote machine via compass and Im not sure why adding 0.0.0.0 is not working and throwing error

Edit: Looks like having both 127.0.0.1,0.0.0.0 is throwing Eadress already in use may be it’s because 0. 0.0.0 basically means all ip and its somehow conflicting and making bindip only to 0.0.0.0 works fine and I 'm able to access the db via remote host however worried if it has any security concerns

This means that there is already a service listening on the server/port combination. Most likely this is another instance of the mongod process.

When you restarted, did you reboot the machine or just restart the mognod process? If you restarted the process, how did you do it?

Setting bindIp to 0.0.0.0 just means that mongod will listen on all network interfaces. If your host has a network interface that allows incoming traffic and you don’t have firewalls in place blocking public traffic to port 27017, then you have every right to be worried about security concerns. If your server has two network cards in it, one for external and one for internal traffic, then 0.0.0.0 will bind to both of these which is not good. External network traffic does not need to connect directly to your MongoDB servers.

I would set my bindIp up to be 127.0.0.1,10.x.x.x. The 127.0.0.1 address is your localhost address so can connect locally from the machine without it going over the network. If you don’t want people to connect from the MongoDB host, you can leave this IP address out. The 10.x.x.x would ideally be an internal only interface that does not allow for outside your network traffic. I only recommend using 0.0.0.0 for testing purposes and then recommend changing back to more restrictive IPs.

The configuration docs have a small section on security considerations. But it’s definitely worth making sure your database server is properly secured.

Thanks for this Info.

Yes I have restarted the mongodb process using systemctl restart mongodb and I tried using 127.0.0.1 , 10.x.x.x however it throwed error in starting mongodb service with status code 48 saying this -

Failed to set up listener: SocketException: Cannot assign requested address
2022-09-12T11:58:36.718-0300 I CONTROL [initandlisten] now exiting
2022-09-12T11:58:36.718-0300 I CONTROL [initandlisten] shutting down with code:48

Run ps aux | grep mongod and see if you have anoter instance of MongoDB running. If you do, kill that instance and then try your sudo systemctl start mongodb command.

As the error states, the requested address cannot be assigned, and exit code 48 is thrown when there is already something listening on the port that mongod is trying to listen on.

It sometimes means that you are trying to listen on an IP address that is not valid for your machine.

Is 10.x.x.x a valid IP for this machine?

If it is, it is possible that the network interface is not up yet. For example, VPNs are started after and your 10.x.x.x is not valid when the mongod service start. If that is the case, you may add a depency in your mongod service file to ensure what ever make 10.x.x.x valid is ran first.

1 Like

Thanks @steevej I see you are correct. An incorrect IP address will give that message. I don’t think that I’ve come across that in the past.

I just associate error 48 with a second instance trying to run on the same machine with the same host/port, but the error message in that case is Address already in use and I obviously didn’t pay attention to the error message in this case which is Cannot assign requested address. :sad:

For testing purposes here are the log entries (filtering only the lines with error or exit in them). The first one is trying to start an instance up when I’ve already got one running on the defaults, and the second one when trying to bind to an IP address that is not assigned to my NIC:

You’ve just taught me something once more. :wink:

@priyatham_ik can you verify, as Steeve mentions, that your IP address is correct?

I have stopped counting how many times learned from you.

Sorry for the late reply, basically I have given it like 10.10.0.0. to make it allow anything that matches this pattern and my machine IP was something like 10.10.x.x .

Am I wrong with this kind of attempt in allowing all internal IP addresses?

It did not work. So yes you were

the way you did it.

Do do that you have to use 0.0.0.0 or bindIpAll as documented.