无法连接到远程服务器上的mongo [英] Unable to connect to mongo on remote server

查看:135
本文介绍了无法连接到远程服务器上的mongo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在本地网络中的machine1(Ubuntu 14.04.3 LTS server)上安装了mongo.我还按照指南,使用以下命令:

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 this guide using following commands:

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(在同一网络中)连接到此端口:


But I am not able to connect to this port from machine2 (in the same network):

$ 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

我不确定我缺少什么.在我第一次设置mongo时,一定是一个愚蠢的错误.请帮助.

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.

更新(针对当前答案中出现的疑问)

  1. 是的,它正在运行.我可以从同一台计算机连接到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
  2. 我将bind_ip更改为0.0.0.0,然后重新启动了mongo.仍然会出现相同的错误.
  1. 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
  2. I changed the bind_ip to 0.0.0.0 and restarted mongo. Still the same error is coming.

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)

推荐答案

问题是bindIp不变.从我这边重新启动mongo时出现问题.

The issue was bindIp didn't change. There was some issue in restarting mongo from my side.

习惯是验证bindIp是否实际更改. (使用sudo netstat -tulpn | grep 27017)

The habit should be to verify if the bindIp actually changed or not. (using sudo netstat -tulpn | grep 27017)

这篇关于无法连接到远程服务器上的mongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆