Issues accessing MongoDB remotely

I have a doubt which is tearing my mind apart. (im talking about the mongodb installed in my local machine) Lets say mongod is the server which connects the client mongo with the database., when i just run mongo without running mongod, why it works??..if mongod implicitly runs in the back and binds to the localhost when run mongo , then the use of mongod command is just to change the configuration?? should i always start mongo server with mongod.conf file everytime to start with the configurations mentioned in the file?

Hi @Shrikar_Man,

Yes, you can run directly type mongo in the shell and run the MongoDB without any command-line options to connect to a MongoDB instance running on your localhost with default port 27017.

mongo

You can also use mongo to run the MongoDB on a specific port if you want.!!

mongo --port 28015

So, you don’t need to run mongod explicitly but you can use the mongod command with the mongod.conf file to run your specific custom configuration on your machine.!!

In case of any further questions, please feel free to reach out to us.

Thanks,
Kushagra

thanks sir !! i figured it out!!..i binded my computer’s ip address in the conf file and ran the mongod server succesfully…but when i try to connect the server from my friend’s computer by typing the following:
mongo --host 192.168.xxx.xxx --port 777
i installed mongodb in his computer too…
so, when i ran this , i got NetworkTimeout error named Socket operation timed out
ho to resolve this?

Hi @Shrikar_Man,

In general, when you do not use the mongo shell for some time it happens, I would recommend you to close the current connection and try to connect again and it should work fine.!

But, there could be some other reasons, if you can share the exact error message or the screenshot, we could narrow down the specific reason behind your issue.!!

Please feel free to reach out to us for any assistance!!

Kind Regards,
Kushagra

1 Like

Kindly listen to the whole process i did !
by default my mongodb instance listens to localhost, I tried to bind it with my ip address.
so i used ipconfig command to know my ip adress.
cmd_LI

And then i used that ip address to bind in the config file.
config

And then i tried to connect the mongo shell from my same computer and everything worked fine.

I felt happy and i tried to connect this instance from my friend’s computer which runs in another lan network but i got this error.

By surfring through the internet i read in some article where we need to bind our instance the public ip address in order to access it rometly from a different lan .
so i got my public ip address in whatismyipaddress.com

And i tried to use this ip address in the config file
newconfig

Now i tried to start the mongod server with this config file and it stopped


so i tried to look into the log files and got the error as the requested address is not vaild in its context.

Kindly help me to know what is the mistake im doing!!

Hi @Shrikar_Man,

The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer.

  • First, you should check that your instance actually has the IP assignment by using ipconfig command. If your IP address is not on the output list, then the IP address is invalid to bind to. i.e. You can’t bind to an external IP.

  • And if the IP address is on the output list of ipconfig, and you are trying to connect your computer with your friend’s computer from the same LAN, then:

  1. Check any active firewall that may be blocking the network access. i.e. Windows Firewall
  2. Try connecting with the two computers without involving MongoDB to test the network connection.
  • Lastly, If you are trying to connect your computer with your friend’s computer from a different LAN, then you need to configure the router to forward the requests to your computer.

As this feels more of a networking question rather than MongoDB usage.!!

Thanks,
Kushagra

the ip address which i’ll get when i run ipconfig will be my private ip address right??..
mongodb remote access
In this article it is said that i need to bind the server computer’s public ip address in the config file in order to remotely access the server. But the mongod refuses to start and throws this error of “The requested address is not in its context”.

Private ip address issue :
If i just use my private ip address which is shown in ipconfig , Then the mongod server starts and only i can able to access it …when i try to access it from my friend’s computer i’m getting this error of “Socket operation timed out”.

Hi @Shrikar_Man,

Here, 192.168.* addresses are Private IPs. You cannot directly connect to those over the public internet.

So, you will need VPN or something for port forwarding.

Explanation: Devices on the same side of the router as the private IP can talk to each other via the 192 addresses but outbound requests to public IPs from devices on the private network transit via the router and will originate from the router’s public IP, like this:

192.168.x.y (private IP) → router → 151.x.y.z (public IP)

So, you have to tunnel your incoming requests of the public IP to the private IP i.e., your friend’s computer IP via VPN, SSH, or port forwarding. Then you can connect.

Hope it helps!!

Thanks,
Kushagra

2 Likes

Thats a very useful information…could you please provide any resource on how to do that port forwarding and what are all the changes i have to do in the config file and in the computers of mine and my friend’s one

This is basic IP networking and routing and completely not directly related to MongoDB and outside the scope of this forum.

There are so many way to achieve what you what. My recommendation is to use Atlas since it does not require the IP networking and routing knowledge you are missing.

2 Likes