无法连接到远程mongodb服务器

Gle*_*eeb 13 linux admin mongodb vagrant

我正在尝试创建一个远程mongodb服务器来处理我的java项目.

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

我总是通过网络拒绝连接.localhost上的相同代码完美运行.

我正在使用流浪盒: 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 
Run Code Online (Sandbox Code Playgroud)

我添加了iptables例外:

sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

这是活跃的:

[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
Run Code Online (Sandbox Code Playgroud)

我的java代码定义 MongoDbFactory

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

但是当我尝试连接它时,我总是得到以下异常.

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}}]
Run Code Online (Sandbox Code Playgroud)

如果我从localhost运行mongodb并相应地更改IP,一切正常.

谢谢

Scr*_*ble 42

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

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

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

bind_ip = 0.0.0.0
port = 27017
Run Code Online (Sandbox Code Playgroud)

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

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

net:
   bindIp: 0.0.0.0
   port: 27017
Run Code Online (Sandbox Code Playgroud)

  • 神圣的母亲是bind_ip.非常感谢.! (7认同)