我最近设置了一个数字海洋服务器并在其上安装了mongo。我已经创建了ssh公钥和私钥以便能够登录但我不认为我实际上需要使用ssh或服务器本身的凭据,而不仅仅是我的服务器的IP和Mongo服务器的凭据。
2.最重要的是,当我尝试连接时,我遇到了错误。我叫Mongo()进行连接。
错误说 MongoDB.Driver.Core.dll中出现“System.ArgumentException”类型的异常,但未在用户代码中处理
其他信息:'mongodb:// MongoUserName:MongoPassword@XXX.XX.XXX.XXX:27017 / admin:27017'不是有效的终点。
public static class DataSources
{
public static string URIString = "mongodb://"+s_Username
+":"+s_Password+"@"+ip+":"+port+"/admin";
public static string ip = "XXX.XX.XXX.XXX";
public static string port = "27017";
public static string s_Username = "MongoUsername";
public static string s_Password = "MongoPassword";
public static MongoCredential GetMongoCredentials()
{
return MongoCredential.CreateCredential("admin", s_Username, s_Password);
}
public static MongoClientSettings GetMongoSettings()
{
var settings = new MongoClientSettings
{
Credentials = new[] { GetMongoCredentials() },
Server = new MongoServerAddress(URIString)
};
return settings;
}
public static MongoClient Mongo()
{
MongoClient mongoClient = new MongoClient(GetMongoSettings());
return mongoClient;
}
}
答案 0 :(得分:0)
MongoServerAddress
构造函数不接受完整的连接字符串,而只接受服务器和端口,如下所示:
new MongoServerAddress(server, port)
另一方面,MongoClient可以解析完整的连接字符串,所以它应该足够说:
MongoClient mongoClient = new MongoClient(URIString) and drop most of the rest of your code.