无法通过C#客户端连接到MongoDB(MongoLabs) [英] Unable to connect to MongoDB (MongoLabs) via C# client

查看:370
本文介绍了无法通过C#客户端连接到MongoDB(MongoLabs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题背景:

我在MongoLabs(mLab- https://mlab.com/)中进行了设置,并添加了一个非常简单的收藏.我正在使用MongoDB驱动程序尝试通过C#3.2驱动程序连接并使用此集合.

I have setup in MongoLabs (mLab - https://mlab.com/) a database and have added a very simple Collection. I am using the MongoDB driver to attempt to connect and work with this collection via the C# 3.2 driver.

问题:

我无法通过具有恒定身份验证期望的C#驱动程序连接到数据库,如下所示:

I am unable to connect to my database via the C# driver with a constant authentication expection, as shown:

System.TimeoutException:在使用CompositeServerSelector {Selectors = ReadPreferenceServerSelector {ReadPreference = {Mode = Primary,TagSets = []}},LatencyLimitingServerSelector {AllowedLatencyRange = 00:00:00.0150000}}选择服务器30000ms之后发生超时.群集状态的客户端视图为{ClusterId:"1",ConnectionMode:自动",类型:未知",状态:断开连接",服务器:[{ServerId:"{ClusterId:1,端点:" Unspecified/ds048719. mlab.com:48719}",端点:"Unspecified/ds048719.mlab.com:48719",状态:"Disconnected",类型:"Unknown",HeartbeatException:"MongoDB.Driver.MongoConnectionException:打开---> MongoDB.Driver.MongoAuthenticationException:无法使用sasl协议机制SCRAM-SHA-1进行身份验证---> MongoDB.Driver.MongoCommandException:命令saslStart失败:身份验证失败.

System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/ds048719.mlab.com:48719" }", EndPoint: "Unspecified/ds048719.mlab.com:48719", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed.

代码:

我尝试了多种尝试对请求进行身份验证的方法.目前,我正在尝试仅使用MongoClient类,如下所示:

I have tried a number of different ways of trying to authenticate the request. Currently I am trying to simply use the MongoClient class, as shown:

MongoClient client;

var connectionString = "mongodb://userNameGoesHereRemovedForSO:passwordGoesHereRemovedForSO@ds048555.mlab.com:48719/db";

client = new MongoClient(connectionString);

var database = client.GetDatabase("testDB");

var collection = database.GetCollection<string>("test");

我们将非常感谢您提供任何帮助或示例,说明如何成功克服此身份验证问题.

推荐答案

如果我不得不冒险猜测,那么该问题很可能是防火墙问题.您应该检查以下内容

If I had to hazard a guess, the issue is most likely a firewall issue. You should check the following

    C#应用程序主机中主机(ds048719.mlab.com)的
  • nslookup
  • C#应用程序主机中主机(ds048719.mlab.com)的
  • ping(可能会失败,具体取决于mLab的设置)
  • 您的IP地址已已列入白名单
  • 在运行C#应用程序的同一主机上使用Mongo Shell测试连接. mLab在此处提供文档.
  • 使用原始的telnet(例如telnet ds048719.mlab.com 48719
  • )测试连接
  • 确保您使用的是正确的authenticationDatabase(在您的示例中,这是由/db指定的),通常为admin,但是如果您在共享实例上,则可以是您的数据库名称.
  • nslookup of the host (ds048719.mlab.com) from the C# Application Host
  • ping of the host (ds048719.mlab.com) from the C# Application Host (might fail, depending on mLab's settings)
  • That your IP address is whitelisted
  • Test the connection using the Mongo Shell from the same host where the C# Application is running. mLab has docs here.
  • Test the connection with a raw telnet, eg telnet ds048719.mlab.com 48719
  • Ensure you are using the correct authenticationDatabase (in your example, this is specified by the /db), this is usually admin but could be your database name if you are on a shared instance.

您可以在

You can find the docs on connecting with the C# driver in the MongoDB C# Driver Docs. It is important to note the following:

数据库组件

数据库组件是可选的,用于指示要针对哪个数据库进行身份验证.如果未提供数据库组件,则使用"admin"数据库.

The database component is optional and is used to indicate which database to authenticate against. When the database component is not provided, the "admin" database is used.

mongodb://host:27017/mydb

上面,名称为"mydb"的数据库是存储应用程序凭据的位置.

Above, the database by the name of "mydb" is where the credentials are stored for the application.

注意:

某些驱动程序利用数据库组件来指示默认情况下要使用的数据库. .NET驱动程序在解析数据库组件时,除了身份验证外,不将其用于其他任何用途.

Some drivers utilize the database component to indicate which database to work with by default. The .NET driver, while it parses the database component, does not use the database component for anything other than authentication.

最后,我建议将来在发布到SO时混淆主机名和端口.虽然仅靠模糊性来保证安全性是不好的策略,但它无疑为MongoDB部署增加了一层防御.

Finally, I would suggest in the future, obfuscate the hostname and port when posting to SO. While security through obscurity alone is a bad policy, it certainly adds a layer of defense for your MongoDB deployment.

这篇关于无法通过C#客户端连接到MongoDB(MongoLabs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆