我在本地网络的machine1(Ubuntu 14.04.3 LTS server
)上安装了mongo。我还使用以下命令打开了this指南中提到的端口27017
:
sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
目前的规则是(iptables -L
):
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:27017 state NEW,ESTABLISHED
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp spt:27017 state ESTABLISHED
但我无法从machine2(在同一网络中)连接到此端口:
$ mongo --host 192.168.0.108
MongoDB shell version: 3.0.4
connecting to: 192.168.0.108:27017/test
2016-01-23T18:02:14.848+0530 W NETWORK Failed to connect to 192.168.0.108:27017, reason: errno:61 Connection refused
2016-01-23T18:02:14.850+0530 E QUERY Error: couldn't connect to server 192.168.0.108:27017 (192.168.0.108), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
我也尝试检查端口是否打开:
$ nc -v 192.168.0.108 27017
nc: connectx to 192.168.0.108 port 27017 (tcp) failed: Connection refused
我不确定我错过了什么。当我第一次设置mongo时,一定是一些愚蠢的错误。请帮助。
更新(针对当前答案中提出的疑问)
$ service mongod status mongod start/running, process 31205
,端口为27017 $ sudo netstat -tulpn |grep 27017 tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 31205/mongod
bind_ip
更改为0.0.0.0
并重新启动了mongo。仍然会出现同样的错误。Mongo记录:
$ cat /var/log/mongodb/mongod.log
2016-01-23T16:28:13.155+0530 I CONTROL [initandlisten] MongoDB starting : pid=31205 port=27017 dbpath=/var/lib/mongodb 64-bit host=dexter
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] db version v3.2.1
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1f 6 Jan 2014
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] allocator: tcmalloc
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] modules: none
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] build environment:
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] distmod: ubuntu1404
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] distarch: x86_64
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] target_arch: x86_64
2016-01-23T16:28:13.156+0530 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2016-01-23T16:28:13.173+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-01-23T16:28:14.444+0530 I CONTROL [initandlisten]
2016-01-23T16:28:14.444+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'
2016-01-23T16:28:14.444+0530 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-01-23T16:28:14.949+0530 I NETWORK [initandlisten] waiting for connections on port 27017
2016-01-23T16:38:37.046+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37032 #1 (1 connection now open)
2016-01-23T16:39:31.447+0530 I NETWORK [conn1] end connection 127.0.0.1:37032 (0 connections now open)
2016-01-23T16:49:24.240+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37033 #2 (1 connection now open)
2016-01-23T16:49:38.249+0530 I NETWORK [conn2] end connection 127.0.0.1:37033 (0 connections now open)
2016-01-23T16:51:51.707+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37034 #3 (1 connection now open)
2016-01-23T16:51:55.785+0530 I NETWORK [conn3] end connection 127.0.0.1:37034 (0 connections now open)
2016-01-23T17:32:15.546+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37036 #4 (1 connection now open)
2016-01-23T17:32:21.180+0530 I NETWORK [conn4] end connection 127.0.0.1:37036 (0 connections now open)
2016-01-23T18:11:57.885+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:37037 #5 (1 connection now open)
2016-01-23T18:29:55.365+0530 I NETWORK [conn5] end connection 127.0.0.1:37037 (0 connections now open)
答案 0 :(得分:17)
确保mongodb守护程序正在运行,并且正在侦听0.0.0.0,而不是127.0.0.1端口
例如,对于我的本地mongodb,它有这个配置:
[vodolaz095@steel ~]$ cat /etc/mongod.conf
##
### Basic Defaults
##
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip = 127.0.0.1
# Specify port number (27017 by default)
#port = 27017
对于mongodb服务器来监听远程连接,你可以改变
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip = 0.0.0.0
答案 1 :(得分:9)
使用mongoDB服务器版本3.6.4,Ubuntu 16.4.4我通过设置/etc/mongod.conf中的net部分解决了这个问题:
net:
port: 27017
bindIpAll: true
# bindIp: 127.0.0.1
答案 2 :(得分:6)
问题是bindIp
没有改变。从我这边重启mongo有一些问题。
习惯应该是验证bindIp是否实际发生了变化。 (使用sudo netstat -tulpn | grep 27017
)
答案 3 :(得分:0)
确保你的machine1上有一个监听端口27017的守护进程。我们收到同样的错误,发现守护进程没有运行。
答案 4 :(得分:0)
我尝试了另一种方法来解决此问题。我已经从
更改了配置文件bind_ip = 127.0.0.1
#port = 27017
到
bind_ip = 0.0.0.0
#port = 27017
希望有帮助。编码愉快;)