Connect to MongoDB by Using a SOCKS5 Proxy

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

Connect to MongoDB by Using a SOCKS5 Proxy

Overview

In this guide, you can learn how to use the MongoDB Kotlin Driver to connect to MongoDB by using a SOCKS5 proxy. SOCKS5 is a standardized protocol for communicating with network services through a proxy server.

Tip:

To learn more about the SOCKS5 protocol, see the Wikipedia entry on SOCKS.

SOCKS5 Proxy Settings

The proxy settings specify the SOCKS5 proxy server address and your authentication credentials. You can specify your settings in an instance of MongoClientSettings or in your connection string.

The following table describes the SOCKS5 client options:

Name Accepted Values Description
proxyHost String Specifies the SOCKS5 proxy IPv4 address, IPv6 address, or hostname. You must provide this value to connect to a SOCKS5 proxy.
proxyPort Non-negative integer Specifies the TCP port number of the SOCKS5 proxy server. If you set a value for proxyHost, this option defaults to 1080, but you can specify a different port number.
proxyUsername String Specifies the username for authentication to the SOCKS5 proxy server. The driver ignores null and empty string values for this setting. The driver requires that you pass values for both proxyUsername and proxyPassword or that you omit both values.
proxyPassword String Specifies the password for authentication to the SOCKS5 proxy server. The driver ignores null and empty string values for this setting. The driver requires that you pass values for both proxyUsername and proxyPassword or that you omit both values.

Examples

The following examples show how to instantiate a MongoClient that connects to MongoDB by using a SOCKS5 proxy. The proxy settings can be specified in a MongoClientSettings instance or a connection string. These examples use the placeholder values described in the [SOCKS5 Proxy Settings](https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/security/socks5.md#std-label-kotlin-socks-proxy-settings section. Replace the placeholders with your proxy specifications and credentials.

Specify Proxy Settings in the MongoClientSettings

The following code example shows how to specify SOCKS5 proxy settings by using the applyToSocketSettings() builder method when creating a MongoClientSettings instance:

val uri = "<connection string>"

val mongoClient = MongoClient.create(
    MongoClientSettings.builder()
        .applyConnectionString(ConnectionString(uri))
        .applyToSocketSettings{ builder ->
            builder
                .applyToProxySettings{ proxyBuilder ->
                    proxyBuilder
                        .host("<proxyHost>")
                        .port("<proxyPort>".toInt())
                        .username("<proxyUsername>")
                        .password("<proxyPassword>")
                        .build()
                }
        }
        .build()
)

Specify Proxy Settings in the Connection String

The following code example shows how to specify SOCKS5 proxy settings in your connection string:

val connectionString = ConnectionString(
    "mongodb+srv://<db_username>:<db_password>@<cluster-url>/?" +
        "proxyHost=<proxyHost>" +
        "&proxyPort=<proxyPort>" +
        "&proxyUsername=<proxyUsername>" +
        "&proxyPassword=<proxyPassword>"
)

val mongoClient = MongoClient.create(connectionString)

API Documentation

To learn more about the methods and types discussed in this guide, see the following API documentation: