Search code examples
linuxmongodbfirewalliptables

MongoDB cannot connect from remote computer


I've installed MongoDB 3.6 on CentOS 7 and am able to connect to it locally:

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
# mongo
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Welcome to the MongoDB shell.
...
>

My server IP address is 192.168.83.45, but I can't login to the MongoDB from the same server via the IP address instead of 127.0.0.1:

# ip addr | grep 'inet '
    inet 127.0.0.1/8 scope host lo
    inet 192.168.83.45/24 brd 192.168.83.255 scope global enp0s3
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic enp0s8
# mongo --host 192.168.83.45
MongoDB shell version v3.6.2
connecting to: mongodb://192.168.83.45:27017/
2018-01-31T23:29:35.817-0500 W NETWORK  [thread1] Failed to connect to 192.168.83.45:27017, in(checking socket for error after poll), reason: Connection refused
2018-01-31T23:29:35.818-0500 E QUERY    [thread1] Error: couldn't connect to server 192.168.83.45:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

I have checked the following:

  • iptables rules: appended (meanwhile my Apache HTTP server is not blocked)
  • SELinux status: disabled
  • MongoDB IP bind: commented out

The check is shown below:

iptables (rule added):

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:3000
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:27017
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    

My Apache HTTP server works well on port 80 and is not blocked:

# curl http://192.168.83.45
<html>
<head>
<title>Hello World!</title>
</head>

<body>
Hello World!
</body>
</html>

SELinux (disabled):

# sestatus
SELinux status:                 disabled

mongod.conf (IPbind was commented out, and I clearly understand the risk of simply commenting out this line but this is a virtual machine and is under host only network so it's fine):

# cat /etc/mongod.conf 
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

I've not only restarted the services, but also restarted the whole computer, but it still doesn't work. I can neither access my MongoDB from the same computer but via the IP address, nor from a remote computer.

I tested one more thing and now I'm sure it has nothing to do with my firewall. I stopped the MongoDB, changed the default listening port of Apache HTTP server from 80 to 27017 and restarted. Now I can get the HTML document via 27017 port with IP address 192.168.83.45. So I think my firewall rule is OK. There must be something wrong with the MongoDB:

# curl 'http://192.168.83.45:27017'
<html>
<head>
<title>Hello World!</title>
</head>

<body>
Hello World!
</body>
</html>

Solution

  • Despite @Sridharan r.g's solution doesn't work, my resolution was inspired by his answer.

    I was so close to the solution:

    Change the "bindIp" value from "127.0.0.1" in /etc/mongod.conf AND KEEP TWO SPACES BEFORE THE "bindIp", like this:

    ...
    # network interfaces
    net:
      port: 27017
      bindIp:  0.0.0.0
    ...
    

    Please note:

    • There must be exactly two spaces before "bindIp": neither too many nor too few.
    • In the default file format of MongoDB 3.6, it doesn't use "bind_ip = " but rather "bindIp:"
    • There MUST BE AT LEAST ONE SPACE between the colon after "bindIp" and the IP address (here it is 0.0.0.0)
    • If you want to add more than one IP addresses, use comma to separate each values, and KEEP AT LEAST ONE SPACE between the comma and the next IP address.

    The file format is a little bit tricky, check here the file format specification.