1
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
public class DBHandler : MonoBehaviour {
string connectionString = "mongodb://localhost:27017";
void Start() {
/*
* Establish connection
*/
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("unity");
var shopcollections= database.GetCollection<BsonDocument>("vrshop");
Debug.Log ("Connected!!0.0");
//print all collections
foreach (var document in shopcollections.FindAll()) {
Debug.Log ("Get all info: \n" + document);
}
}
}
這些代碼用來工作,它可以連接到成功的MongoDB,但是,在今天,當我嘗試連接它,它出來一個錯誤:連接的MongoDB用C#團結錯誤
MongoConnectionException: Unable to resolve host name 'localhost'.
MongoDB.Driver.MongoServerAddress.ToIPEndPoint (AddressFamily addressFamily)
MongoDB.Driver.MongoServerInstance.GetIPEndPoint()
MongoDB.Driver.Internal.MongoConnection.Open()
MongoDB.Driver.Internal.MongoConnection.GetNetworkStream()
MongoDB.Driver.Internal.MongoConnection.SendMessage (MongoDB.Bson.IO.BsonBuffer buffer, Int32 requestId)
Rethrow as MongoConnectionException: Unable to connect to server localhost:27017: Unable to resolve host name 'localhost'..
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect (TimeSpan timeout, MongoDB.Driver.ReadPreference readPreference)
MongoDB.Driver.Internal.DirectMongoServerProxy.ChooseServerInstance (MongoDB.Driver.ReadPreference readPreference)
MongoDB.Driver.MongoServer.AcquireConnection (MongoDB.Driver.ReadPreference readPreference)
MongoDB.Driver.MongoCursor`1+MongoCursorConnectionProvider[MongoDB.Bson.BsonDocument].AcquireConnection()
MongoDB.Driver.Operations.QueryOperation`1[MongoDB.Bson.BsonDocument].GetFirstBatch (IConnectionProvider connectionProvider)
MongoDB.Driver.Operations.QueryOperation`1[MongoDB.Bson.BsonDocument].Execute (IConnectionProvider connectionProvider)
MongoDB.Driver.MongoCursor`1[MongoDB.Bson.BsonDocument].GetEnumerator()
DBHandler.Start() (at Assets/DBHandler.cs:24)
我相信我使用相同的mongodb ....它有什麼問題?
首先檢查您的計算機上的任何內容可以作爲'localhost'訪問。嘗試ping它,如果你有web服務器運行嘗試訪問它在瀏覽器中作爲http:// localhost /等檢查防火牆設置,如果你有一個運行。檢查「主機」文件是否有奇怪的記錄。 – GSazheniuk
是的,我可以從我的Robomongo訪問本地主機:27017 ...我想這意味着我可以訪問它....但是,我不知道爲什麼團結? –
如果您正在運行防火牆,可能會有基於應用程序的規則。所以你的統一應用程序可能沒有適當的設置來訪問本地主機上的27017。如果您的Unity應用程序最近更新了,它可能需要更新防火牆規則。 – GSazheniuk