java连接mongodb报错

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;">写一个简短的测试程序却报错,郁闷</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="html">public static void main(String[] args) {    

  try {    

   Mongo mongo = new Mongo("10.2.167.27",27017);    

   DB db = mongo.getDB("company");    

   DBCollection collection = db.getCollection("employees");    

   BasicDBObject employee = new BasicDBObject();
   employee.put("name", "Hannah");
   employee.put("no", 2);    

   collection.insert(employee);    

   BasicDBObject searchEmployee = new BasicDBObject();
   searchEmployee.put("no", 2);    

   DBCursor cursor = collection.find(searchEmployee);    

   while (cursor.hasNext()) {
    System.out.println(cursor.next());
   }    

   System.out.println("The Search Query has Executed!");    

  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (MongoException e) {
   e.printStackTrace();
  }    

 }    

报错信息如下:

java.io.IOException: couldn‘t connect to [/10.2.167.27:27017] bc:java.net.ConnectException: Connection refused: connect

at com.mongodb.DBPort._open(DBPort.java:224)

at com.mongodb.DBPort.go(DBPort.java:101)

at com.mongodb.DBPort.go(DBPort.java:82)

at com.mongodb.DBPort.findOne(DBPort.java:142)

at com.mongodb.DBPort.runCommand(DBPort.java:151)

at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:429)

at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:544)

at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:244)

at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:217)

at com.mongodb.DBCollection.insert(DBCollection.java:87)

at mongodb.MongoDBClient.main(MongoDBClient.java:28)

看看服务器的端口:tcp        0      0 127.0.0.1:27017             0.0.0.0:*                   LISTEN      31933/mongod

说明端口监听已ok。那就是配置问题

打开vim /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.

bind_ip=127.0.0.1

看到一条这样的记录。端口对谁开放。注释了。ok就可以远程访问了。

得到正确结果如下:

{ "_id" : { "$oid" : "54a8fcad2d1dc6f5db50564b"} , "name" : "Hannah" , "no" : 2}

The Search Query has Executed!

时间: 2024-11-17 10:32:01

java连接mongodb报错的相关文章

java连接MongoDB报错解决

报错内容 com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKN

java连接sqlserver2008报错 java.sql.SQLException: 对象名 &#39;表名&#39; 无效.

注意:c3p0的数据库配置方式为: <named-config name="sqlsvr"> <property name="driverClass">net.sourceforge.jtds.jdbc.Driver</property> <property name="jdbcUrl">jdbc:jtds:sqlserver://localhost:1433/WaterNet_DaFeng<

关于 64位系统 java连接access 报错java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

报错的原因是url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:/公司/2000.mdb"; 这样是不行滴,要改成 url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:/公司/2000.mdb"; 我去,网上找了好久好久,才找到...

Java连接MySQL报错:CommunicationsException: Communications link failure

现象: 报错:Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 解决办法: 更换 mysql-connector-java-8.0.11.jar 为 mysql-connector-java-8.0.16.jar, 下载位置:https://mvnrepository.com/artifact/mysql/mysql-c

Java 连接Kafka报错java.nio.channels.ClosedChannelExcep

Java 客户端连接Kafka报如下错误 bin/kafka-console-consumer.sh --zookeeper 255.255.255.255:2181 --topic eventbustopic [2015-06-02 16:23:04,375] WARN Fetching topic metadata with correlation id 0 for topics [Set(eventbustopic)] from broker [id:1,host:SOME_HOST,po

jdbc连接sqlserver报错java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

使用2008的数据库, 我已经引入的sqljdbc4的包,单还是报这个错,很奇怪突然想到在配置hibernate的时候,是拷贝下来的代码 然后到网上查了下, 因为是2008的版本驱动和2000的有点不同, 之前的都是可能是2000或者2005的class是com.microsoft.jdbc.sqlserver.SQLServerDriver 可是2008  去是com.microsoft.sqlserver.jdbc.SQLServerDriver 就这么改过来就可以了 jdbc连接sqlse

连接mysql报错java.sql.SQLException: The server time zone value &#39;?й???????&#39; is unrecognized...解决方法

报错内容: java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time

redis运用连接池报错解决

redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.jav

连接mysql报错:error 2003 (hy000):can&#39;t connect to mysql server on &#39;localhost&#39; (10061)

一.mysql 的bin目录下有个MySQLInstanceConfig.exe,运行就可以进行创建数据库实例,创建实例时也可以生成windows 服务,把服务设置成自动启动就可以了 二.安装在D盘的discuzz!打开后出现上述错误,请问应如何处理?winmysqladmin已经启动,但三色树上有一个小红点如果出现"ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)",说明你的MySQL还没有启动.解决办法