我正在try 创建一个远程mongodb服务器,用于我的java项目.

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

我总是被网络拒绝连接.localhost上的相同代码可以完美地工作.

我用的是 vagrant 盒子:chef/centos-6.5

我的主要任务如下:

- 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

它处于活动状态:

[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

我的java代码定义MongoDbFactory

public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
    MongoClient mongo = new MongoClient("<ip-of-machine>", 27017);
    return new SimpleMongoDbFactory(mongo, "test");
}

但当我try 连接到它时,总是会遇到以下异常.

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}}]

如果我从localhost运行mongodb并相应地更改ip,一切都会正常工作.

谢谢

推荐答案

首先,为了确保这不是一个防火墙问题,请停止两台服务器上的IPTABLES(不要忘记在之后重新启用).

在您试图连接的机器上,直接使用ssh连接到它,并确保MongoDB正在运行,在本地连接并判断您是否可以访问数据库.

默认情况下,MongoDb未配置为接受来自远程主机的连接,您能否确保您的/etc/mongodb.conf文件中有以下行:

bind_ip = 0.0.0.0
port = 27017

确保在进行任何更改后重新启动MongoDB.试试这个,如果你仍然有问题,我会用更多的建议更新答案.

EDIT:从2.6版开始,配置文件格式已更改为YAML,详细信息here,文件应位于/etc/mongod.conf.

net:
   bindIp: 0.0.0.0
   port: 27017

Mongodb相关问答推荐

如何对嵌套在另一个数组中的数组中的字段执行查找?

最有效的Mongo聚合来展开和过滤:匹配、展开、匹配与投影和过滤

除非满足某个条件,否则Mongo是否按日期排序?

如何获取键值对的对象,其中值仅具有 mongoDB 中的投影字段

如何在 MongoDB 中的集合下查找同一文档

Mongo查找条件:不存在

找到一个用户,然后使用 MongoDB 根据他们的总分获得他们的排名

连接到远程 mongo 服务器导致异常连接失败

具有最佳插入/秒性能的数据库?

MongoDB:按标签获取文档

MongoDB获取聚合查询的executionStats

$lookup 结果 mongodb 的计数

mongoose递归填充

在 mongodb 聚合框架中执行 case-statement

在 MongoDB 中合并两个集合

mongodb 的计数性能

在 mongodb 中插入当前日期时间

mongo中的稀疏索引和空值

停止副本集 MongoDB

MongoMapper 和迁移