无法连接到远程服务器上的 mongo
Posted
技术标签:
【中文标题】无法连接到远程服务器上的 mongo【英文标题】:Unable to connect to mongo on remote server 【发布时间】:2016-04-30 01:06:10 【问题描述】:我已经在本地网络的 machine1(Ubuntu 14.04.3 LTS server
) 上安装了 mongo。我还使用以下命令打开了27017
指南中提到的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。请帮忙。
更新(针对当前答案中提出的疑问)
-
是的,它正在运行。我能够从同一台机器连接到 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)
【问题讨论】:
是否可以在某处添加 mongod 服务器日志?最初的 100 行就足够了。 @nobody 更新了 mongo 日志 希望你已经从日志中得到了线索。 是的,有问题***.com/a/35063229/742173 【参考方案1】:确保 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
【讨论】:
我更改了bind_ip
并重新启动了mongo。仍然会出现同样的错误。
防火墙打开了吗?使用什么linux发行版?是否启用 SELINUX? mongodb日志中是否有任何信息?
防火墙打开是什么意思?我对防火墙了解不多。我刚刚在iptables
中添加了规则。发行版:$ cat /etc/*release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS" NAME="Ubuntu" VERSION="14.04.3 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.3 LTS" VERSION_ID="14.04"
日志中没有任何内容
所以防火墙规则(iptables -l
)看起来不错,端口正确,监听地址(配置中的bind_ip
),而且我更熟悉基于 RPM 的发行版而不是基于 DEB 的发行版能够提供帮助。所以我想你可以把这个问题发到askubuntu.com
是的,mongo db 应该 eb 运行在 0.0.0.0【参考方案2】:
使用 mongoDB 服务器版本 3.6.4,Ubuntu 16.4.4 我通过在 /etc/mongod.conf 中设置 net 部分来解决这个问题:
net:
port: 27017
bindIpAll: true
# bindIp: 127.0.0.1
【讨论】:
【参考方案3】:问题是bindIp
没有改变。从我这边重新启动 mongo 时出现了一些问题。
习惯应该是验证bindIp是否真的改变了。 (使用sudo netstat -tulpn | grep 27017
)
【讨论】:
bindIp 将如何变化?我手动将远程地址添加到bindIp下的mongodb.conf
,但仍然无法连接
评论 bindIp
无效。我不得不把它改成0.0.0.0
【参考方案4】:
我尝试了不同的方法来解决这个问题。我已经从
更改了配置文件bind_ip = 127.0.0.1
#port = 27017
到
bind_ip = 0.0.0.0
#port = 27017
希望对您有所帮助。快乐编码;)
【讨论】:
帮助就像魅力,但你不认为这可能是脆弱的。我绑定了我的实例 IP 但无法远程连接任何建议?【参考方案5】:确保您的机器 1 上运行了一个守护程序,它侦听端口 27017。我们遇到了同样的错误,发现守护程序没有运行。
【讨论】:
是的,它正在运行。我能够从同一台机器连接到 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
以上是关于无法连接到远程服务器上的 mongo的主要内容,如果未能解决你的问题,请参考以下文章
无法连接到“localhost”(10061)上的 MySQL 服务器(主要是远程连接)
phpMyAdmin 无法从 Linux 客户端连接到 Windows 7 上的远程服务器