我尝试过的事情:
InetAddress address = InetAddress.getByName("MyPCName");
MongoClientOptions.Builder builder = MongoClientOptions.builder().connectTimeout(3000);
MongoClient mongo = new MongoClient(new ServerAddress(address.getHostAddress(), 3001), builder.build());
try {
mongo.getAddress();
} catch (Exception e) {
System.out.println("Mongo is down");
mongo.close();
}
第二次尝试:
InetAddress address = InetAddress.getByName("MyPCName");
MongoClientURI uri = new MongoClientURI( "mongodb://"+address.getHostAddress()+":27017/"+TEST_SKETCH_APP );
MongoClient instance = MongoDatabaseConnection.getInstance(uri);
try {
instance .getAddress();
} catch (Exception e) {
System.out.println("Mongo is down");
instance .close();
}
FireWall配置正确:
netsh advfirewall firewall add rule name="Allowing mongod" dir=in action=allow program=" C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe"
但是当我尝试连接以执行插入操作时,我收到此错误消息:
Timed out after 3000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=192.168.138.1:3001, type=Unknown, state=Connecting}]
我正在AsyncTask中执行保存操作。我的mongo数据库服务器正在" MyPCName"电脑。我的Mongodb配置也有绑定ip评论。我也试过绑定Ip来保持这个bind_ip = 127.0.0.1,***.***.***.*,0.0.0.0
' *' mark是我执行address.getHostAddress()
时获得的IP地址。
我现在被困在这里。
答案 0 :(得分:0)
试试这段代码:
MongoClientURI mongoUri = new MongoClientURI("mongodb://Dbuser:dbpass@ds047692.mongolab.com:47692");
MongoClient mongoClient = new MongoClient(mongoUri);
DB db = mongoClient.getDB("testdb");
Set<String> collectionNames = db.getCollectionNames();
此外,您应该查看以下答案:https://stackoverflow.com/a/21555200/4810206。