且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法通过C#客户端连接到MongoDB(MongoLabs)

更新时间:2022-06-03 21:58:46

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

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.