我正在使用mongo-java-driver-2.11.3.jar
并尝试使用mongo-2.10.1.jar
两者都出现以下错误。数据库是
已连接,但集合插入和选择无效。
import java.net.UnknownHostException;
import java.util.Set;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
public class mongo_test {
public MongoClient mongo = null;
public DB mongodb = null;
private void mongo_startconnection() {
try {
mongo = new MongoClient("10.0.2.15", 27017);
mongodb = mongo.getDB("foobar");
System.out.println("Mongodb is connected");
BasicDBObject doc = new BasicDBObject();
DBCollection collection = mongodb.getCollection("url_ta");
Set<String> colls = mongodb.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
System.out.println("done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e1) {
e1.printStackTrace();
}
}
public static void main(String args[]) {
mongo_test MT = new mongo_test();
MT.mongo_startconnection();
}
}
我收到了这个错误:
Mongodb is connected
Nov 26, 2013 11:34:39 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on /10.0.2.15:27017
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mongodb.DBPort._open(DBPort.java:223)
at com.mongodb.DBPort.go(DBPort.java:125)
at com.mongodb.DBPort.go(DBPort.java:106)
at com.mongodb.DBPort.findOne(DBPort.java:162)
at com.mongodb.DBPort.runCommand(DBPort.java:170)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:547)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:526)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:236)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DB.getCollectionNames(DB.java:400)
at mongo_test.mongo_startconnection(mongo_test.java:29)
at mongo_test.main(mongo_test.java:69)
com.mongodb.MongoException$Network: Read operation to server /10.0.2.15:27017 failed on database foobar at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DB.getCollectionNames(DB.java:400)
at mongo_test.mongo_startconnection(mongo_test.java:29)
at mongo_test.main(mongo_test.java:69)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mongodb.DBPort._open(DBPort.java:223)
at com.mongodb.DBPort.go(DBPort.java:125)
at com.mongodb.DBPort.call(DBPort.java:92)
at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
... 6 more
答案 0 :(得分:2)
1.想知道您的IP地址和端口号是否可以访问。
使用命令:telnet 10.0.2.15",27017
来检查此ip的端口是否打开以进行连接。
2.如果您有权登录mongodb服务器,只需登录然后:
如果您的mongodb是一个群集,请使用命令:ps -ef|grep mongos
检查mongodb服务器实例是否已启动且端口是否正常
27017
如果你的mongodb只是一台机器,请使用ps -ef|grep mongod
进行同样的检查。
答案 1 :(得分:0)
package raamji.com.start;
//import com.mongodb.MongoClient;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Start {
public static void main(String[] args) throws UnknownHostException {
//crete Connection
Mongo mongoClient = new Mongo("ind-asingh", 27017);//or "localhost",27017
System.out.println("mongoClient :" + mongoClient);
DB db = mongoClient.getDB("DB Name");//e.g. MYDB
System.out.println("db :" + db);
//---------------------------Access your Collection-----------------------------
DBCollection collection = db.getCollection("message");
System.out.println("collection :" + collection);
System.out.println("Message Count: " + collection.getCount());
DBCursor cursor = collection.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}//end pf psvm
}//end of class