Overview
在本指南中,您可以了解如何使用 SOCKS5 代理连接到 MongoDB。SOCKS5 是一种标准化协议,用于通过代理服务器与网络服务通信。
提示
要学习;了解有关 SOCKS5协议的详情,请参阅有关 SOCKS 的维基百科条目。
SOCKS5 代理设置
代理设置指定 SOCKS5 代理服务器解决和您的身份验证凭证。您可以通过以下方式指定这些设置:
将它们作为参数包含在连接 URI 中。
在
MongoClientSettings实例上,将Socks5ProxySettings属性设立为Socks5ProxySettings类的实例。
下表描述了 SOCKS5 代理设置:
设置 | 说明 |
|---|---|
主机 | Specifies the SOCKS5 proxy IPv4 address, IPv6 address, or hostname.
You must provide this value to connect to a SOCKS5 proxy. |
端口 | Specifies the TCP port number of the SOCKS5 proxy server. Default: 1080 |
身份验证 | Specifies the authentication settings the SOCKS5 proxy server.
You can use the UsernamePassword() method of the Socks5AuthenticationSettings
class to create authentication settings with your credentials. |
例子
以下示例展示了如何使用 SOCKS5 代理连接到MongoDB 部署:
var settings = new MongoClientSettings.FromConnectionString("<connection URI>"); var proxySettings = new Socks5ProxySettings( "<proxy host>", 1, Socks5AuthenticationSettings.UsernamePassword("<username>", "<password>")); settings.Socks5ProxySettings = proxySettings; var client = new MongoClient(settings);
var connectionURI = "mongodb://localhost:27017/" + "?proxyHost=<proxyHost>" + "&proxyPort=<proxyPort>" + "&proxyUsername=<proxyUsername>" + "&proxyPassword=<proxyPassword>"; var client = new MongoClient(connectionURI);
API 文档
要进一步了解本指南所讨论的方法和类型,请参阅以下 API 文档: