Docs Menu
Docs Home
/ / /
Kotlin Coroutine
/ /

Connect to MongoDB by Using a SOCKS5 Proxy

On this page

  • Overview
  • SOCKS5 Proxy Settings
  • Examples
  • Specify Proxy Settings in the MongoClientSettings
  • Specify Proxy Settings in the Connection String
  • API Documentation

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.

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.

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 section. Replace the placeholders with your proxy specifications and credentials.

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()
)

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

val connectionString = ConnectionString(
"mongodb+srv://<user>:<password>@<cluster-url>/?" +
"proxyHost=<proxyHost>" +
"&proxyPort=<proxyPort>" +
"&proxyUsername=<proxyUsername>" +
"&proxyPassword=<proxyPassword>"
)
val mongoClient = MongoClient.create(connectionString)

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

Back

Enable TLS/SSL on a Connection