簡體   English   中英

無法連接到遠程mongodb服務器

[英]Failing to connect to remote mongodb server

我正在嘗試創建一個遠程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 

我添加了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"); } 

但是當我嘗試連接它時,我總是得到以下異常。

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。 如果您仍然遇到問題,請嘗試此操作並發表評論,我會通過更多建議更新答案。

編輯:從版本2.6開始,配置文件格式已更改為YAML,詳細信息在此處 ,文件應位於/etc/mongod.conf

net:
   bindIp: 0.0.0.0
   port: 27017

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM