MONGOC_ADVANCED_CONNECTIONS(3)
π MONGOC_ADVANCED_CONNECTIONS β the libmongoc glow-up guide
okay bestie, this is the MONGOC_ADVANCED_CONNECTIONS page from the libmongoc manual, and it's literally your cheat sheet for connecting to every kind of MongoDB setup that isn't just "one lonely server". grab a boba, we're doing this π§β¨
β¨ DESCRIPTION β¨
The following guide contains information specific to certain types of MongoDB configurations.
For an example of connecting to a simple standalone server, see the Tutorial. To establish a connection with authentication options enabled, see the Authentication page. To see an example of a connection with data compression, see the Data Compression page.
π CONNECTING TO A REPLICA SET
Connecting to a replica set is much like connecting to a standalone MongoDB server. Simply specify the replica set name using the ?replicaSet=myreplset URI option.
#include <bson/bson.h>
#include <mongoc/mongoc.h>
int
main (int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_init ();
/* Create our MongoDB Client */
client = mongoc_client_new (
"mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset");
/* Do some work */
/* TODO */
/* Clean up */
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
TIP: π‘ Multiple hostnames can be specified in the MongoDB connection string URI, with a comma separating hosts in the seed list. It is recommended to use a seed list of members of the replica set to allow the driver to connect to any node.
π CONNECTING TO A SHARDED CLUSTER
To connect to a sharded cluster, specify the mongos nodes the client should connect to. The C Driver will automatically detect that it has connected to a mongos sharding server.
If more than one hostname is specified, a seed list will be created to attempt failover between the mongos instances.
WARNING: π¨ Specifying the replicaSet parameter when connecting to a mongos sharding server is invalid. like, do NOT do that, fr fr.
#include <bson/bson.h>
#include <mongoc/mongoc.h>
int
main (int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_init ();
/* Create our MongoDB Client */
client = mongoc_client_new ("mongodb://myshard01:27017/");
/* Do something with client ... */
/* Free the client */
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
π¦ CONNECTING TO AN IPV6 ADDRESS
The MongoDB C Driver will automatically resolve IPv6 addresses from host names. However, to specify an IPv6 address directly, wrap the address in [].
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://[::1]:27017");
π CONNECTING WITH IPV4 AND IPV6
If connecting to a hostname that has both IPv4 and IPv6 DNS records, the behavior follows RFC-6555. A connection to the IPv6 address is attempted first. If IPv6 fails, then a connection is attempted to the IPv4 address. If the connection attempt to IPv6 does not complete within 250ms, then IPv4 is tried in parallel. Whichever succeeds connection first cancels the other. The successful DNS result is cached for 10 minutes.
As a consequence, attempts to connect to a mongod only listening on IPv4 may be delayed if there are both A (IPv4) and AAAA (IPv6) DNS records associated with the host.
To avoid a delay, configure hostnames to match the MongoDB configuration. That is, only create an A record if the mongod is only listening on IPv4.
π CONNECTING TO A UNIX DOMAIN SOCKET
On UNIX-like systems, the C Driver can connect directly to a MongoDB server using a UNIX domain socket. Pass the URL-encoded path to the socket, which must be suffixed with .sock. For example, to connect to a domain socket at /tmp/mongodb-27017.sock:
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://%2Ftmp%2Fmongodb-27017.sock");
Include username and password like so:
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://user:pass@%2Ftmp%2Fmongodb-27017.sock");
π₯ CONNECTING TO A SERVER OVER TLS
These are instructions for configuring TLS/SSL connections.
To run a server locally (on port 27017, for example):
$ mongod --port 27017 --tlsMode requireTLS --tlsCertificateKeyFile server.pem --tlsCAFile ca.pem
Add /?tls=true to the end of a client URI.
mongoc_client_t *client = NULL;
client = mongoc_client_new ("mongodb://localhost:27017/?tls=true");
MongoDB requires client certificates by default, unless the --tlsAllowConnectionsWithoutCertificates is provided. The C Driver can be configured to present a client certificate using the URI option tlsCertificateKeyFile, which may be referenced through the constant MONGOC_URI_TLSCERTIFICATEKEYFILE.
mongoc_client_t *client = NULL;
mongoc_uri_t *uri = mongoc_uri_new ("mongodb://localhost:27017/?tls=true");
mongoc_uri_set_option_as_utf8 (uri, MONGOC_URI_TLSCERTIFICATEKEYFILE, "client.pem");
client = mongoc_client_new_from_uri (uri);
The client certificate provided by tlsCertificateKeyFile must be issued by one of the server trusted Certificate Authorities listed in --tlsCAFile, or issued by a CA in the native certificate store on the server when omitted.
See Configuring TLS for more information on the various TLS related options.
π COMPRESSING DATA TO AND FROM MONGODB
This content has been relocated to the Data Compression page.
π ADDITIONAL CONNECTION OPTIONS
The full list of connection options can be found in the mongoc_uri_t docs.
Certain socket/connection related options are not configurable:
SO_KEEPALIVEβ TCP Keep Alive β value:EnabledTCP_KEEPIDLEβ How long a connection needs to remain idle before TCP starts sending keepalive probes β value:120 secondsTCP_KEEPINTVLβ The time in seconds between TCP probes β value:10 secondsTCP_KEEPCNTβ How many probes to send, without acknowledgement, before dropping the connection β value:9 probesTCP_NODELAYβ Send packets as soon as possible or buffer small packets (Nagle algorithm) β value:Enabled (no buffering)
πΈ AUTHOR
MongoDB, Inc
π COPYRIGHT
2017-present, MongoDB, Inc
okay that's the whole page, you ate that π β¨ this was version 1.26.0 of the libmongoc manual, dated Mar 31, 2024 β go connect to something iconic, bestie π«Άπ
π¦ where this page comes from
- ubuntu noble: libmongoc-doc 1.26.0-1.1ubuntu2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms