无法连接到远程Mongodb服务器

18

我正在尝试创建一个远程mongodb服务器,以便在我的Java项目中使用。

我正在使用vagrant + ansible创建环境,但由于某种原因我无法建立连接。

我总是通过网络得到连接被拒绝的错误。同样的代码在本地主机上完美运行。

我正在使用vagrant box:chef/centos-6.5,我的ansible任务如下:

- name: Download EPEL Repo
  get_url: url=http://dl.fedoraproject.org/pub/epel/6/x86_64/epel- release-6-8.noarch.rpm dest=/tmp/epel-release-6-8.noarch.rpm
- name: Install EPEL Repo
  command: rpm -ivh /tmp/epel-release-6-8.noarch.rpm creates=/etc/yum.repos.d/epel.repo
- name: Install libselinux-python
  yum: name=libselinux-python
- name: Create the repository for 10Gen  
  copy: src=10gen.repo.j2 dest=/etc/yum.repos.d/10gen.repo
- name: Create the EPEL Repository.
  copy: src=epel.repo.j2 dest=/etc/yum.repos.d/epel.repo
- name: Create the GPG key for EPEL
  copy: src=RPM-GPG-KEY-EPEL-6 dest=/etc/pki/rpm-gpg
- name: Create the mongod user
  user: name=mongod comment="MongoD"
- name: Create the data directory for the namenode metadata
  file: path={{ mongodb_datadir_prefix }} owner=mongod group=mongod state=directory
- name: Install the mongodb package
  yum: name={{ item }} state=installed
  with_items:
   - libselinux-python
   - mongo-10gen
   - mongo-10gen-server
   - bc
   - python-pip
- name: Install the latest pymongo package
  pip: name=pymongo state=latest use_mirrors=no 
我已经添加了iptables的例外规则:
sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

活跃的:

which is active:
[root@localhost ~]# iptables -L -v
Chain INPUT (policy ACCEPT 209 packets, 13148 bytes)
 pkts bytes target     prot opt in     out     source               destination
 1070 68777 ACCEPT     tcp  --  any    any     anywhere             anywhere            tcp dpt:27017

我定义了一个MongoDbFactory的Java代码:

public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
    MongoClient mongo = new MongoClient("<ip-of-machine>", 27017);
    return new SimpleMongoDbFactory(mongo, "test");
}
但是每次尝试连接它时,我总是会遇到以下异常情况。
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=192.168.1.11:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]

如果我从本地主机运行MongoDB并相应地更改IP,一切都正常。

谢谢


你能够telnet到目标地址和端口吗?telnet <机器的IP> 27017 - Chris Fei
不行:MacBook-Pro:~ me$ telnet 192.168.1.11:27017 192.168.1.11:27017:未提供节点名或服务名,或者无法识别。虽然ping命令可以使用。 - Gleeb
1
不对,使用telnet时主机和端口之间不要使用冒号。telnet 192.168.1.11 27017 - Chris Fei
抱歉,但仍然无法连接,显示“Connection refused”。MacBook-Pro:~ me$ telnet 192.168.1.11 27017 正在尝试连接 192.168.1.11... telnet: 无法连接到地址 192.168.1.11: 连接被拒绝 telnet: 无法连接到远程主机 - Gleeb
1
是的,如果无法连接,可能是因为你试图连接错误的主机。你的iptables规则乍一看起来没问题。你可以ping一下那个主机吗? - Chris Fei
1个回答

50

首先,为了确保不是防火墙问题,请停止两个服务器上的IPTABLES(别忘了重新启用)。

在您尝试连接的计算机上,直接通过ssh连接并确保MongoDB正在运行,然后在本地连接并检查是否可以访问数据库。

MongoDB默认未配置为接受来自远程主机的连接,请确保您的/etc/mongodb.conf文件中有以下这些行:

bind_ip = 0.0.0.0
port = 27017

在进行任何更改之后,请确保重新启动MongoDB。尝试此操作,如果仍有问题,请发表评论,我会用更多的建议更新答案。

编辑:从2.6版本开始,配置文件格式已更改为YAML,详情参见此处,文件应位于/etc/mongod.conf

net:
   bindIp: 0.0.0.0
   port: 27017

9
天哪,原来是绑定IP的问题。非常感谢! - Gleeb
1
请确保您的MongoClient连接到正确的端口27017,URL模式应为mongodb:// localhost:27017 / my_database_name。在Ubuntu上,请通过命令service mongodb restart重启服务。 - Abhijeet
@Scriptable - 我们能否指定特定服务器的IP地址,而不是使用0.0.0.0?0.0.0.0可能存在安全问题。 - Sachin Vairagi
@SachinVairagi 如果你喜欢的话,可以阅读文档并尝试一下 :) - Scriptable
@Scriptable - 感谢您的回复,顺便说一句,我尝试了一下但没有成功 :( - Sachin Vairagi
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接