无法连接到远程mongodb服务器 [英] Failing to connect to remote mongodb server

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

问题描述

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

I am trying to create a remote mongodb server to work with on my java project.

我正在使用vagrant + ansible来创建环境,但是我无法获得连接.

I am using vagrant + ansible to create the environment, but fr soem reson i cannot get a connection.

我总是通过网络拒绝连接.本地主机上的相同代码可以完美地工作.

I always get connection refused through the network. the same code on localhost works perfectly.

我正在使用无业游民的盒子:chef/centos-6.5

I am using vagrant box: 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例外:

i have added the iptables exceptions:

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

我定义MongoDbFactory

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

但是尝试连接到它时,总是会出现以下异常.

But i always get the following exception when trying to connect to it.

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,则一切正常.

If i run mongodb from localhost and change the ip accordingly, everything works fine.

谢谢

推荐答案

首先,要确保它不是防火墙问题,请在两台服务器上都停止IPTABLES(不要忘了以后重新启用).

Firstly, To ensure its not a firewall issue, stop IPTABLES on both servers (don't forget to re-enable afterwards).

在您要连接的计算机上,直接SSH到它并确保MongoDB在运行,在本地连接并检查您是否可以访问数据库.

On the machine that you are trying to connect to, ssh directly to it and ensure MongoDB is running, Connect locally and check that you can access a DB.

默认情况下,

MongoDb未配置为接受来自远程主机的连接,请确保您在/etc/mongodb.conf文件中包含以下行:

MongoDb isn't configured to accept connections from remote hosts by default, could you ensure you have these lines in your /etc/mongodb.conf file:

bind_ip = 0.0.0.0
port = 27017

确保在进行任何更改后重新启动MongoDB.尝试此操作,如果您仍然遇到问题,请发表评论,我会用更多建议来更新答案.

Make sure you restart MongoDB after making any changes. Try this and comment if your still having issues and I'll update the answer with more suggestions.

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

As of version 2.6 the config file format has changed to YAML, details here, the file should be located at /etc/mongod.conf.

net:
   bindIp: 0.0.0.0
   port: 27017

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

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