Docs
mongodb_nvme
Quick Start
Connection in Replica Set Mode

How to Connect via Replica Set Mode?

Introduction

Connecting in replica set mode refers to the method of viewing the replica set as a whole, automatically detecting the current primary node and connecting to it. This allows the client to connect to the correct node regardless of which node in the replica set is currently the primary, and does not require extra modification to the client program after switching. In combination with the read preference parameter, it can perform “read-write separation” functions such as performing write operations on the primary database and read operations on the secondary database.

Format

In general, clients can use the MongoDB URL pattern to connect to the replica set. The format of the MongoDB URL is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

The key parameter for connecting in replica set mode is: replicaSet=xxxx (xxxx refers to the replica set name, which can be obtained from the details page of the console). As long as this parameter is specified and multiple replica set node IP addresses are specified in the IP list, the replica set mode can be connected.

Example

Assume there is a replica set on the console:

The URL address of the replica set can be found in the Access Address column. After copying, change ’****’ to the replica set’s password to establish a connection.

Replica set connections can also specify the connection user (-u), password (-p), and database (—authenticationDatabase), etc through parameters.

Realize “Read-Write Separation” Using Replica Set Connection Mode

The readPreference parameter of the replica set mode can realize the “read-write separation” function where write operations are performed on the primary database and read operations are performed on secondary database. It has the following values:

  • primary: All read requests go to primary (secondary nodes are purely used for high availability disaster recovery): This is the most classic connection method. It treats the replica set as a pure high availability cluster, and it can guarantee that the data read is the most accurate.
  • secondary: All write requests go to primary, and all read requests go to secondary. This method treats the replica set as a read-write separation cluster. It can improve the throughput of read operations (by adding multiple secondary nodes), but its disadvantage is that the data read may be inconsistent with the primary database. Therefore, be cautious in scenarios where data consistency is important.
  • primaryPreferred: Primary is preferred, and only a few cases will go to secondary to read data.
  • secondaryPreferred: Secondary is preferred, and only a few cases will go to primary to read data.
  • nearest: Select a node based on the client’s latency to each node. If the latency is within a certain threshold, a node is randomly selected, if the latency to all nodes exceeds a certain threshold, the node with the smallest latency is selected.

In the example above, the following method can be used to realize the read-write separation effect:

mongodb://root:thisispassword@10.60.52.158:27017,10.60.188.13:27017,10.60.128.181:27017/admin?replicaSet=umongodb-rs-xjnas2un&readPreference=secondaryPreferred

Reference documents: