刚刚安装好了 mongodb 。对着 mongodb in action上的例子敲了下面的代码:

public class TestDBConnect {
@Test
public void testdb() { MongoClient mongoClient = null;
try {
mongoClient = new MongoClient("xx.xx.xx.xx", 27017);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} DB db = mongoClient.getDB("crawler");
DBCollection coll = db.getCollection("sites"); DBObject doc = new BasicDBObject();
String[] tags = { "database", "open-source" }; doc.put("url", "org.mongodb");
doc.put("tags", tags); DBObject attrs = new BasicDBObject();
attrs.put("lastAddress", new Date());
attrs.put("pingtime", 20); doc.put("attrs", attrs); coll.insert(doc); System.out.println("Initial document:n");
System.out.println(doc.toString()); System.out.println("updating pingtime...n");
coll.update(new BasicDBObject("_id", doc.get("_id")),
new BasicDBObject("$set", new BasicDBObject("pingtime", "30"))); DBCursor cursor = coll.find();
System.out.println("After updaten");
System.out.println(cursor.next().toString());
System.out.println("Number of site documents:" + coll.count());
System.out.println("Removing documents..."); coll.remove(new BasicDBObject());
}
}

上面服务器的IP 用XX.XX.XX.XX隐去了,运行之后报了错:

九月 20, 2013 10:19:52 上午 com.mongodb.DBTCPConnector initDirectConnection

警告: Exception executing isMaster command on localhost/xx.xx.xx.xx:27017

java.net.ConnectException: Connection refused: connect

说白了就是连接不上。

说明一下网络情况:

安装了mongodb的是一台Linux服务器,IP是aa.bb.cc.dd,本机运行java程序去链接。

接下来我又尝试了,在本机上运行mongo客户端 使用

mongo aa.bb.cc.dd来连接,同样失败。

不死心,ssh到服务器上,使用mongo aa.bb.cc.dd来连接,失败,我擦!

不加IP,或者使用mongo 127.0.0.1 可以连接上,到这儿我基本猜出了问题的原因了,mongodb的数据库实例被绑定到了127.0.0.1这个IP了,使用外网IP无法访问。

打开/etc/mongodb.conf.

bind_ip = 127.0.0.1这一行替换为

bind_ip = aa.bb.cc.dd。重新运行上面的代码,测试通过。

mongodb的连接问题,绑定IP惹的祸的更多相关文章

  1. mongodb远程连接配置

    mongodb远程连接配置如下: 1.修改配置文件mongodb.conf 命令:vim /etc/mongodb.conf 把 bind_ip=127.0.0.1 这一行注释掉或者是修改成 bind ...

  2. redis绑定ip以及启动和查看启动状态

    改绑定ip: 或许是对redis的了解还不够多的缘故,单单只是从安装和启动来讲,个人觉得好像是比mongodb和mysql要简单一些. 我的安装包是这个:http://download.csdn.ne ...

  3. windows下远程访问Redis,windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效,Windows Redis requirepass不生效,windows下远程访问redis的配置

    转载:http://fanshuyao.iteye.com/blog/2384074 一.Redis下载地址: https://github.com/MicrosoftArchive/redis/re ...

  4. windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效, Windows Redis requirepass不生效

    windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效, Windows Redis requirepass不生效 >>>>&g ...

  5. 多网卡下对ServerSocket以TCP协议绑定IP和端口的测试

    一.引言:之前开发TCP协议的程序(C#里是Socket为主)都是基于主机上只有一个IP的,后来项目里涉及到了主机需要同时连接内外和外网的情况,在该主机上部署着一套WCS系统和一套WMS系统:WCS系 ...

  6. 阿里云服务器redis启动绑定ip 开放端口仍无法访问问题

    今天使用云服务器其redis 始终无法访问.redis.conf 这个配置文件也是改了又改.最后发现 执行redis启动命令时没有带上配置文件.仍然使用默认配置. src/redis-server  ...

  7. [转载]MongoDB 标准连接字符串

    MongoDB 标准连接字符串 mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[data ...

  8. 配置公网的域名绑定IP

    1. 在万网.美橙申请了一个域名,当然付完费了.   2. 点击"管理",找到了域名解析   3. 点击"域名解析"   注意"记录值",这 ...

  9. Linux tomcat设置ip地址直接访问,tomcat设置ip地址直接访问,tomcat绑定ip地址

    Linux tomcat设置ip地址直接访问,tomcat设置ip地址直接访问,tomcat绑定ip地址 >>>>>>>>>>>> ...

随机推荐

  1. 使用Android平板编程,执行linux命令

    android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...

  2. About abstract class.

    Abstract means should be realized. Virtual means could be overrided. It is very different!

  3. cf C. Dima and Containers

    http://codeforces.com/contest/358/problem/C 将最大的放在stack里面,第二大的放在queue中,第三大的放在deck里面.然后模拟就可以了. #inclu ...

  4. 使用NUnit进行项目的单元测试

    using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; nam ...

  5. 关于IoCallDriver

    通常我们所知IoCallDriver是把irp传递给下一层设备,传递到底是什么意思呢?IoCallDriver中实际调用了IopfCallDriver,其代码如下:NTSTATUSFORCEINLIN ...

  6. authbind start tomcat services as user with less that 1024 ports. linux常规用户使用tomcat的80端口

    Start tomcat services using authbind this will allow user to start ports less than 1024 we do not ne ...

  7. 【转】Windows 7/8/8.1 硬盘安装法实现 ubuntu 14.04 双系统

    原文网址:http://www.cnblogs.com/chenguangqiao/p/4219532.html 一.软件准备 1. 下载 Ubuntu 系统镜像:http://www.ubuntu. ...

  8. c#类和结构体的关系

    原文地址:http://www.dnbcw.com/biancheng/c/fvhc81798.html 简介:这是c#类和结构体的关系的详细页面,介绍了和c/c++,有关的知识,谢谢大家的观看!要查 ...

  9. poj3696:同余方程,欧拉定理

    感觉很不错的数学题,可惜又是看了题解才做出来的 题目大意:给定一个数n,找到8888....(x个8)这样的数中,满足能整除n的最小的x,若永远无法整除n 则输出0 做了这个题和后面的poj3358给 ...

  10. Hadoop 2、配置HDFS HA (高可用)

    前提条件 先搭建 http://www.cnblogs.com/raphael5200/p/5152004.html 的环境,然后在其基础上进行修改 一.安装Zookeeper 由于环境有限,所以在仅 ...