linux - MongoDB 无法从远程计算机连接

标签 linux mongodb firewall iptables

我已经在 CentOS 7 上安装了 MongoDB 3.6,并且能够在本地连接到它:

# 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.
...
>

我的服务器 IP 地址是 192.168.83.45,但我无法通过 IP 地址而不是 127.0.0.1 从同一服务器登录 MongoDB:

# 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

我检查了以下内容:

  • iptables 规则:附加(同时我的 Apache HTTP 服务器不是 阻止)
  • SELinux 状态:禁用
  • MongoDB IP 绑定(bind):注释掉

检查如下:

iptables(添加规则):

# 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    

我的 Apache HTTP 服务器在端口 80 上运行良好并且没有被阻止:

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

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

SELinux(禁用):

# sestatus
SELinux status:                 disabled

mongod.conf(IPbind 被注释掉了,我清楚地理解简单地注释掉这一行的风险,但这是一个虚拟机并且在仅主机网络下所以没问题):

# 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:

我不仅重启了服务,还重启了整台电脑,还是不行。我既不能通过 IP 地址从同一台计算机访问我的 MongoDB,也不能从远程计算机访问我的 MongoDB。

我又测试了一件事,现在我确定它与我的防火墙无关。我停止了 MongoDB,将 Apache HTTP 服务器的默认监听端口从 80 更改为 27017,然后重新启动。现在我可以通过IP地址为192.168.83.45的27017端口获取HTML文档。所以我认为我的防火墙规则没问题。 MongoDB 一定有问题:

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

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

最佳答案

尽管@Sridharan r.g 的解决方案不起作用,但我的解决方案是受到他的启发。

我非常接近解决方案:

在/etc/mongod.conf 中将“bindIp”的值从“127.0.0.1”更改为“bindIp”,并在“bindIp”之前保留两个空格,如下所示:

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

请注意:

  • “bindIp”前必须正好有两个空格:不能太多 也不会太少。
  • 在MongoDB 3.6的默认文件格式中,不使用 "bind_ip = "而是 "bindIp:"
  • “bindIp”后的冒号之间必须至少有一个空格 和 IP 地址(这里是 0.0.0.0)
  • 如果要添加多个IP地址,请用逗号分隔 每个值,并在逗号和 下一个 IP 地址。

文件格式有点棘手,查看here文件格式规范。

关于linux - MongoDB 无法从远程计算机连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48556003/

相关文章:

php - Symfony2 DoctrineMongoDBBundle : Getting empty result after creating Repository classes

mongodb - 如何匹配两个字段都不存在

azure - Azure SQL 数据库的连接日志

c++ - 使用WFP筛选器允许端口和IP对

python - 唯一的 URLError : <urlopen error timed out> Error I am getting

linux - Linux OOM killer 中的默认 oom_adj 值

linux - 如何使用参数在 Remote 之间复制文件

java - NoClassDefFound错误: com/mysema/query/mongodb/MongodbQuery

linux - 将所有命令和标准输出记录到文件的别名

linux - libc内存管理