mongodb 无法连接到远程服务器上的 mongo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34963522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to connect to mongo on remote server
提问by Abhishek Gupta
I have installed mongo on machine1(Ubuntu 14.04.3 LTS server
) in my local network. I have also opened the port 27017
as mentioned in thisguide using following commands:
我已经Ubuntu 14.04.3 LTS server
在本地网络的machine1( )上安装了 mongo 。我还使用以下命令打开了本指南中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
Current rules are (iptables -L
):
目前的规则是(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
But I am not able to connect to this port from machine2 (in the same network):
但是我无法从 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
I also tried checking if port is opened or not:
我还尝试检查端口是否打开:
$ nc -v 192.168.0.108 27017
nc: connectx to 192.168.0.108 port 27017 (tcp) failed: Connection refused
I am not sure what I am missing. Must be some silly mistake as I am setting up mongo for the first time. Pls help.
我不确定我错过了什么。当我第一次设置 mongo 时,一定是一些愚蠢的错误。请帮忙。
Update (for the doubts raised in current answers)
更新(针对当前答案中提出的疑问)
- Yeah its running. I am able to connect to mongo form the same machine but not from the other machine. The daemon is running:
$ service mongod status mongod start/running, process 31205
and the port is 27017$ sudo netstat -tulpn |grep 27017 tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 31205/mongod
- I changed the
bind_ip
to0.0.0.0
and restarted mongo. Still the same error is coming.
- 是的,它正在运行。我可以从同一台机器连接到 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 logs:
蒙戈日志:
$ 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)
采纳答案by Abhishek Gupta
The issue was bindIp
didn't change. There was some issue in restarting mongo from my side.
问题是bindIp
没有改变。从我这边重新启动 mongo 时出现了一些问题。
The habit should be to verify if the bindIp actually changed or not. (using sudo netstat -tulpn | grep 27017
)
习惯应该是验证 bindIp 是否真的改变了。(使用sudo netstat -tulpn | grep 27017
)
回答by vodolaz095
make sure that mongodb daemon is running, and listening on 0.0.0.0, but not 127.0.0.1 port
确保 mongodb 守护进程正在运行,并监听 0.0.0.0,而不是 127.0.0.1 端口
for example, for my local mongodb, it has this config:
例如,对于我的本地 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
for mongodb server to listen for remote connections, you can change
对于mongodb服务器监听远程连接,你可以改变
# Comma separated list of ip addresses to listen on (all local ips by default)
bind_ip = 0.0.0.0
回答by Mirko
With mongoDB server version 3.6.4, Ubuntu 16.4.4 I solved this by setting the net section in /etc/mongod.conf like this:
使用 mongoDB 服务器版本 3.6.4,Ubuntu 16.4.4 我通过在 /etc/mongod.conf 中设置 net 部分解决了这个问题,如下所示:
net:
port: 27017
bindIpAll: true
# bindIp: 127.0.0.1
回答by Arun Wilson
I have tried a different way to solve this issue. I have changed the config file from
我尝试了不同的方法来解决这个问题。我已经将配置文件从
bind_ip = 127.0.0.1
#port = 27017
to
到
bind_ip = 0.0.0.0
#port = 27017
Hope it helps. Happy coding ;)
希望能帮助到你。快乐编码;)
回答by Ashish Anand
Make sure you have a daemon running on your machine1 which listen to port 27017. We were getting the same error and found that daemon was not running.
确保你的机器 1 上有一个守护进程在运行,它监听端口 27017。我们遇到了同样的错误,发现守护进程没有运行。