我在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(增加规则):
# 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:
我不仅重新启动了服务,还重新启动了整台计算机,但它仍然无法运行。我既不能从同一台计算机访问我的MongoDB,也不能通过IP地址访问,也不能从远程计算机访问。
我测试了一件事,现在我确定它与我的防火墙无关。我停止了MongoDB,将Apache HTTP服务器的默认侦听端口从80更改为27017并重新启动。现在我可以通过27017端口获取HTML文件,IP地址为192.168.83.45。所以我认为我的防火墙规则没问题。 MongoDB一定有问题:
# curl 'http://192.168.83.45:27017'
<html>
<head>
<title>Hello World!</title>
</head>
<body>
Hello World!
</body>
</html>
答案 0 :(得分:1)
确保mongodb守护程序正在运行,并且正在侦听0.0.0.0,而不是127.0.0.1端口
检查指定mongodb端口列表是不是借助于netstat命令
你仍然面临着改变的问题 $ vim /etc/mongod.conf
bind_ip = 127.0.0.1,192.168.161.100,45.56.65.100
答案 1 :(得分:1)
尽管@Sridharan r.g的解决方案并不起作用,但我的解决方案却受到了他的回答的启发。
我非常接近解决方案:
更改&#34; bindIp&#34;价值来自&#34; 127.0.0.1&#34;在/etc/mongod.conf中并在&#34; bindIp&#34;之前保留两个空格,如下所示:
...
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
...
请注意:
文件格式有点棘手,请检查here文件格式规范。