2016-10-23 115 views
0

只是隨身帶着虛擬服務器,試圖提高我的技能。來自Java的Mongo數據庫連接

所以我試圖連接到本地託管的mongoDB並有一些問題。想迭代一個集合並獲得一些輸出,所以我知道我是連接的東西。問題是我不能看到一個簡單的方法來將集合(我想我有)轉換爲java可以理解的東西。我正在使用3.4 java驅動程序運行mongo3.2。如果有問題,我還運行Ubuntu 16.04。 仍然沒有複製和粘貼到我的虛擬機上工作,所以我會附上幾張顯示數據庫狀態的圖片。 enter image description hereenter image description here 這裏是我的Java應用程序代碼

static String user = "guest"; 
static String database = "VsJogger"; 
static char[] password = {'g','u','e','s','t'}; 
public static void main(String[] args) { 
    MongoCredential credential = MongoCredential.createCredential(user, 
                    database, 
                    password); 

    MongoClientURI uri = new MongoClientURI("mongodb://"+user+":" 
      +String.valueOf(password) 
      +"@localhost/?authSource="+database); 

    try{ 

    // To connect to mongodb server 
    MongoClient mongoClient = new MongoClient(uri); 


    // Now connect to your databases 
    MongoDatabase db = mongoClient.getDatabase("VsJogger"); 
    System.out.println("Connect to database successfully"); 
    MongoCollection coll = db.getCollection("Jog"); 
    MongoCursor<Document> cursor = coll.find().iterator(); 

    int i = 1; 
    while (cursor.hasNext()) { 
     System.out.println("Inserted Document: "+i); 
     System.out.println(cursor.next()); 
     i++; 
    } 


    }catch(Exception e){ 
    System.err.println(e.getClass().getName() + ": " + e.getMessage()); 
    } 

和輸出。

Oct 23, 2016 3:59:45 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 
Connect to database successfully 
Oct 23, 2016 3:59:45 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out 
Oct 23, 2016 3:59:46 PM com.mongodb.diagnostics.logging.JULLogger log 
INFO: Exception in monitor thread while connecting to server localhost:27017 
com.mongodb.MongoSocketOpenException: Exception opening socket 
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) 
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) 
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) 
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) 
    ... 3 more 

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=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}] 
BUILD SUCCESSFUL (total time: 31 seconds) 

回答

0

大概應該已經更清楚了。我正在使用運行在Windows(虛擬盒)上的Ubuntu虛擬機來託管服務器並嘗試從Windows訪問它。

通過修改mongo配置文件來包含虛擬機的192.xx ..地址,然後在通過java連接時使用該地址。環回地址'localhost'對機器來說是唯一的,即。我的虛擬機和我的主系統無法訪問。