mongodb listen to localhost and lan ip only - mongodb

I have an Ubuntu server with mongodb installed. Is it possible to configure mongodb in such a way that I can connect to it using only localhost and its LAN IP address.
The default mongo configuration works fine for connecting using localhost:
bind_ip = 127.0.0.1
If I change to the LAN IP, then I am only able to connect using that IP, and not using localhost:
bind_ip = 10.10.10.10
If I try to add both, then I get an address in use error and doesn't start:
bind_ip = 127.0.0.1, 10.10.10.10
If I remove the bind_ip setting, then both work, but I am also able to connect with the servers external IP (which I do not want).
#bind_ip = 127.0.0.1, 10.10.10.10
So, how can I tell mongo to listen on the local interface, but allow connections from both localhost and its LAN IP?

With the help of #wdberkeley's comment, I realized that the problem was the space after the , in the list of IP addresses the bind to. However, it seems that this issue only affects the old config file format (not YAML).
# This Works:
bind_ip = 127.0.0.1,10.10.10.10
# This doesn't work:
bind_ip = 127.0.0.1, 10.10.10.10

Bind to address 0.0.0.0 this will allow it to answer regardless of the IP address being used and even on machines that have multiple network cards with different IP addresses

Related

MongoDB: Connect to localhost from another computer in same network

I'm running MongoDB on one computer and want to connect to the server instance from another computer on the same network.
Going into Mongo shell gives me the following output for both computers: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb with a different instance: Implicit session: session { "id" : UUID(...) }.
I tried the proposed solutions from here and here to no avail. Thought that binding the IP to 0.0.0.0 would help, but it didn't.
Any additional suggestions from someone who dealt with a similar issue?
0.0.0.0 is an address, not a port. You need to configure the server to listen on all addresses or explicitly on loopback (127.0.0.1) and the network address (whatever it is), then configure the client to connect to the server's address.
Use netstat on the server side to verify the server is listening on your configured addresses.

Why can I connect to '127.0.0.1' but not 'localhost' when no internet connection is present in MongoDB compass?

I am curious why I can connect to the localhost server by specifying the loopback IP address (127.0.0.1) but not using the name 'localhost', when no internet connection is present in mongoDB compass? When I try to connect with the domain name I get the following error:
getaddrinfo ENOTFOUND localhost localhost:27017
I'm guessing the domain name lookup process is requiring the internet in order to convert localhost to 127.0.0.1. Not sure if I'm on the right track or not.
When you are connected to the internet the DNS service of your device handles the resolution of localhost. When you are not connected to the device the only way for your machine to know about the name localhost is the /etc/hosts file (or C:\Windows\System32\drivers\etc\hosts if you're running windows, latest versions of windows although might resolve localhost without using the hosts file).

Mongo DB bindIp configuration

In the mongodb config file, what are the IP adresses which are passed in bindIP option
Are they the IP adresses of remote server which have to provided access to mongo database server or The IP addresses and/or full Unix domain socket paths on which mongod should listen for client connections.
From the net.bindIp section of the documentation page:
The hostnames and/or IP addresses and/or full Unix domain socket paths on which mongos and mongod should listen for client connections.
Thus it is the server's IP addresses on which the mongod process should be listening to for incoming connections.
Note that in MongoDB versions 3.6 and later, this parameter has a default value of 127.0.0.1 (localhost).

Issues with setting bind_ip in mongo on centos, pymongo

Running Mongo 3.0.3 on Centos6.5. Running app with pymongo (python3.4) from a different server.
Can only get connection from remote server to work by commenting out bind_ip completely, which I'm a bit worried about. How can bind_ip be set to accept connections from specific IP of remote server and from localhost?
I tried:
bind_ip = 127.0.0.1 # only local works
bind_ip = 127.0.0.1, <remote server IP> # all traffic stops
The bind_ip configuration option in mongodb is for attaching mongos or mongod process to listen to specific network interface. As you may know a server may have multiple network interfaces, you may choose on which interface you want mongos/mongod to listen for connections.
If I put it simply, bind_ip configuration allows attaching mongos/mongod process to one or multiple ips that are available on a server so that it will listen for connection requests coming on the specific ip/ips. bind_ip sometimes confuses beginers. They may feel that bind_ip is for allowing an ip FROM where connections to the mongodb are allowed. But this is not true.
For example, suppose you have a server that has 2 network interface i.e x.x.x.x and y.y.y.y
Now if you run mongod process on this server and use x.x.x.x in bind_ip configuration then you can connect to this mongod from anywhere if you use x.x.x.x:27017 in your connection string. Though the other ip is also on the same server but you will not be able to connect to this mongod using y.y.y.y:27017 in your connection string. Similarly, if you bind your mongos/mongod to 127.0.0.1 then you will only be able to connect to it from the server itself.
Hope this helps.

MongoDB remote connection

I am trying to use MongoDB for remote connection. First I tried localhost and internal ip address to build the connection. Both of them succeeded. Then I tried external ip address to connect on my own computer of MAC as an experiment first. I have created an admin user for the database in MongoDB. The bind_ip option is also commented in mongodb.conf. The firewall should also have been turned down. But the connection cannot been built still. Does anyone know the reason?
Thank you very much!
To allow connecting remote clients (which is your case) to MongoDB Server.
Make sure to do set bind_ip to either of values, in your mongodb.conf file -
Following is with any IP
bind_ip = 0.0.0.0
Following for bind to specific IP
bind_ip = 10.52.36.99 (or any specific IP)