Explaining Mongodb connection string parameters and options in a simple way.
Size: 1.13 MB
Language: en
Added: Mar 19, 2019
Slides: 16 pages
Slide Content
Mongodb Connection String
Connection String Format From Mongodb 3.6 onward you can specify the MongoDB connection string in two ways: the Standard Connection String Format the DNS Seedlist Connection Format
Standard Format If the username or password includes the special characters like @,:, /, or % then must use percent encoding <https://tools.ietf.org/html/rfc3986#section-2.1>. If your Mongodb is not in auth mode, then you can remove user-name & password from your connection string. mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
DNS Seed-list Format
DNS Seed-list Format Using DNS to construct the available servers list allows more flexibility of deployment and the ability to change the servers in rotation without reconfiguring clients. In order to leverage the DNS seedlist , use a connection string prefix of mongodb+srv : rather than the standard mongodb :. The + srv indicates to the client that the hostname that follows corresponds to a DNS SRV record. The driver or mongo shell will then query the DNS for the record to determine which hosts are running the mongod instances . mongodb+srv ://server.example.com/? connectTimeoutMS =300000&authSource= aDifferentAuthDB
The standard URI connection scheme mongodb ://[ username:password @]host1[:port1][,... hostN [: portN ]]][/[database][?options]] Mongodb:// --> This is a required prefix to identify that this is a string in the standard connection format. user-name:password @ --> Authentication credentials. The client will attempt to log in to the specific database using these credentials. This is an optional parameter, i.e. if Mongod is not running in auth mode then this must be ignored. host[:port] --> The host & port number where the mongod /mongos instance is running. Host may be a Hostname or an IP address. If port number is unspecified then by default port 27017 would be considered. In case of multiple Mongos( Sharded Cluster) or replicated environment you can provide multiple host:port values. /database --> The name of the database to authenticate if the connection string includes login & password. If this value is not supplied, then driver will authenticate to the admin database. ?<options> --> A query string that specifies connection specific options like ConncetionTimeout , socketTimeoutMS , SSL as <name>=<value> pairs. Options are optiional parameters.
Options
Options replicaSet : If your target Mongod is a part of replicaSet then you should specify the replicaset name. ssl : This is a boolean value(true/false) to enable or disables TLS/SSL for the connection. Default value for Standard Connection String Format is "false" while in DNS Seedlist Connection String Format it is "true". poolSize : Number of connections in the connection pool for each server instance. 5 as default(Node JS) maxPoolSize : The maximum number of connections in the connection pool. The default value is 100. minPoolSize : The minimum number of connections in the connection pool. The default value is 0. maxIdleTimeMS : The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. waitQueueMultiple : A number that the driver multiples the maxPoolSize value to, to provide the maximum number of threads allowed to wait for a connection to become available from the pool. waitQueueTimeoutMS : The maximum time in milliseconds that a thread can wait for a connection to become available. Note: These parameters and its default values vary as per the drive. So, if required please visit your driver documentation
Options(Continue) connectTimeoutMS : A value of specified milliseconds would mean the driver would wait up to specified milliseconds for a response from a MongoDB server. socketTimeoutMS : It means if the value was set to specified milliseconds the socket would close if there was no activity during a specified milliseconds window. In another words, The socketTimeoutMS sets the number of milliseconds a socket will stay inactive after the driver has successfully connected before closing. Note: Setting connectTimeoutMS and socketTimeoutMS to the value 0 has a special meaning. On the face of it, it means never timeout. However this is a truth with some modifications. Setting it to 0 actually means apply the operating system default socket timeout value. serverSelectionTimeoutMS : Specifies how long (in milliseconds) to block for server selection before throwing an exception. Default: 30,000 milliseconds. In sharded replicated environment Mongos selects the proper Primary/Secondary mongod node to redirect the read/write hence server selection time comes into picture.
Options(Continue) compressors: This feature has been introduced in Mongodb 3.6. Compressors enable network compression for communication between this client and a mongod /mongos instance. Two types of compressors- Snappy & Zlib are available. You can mention both the compressors in the option. Messages are compressed only when both parties enable network compression otherwise communication will be uncompressed. zlibCompressionLevel : If you are using zlib for network compression you can specify the compression level from -1 to 9. -1 Default compression level. 0 No compression. 1 - 9 Increasing level of compression but at the cost of speed.
Write Concern
Options(Continue)- writeConcern w: Corresponds to the write concern w Option. The w option requests acknowledgement that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. You can specify a number, the string majority, or a tag set. Values of w- -1 ignore network errors 0 no write acknowledgement 1 perform a write acknowledgement 2 perform a write acknowledgement across primary and one secondary ‘majority’ perform a write acknowledgement across the majority of servers in the replicaset ‘tag name’ perform a write acknowledgement against the replicaset tag name journal: Corresponds to the write concern j Option option . The journal option requests acknowledgement from MongoDB that the write operation has been written to the journal. (better not to set it). wtimeoutMS : Corresponds to the write concern wtimeout . wtimeoutMS specifies a time limit, in milliseconds, for the write concern. When wtimeoutMS is 0, write operations will never time out. mongodb://example1.com,example2.com,example3.com/?replicaSet=test&w=2&wtimeoutMS=2000
Options(Continue)- readConcern readConcern : Allows clients to choose a level of isolation for their reads from replica sets. Values- local: The query returns data from the instance with no guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back). This means that the data may or may not be replicated and persisted into all the replicas but still available in the directed copy of the database. available: The query returns data from the instance with no guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back). This is the default for reads against the secondaries if the reads are not associated with causally consistent sessions. majority: The query returns the data that has been acknowledged by a majority of the replica set members. The documents returned by the read operation are durable, even in the event of failure. This option basically returns the documents which are written onto the majority of the replica set members and hence is believed to be more consistent. linearizable: This is the fourth type of read concern that can be specified. This is the read level that only returns the records. This type of read level waits for the concurrent ongoing writes to be written to the respective replica sets and only then returns the data. Thus, such a read level is preferable in an environment where high data consistency is of importance. mongodb://db0.example.com,db1.example.com,db2.example.com/?replicaSet=myRepl&readConcernLevel=majority
Options(Continue)- readPreference readPreference : Describes the behavior of read operations with regards to replica sets. Values: primary: Default mode. All operations read from the current replica set primary. primaryPreferred : In most situations, operations read from the primary but if it is unavailable, operations read from secondary members. secondary: All operations read from the secondary members of the replica set. secondaryPreferred : In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary. nearest: Operations read from member of the replica set with the least network latency, irrespective of the member’s type.
Options(Continue) readPreferenceTags : A representation of a tag set as a comma-separated list of colon-separated key-value pairs, e.g. dc:ny,rack:1. Spaces should be stripped from beginning and end of all keys and values. To specify a list of tag sets, using multiple readPreferenceTags , e.g. readPreferenceTags = dc:ny maxStalenessSeconds : Replica set members can lag behind the primary due to network congestion, low disk throughput, long-running operations, etc. The read preference maxStalenessSeconds option lets you specify a maximum replication lag, or “staleness”, for reads from secondaries. When a secondary’s estimated staleness exceeds maxStalenessSeconds , the client stops using it for read operations. mongodb://mongos1.example.com,mongos2.example.com/?readPreference=secondary&readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&maxStalenessSeconds=300 appName : Specify a custom app name. The app name appears in mongod and mongos logs,db.currentOp () method and profiler output. retryWrites : Enables retriable writes. Retriable writes allow MongoDB drivers to automatically retry certain write operations a single time if they encounter network errors, or if they cannot find a healthy primary in the replica sets or sharded cluster. The default value is false. Write operations issued with a Write Concern of 0 are not retriable.