Overview概述
In this guide, you can learn how to connect to MongoDB instances by using a SOCKS5 proxy. SOCKS5 is a standardized protocol for connecting to network services through a proxy server.在本指南中,您可以学习如何使用SOCKS5代理连接到MongoDB实例。SOCKS5是用于通过代理服务器连接到网络服务的标准化协议。
Install the socks Package安装袜子包装
Starting in version 6.0 of the Node.js driver, you must install the 从Node.js驱动程序的6.0版本开始,您必须安装socks package to use SOCKS5 proxy support in your application. You can install socks by running the following command in your shell:socks包才能在应用程序中使用SOCKS5代理支持。您可以通过在shell中运行以下命令来安装socks:
npm i socksSOCKS5 Client OptionsSOCKS5客户端选项
You can set options in your 您可以在MongoClientOptions instance or in your connection URI to configure SOCKS5 proxy support for your connection. The following table describes the client options related to SOCKS5:MongoClientOptions实例或连接URI中设置选项,为您的连接配置SOCKS5代理支持。下表描述了与SOCKS5相关的客户端选项:
proxyHost | string | null | |
proxyPort | non-negative integer | null | proxyHost option, the value of this option defaults to 1080.proxyHost选项,则此选项的值默认为1080。 |
proxyUsername | string | null | |
proxyPassword | string | null |
Important
The driver throws an error if you set the 如果您设置了proxyPort, proxyUsername, or proxyPassword options without setting the proxyHost option.proxyPort、proxyUsername或proxyPassword选项而没有设置proxyHost选项,则驱动程序会抛出错误。
Example示例
This example shows how to instantiate a 此示例显示了如何实例化使用SOCKS5代理支持的MongoClient that uses SOCKS5 proxy support. The following example code specifies proxy server options and connects to MongoDB:MongoClient。以下示例代码指定了代理服务器选项并连接到MongoDB:
// Replace the placeholder with your connection string用连接字符串替换占位符
const uri = "<connection string uri>";
// Replace the placeholders with your SOCKS5 proxy server details用您的SOCKS5代理服务器详细信息替换占位符
const socksOptions = {
proxyHost: "<host>",
proxyPort: 1080,
proxyUsername: "<username>",
proxyPassword: "<password>",
};
// Create a new client with the proxy server details使用代理服务器详细信息创建新客户端
const client = new MongoClient(uri, socksOptions);
Tip
The preceding sample code uses placeholders for the connection URI and proxy server details. To run this code, you must replace these placeholders with the information for your deployment and proxy server.前面的示例代码使用占位符作为连接URI和代理服务器详细信息。要运行此代码,您必须用部署和代理服务器的信息替换这些占位符。
Additional Information附加信息
For more information about SOCKS5 proxy support, see the MongoDB SOCKS5 specification.有关SOCKS5代理支持的更多信息,请参阅MongoDB SOCKS5规范。
API Documentation文档
To learn more about the methods and types discussed in this guide, see the following API Documentation:要了解有关本指南中讨论的方法和类型的更多信息,请参阅以下API文档: