.\" Man page generated from reStructuredText. . .TH "MONGOC_ADVANCED_CONNECTIONS" "3" "Feb 23, 2019" "1.14.0" "MongoDB C Driver" .SH NAME mongoc_advanced_connections \- Advanced Connections . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp The following guide contains information specific to certain types of MongoDB configurations. .sp 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. .SH CONNECTING TO A REPLICA SET .sp Connecting to a \fI\%replica set\fP is much like connecting to a standalone MongoDB server. Simply specify the replica set name using the \fB?replicaSet=myreplset\fP URI option. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include #include 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; } .ft P .fi .UNINDENT .UNINDENT .sp \fBTIP:\fP .INDENT 0.0 .INDENT 3.5 Multiple hostnames can be specified in the MongoDB connection string URI, with a comma separating hosts in the seed list. .sp It is recommended to use a seed list of members of the replica set to allow the driver to connect to any node. .UNINDENT .UNINDENT .SH CONNECTING TO A SHARDED CLUSTER .sp To connect to a \fI\%sharded cluster\fP, specify the \fBmongos\fP nodes the client should connect to. The C Driver will automatically detect that it has connected to a \fBmongos\fP sharding server. .sp If more than one hostname is specified, a seed list will be created to attempt failover between the \fBmongos\fP instances. .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 Specifying the \fBreplicaSet\fP parameter when connecting to a \fBmongos\fP sharding server is invalid. .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include #include 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; } .ft P .fi .UNINDENT .UNINDENT .SH CONNECTING TO AN IPV6 ADDRESS .sp The MongoDB C Driver will automatically resolve IPv6 addresses from host names. However, to specify an IPv6 address directly, wrap the address in \fB[]\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C mongoc_uri_t *uri = mongoc_uri_new ("mongodb://[::1]:27017"); .ft P .fi .UNINDENT .UNINDENT .SH CONNECTING WITH IPV4 AND IPV6 .sp If connecting to a hostname that has both IPv4 and IPv6 DNS records, the behavior follows \fI\%RFC\-6555\fP\&. 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. .sp 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. .sp 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. .SH CONNECTING TO A UNIX DOMAIN SOCKET .sp 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 \fImust\fP be suffixed with \fB\&.sock\fP\&. For example, to connect to a domain socket at \fB/tmp/mongodb\-27017.sock\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C mongoc_uri_t *uri = mongoc_uri_new ("mongodb://%2Ftmp%2Fmongodb\-27017.sock"); .ft P .fi .UNINDENT .UNINDENT .sp Include username and password like so: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C mongoc_uri_t *uri = mongoc_uri_new ("mongodb://user:pass@%2Ftmp%2Fmongodb\-27017.sock"); .ft P .fi .UNINDENT .UNINDENT .SH CONNECTING TO A SERVER OVER SSL .sp These are instructions for configuring TLS/SSL connections. .sp To run a server locally (on port 27017, for example): .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ mongod \-\-port 27017 \-\-sslMode requireSSL \-\-sslPEMKeyFile server.pem \-\-sslCAFile ca.pem .ft P .fi .UNINDENT .UNINDENT .sp Add \fB/?ssl=true\fP to the end of a client URI. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C mongoc_client_t *client = NULL; client = mongoc_client_new ("mongodb://localhost:27017/?ssl=true"); .ft P .fi .UNINDENT .UNINDENT .sp MongoDB requires client certificates by default, unless the \fB\-\-sslAllowConnectionsWithoutCertificates\fP is provided. The C Driver can be configured to present a client certificate using a \fBmongoc_ssl_opt_t\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C const mongoc_ssl_opt_t *ssl_default = mongoc_ssl_opt_get_default (); mongoc_ssl_opt_t ssl_opts = { 0 }; /* optionally copy in a custom trust directory or file; otherwise the default is used. */ memcpy (&ssl_opts, ssl_default, sizeof ssl_opts); ssl_opts.pem_file = "client.pem" mongoc_client_set_ssl_opts (client, &ssl_opts); .ft P .fi .UNINDENT .UNINDENT .sp The client certificate provided by \fBpem_file\fP must be issued by one of the server trusted Certificate Authorities listed in \fB\-\-sslCAFile\fP, or issued by a CA in the native certificate store on the server when omitted. .sp To verify the server certificate against a specific CA, provide a PEM armored file with a CA certificate, or concatenated list of CA certificates using the \fBca_file\fP option, or \fBc_rehash\fP directory structure of CAs, pointed to using the \fBca_dir\fP option. When no \fBca_file\fP or \fBca_dir\fP is provided, the driver will use CAs provided by the native platform certificate store. .sp See mongoc_ssl_opt_t for more information on the various SSL related options. .SH COMPRESSING DATA TO AND FROM MONGODB .sp MongoDB 3.4 added Snappy compression support, and zlib compression in 3.6. To enable compression support the client must be configured with which compressors to use: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C mongoc_client_t *client = NULL; client = mongoc_client_new ("mongodb://localhost:27017/?compressors=snappy,zlib"); .ft P .fi .UNINDENT .UNINDENT .sp The \fBcompressors\fP option specifies the priority order of compressors the client wants to use. Messages are compressed if the client and server share any compressors in common. .sp Note that the compressor used by the server might not be the same compressor as the client used. For example, if the client uses the connection string \fBcompressors=zlib,snappy\fP the client will use \fBzlib\fP compression to send data (if possible), but the server might still reply using \fBsnappy\fP, depending on how the server was configured. .sp The driver must be built with zlib and/or snappy support to enable compression support, any unknown (or not compiled in) compressor value will be ignored. .SH ADDITIONAL CONNECTION OPTIONS .sp The full list of connection options can be found in the \fBmongoc_uri_t\fP docs. .sp Certain socket/connection related options are not configurable: .TS center; |l|l|l|. _ T{ Option T} T{ Description T} T{ Value T} _ T{ SO_KEEPALIVE T} T{ TCP Keep Alive T} T{ Enabled T} _ T{ TCP_KEEPIDLE T} T{ How long a connection needs to remain idle before TCP starts sending keepalive probes T} T{ 300 seconds T} _ T{ TCP_KEEPINTVL T} T{ The time in seconds between TCP probes T} T{ 10 seconds T} _ T{ TCP_KEEPCNT T} T{ How many probes to send, without acknowledgement, before dropping the connection T} T{ 9 probes T} _ T{ TCP_NODELAY T} T{ Send packets as soon as possible or buffer small packets (Nagle algorithm) T} T{ Enabled (no buffering) T} _ .TE .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .