Connecting to a Replica Set Instance for Read and Write Separation and High Availability
DDS replica set instances can store multiple duplicates to ensure data high availability and support the automatic change of private IP addresses to ensure service high availability. To enhance the read and write performance, you can use the client to read different data copies. You are advised to use the recommended method to connect the replica set instance. Otherwise, the high availability and high read performance of the replica set instance cannot be guaranteed.
The primary node of a replica set instance is not fixed. If the instance settings are changed, or the primary node fails, or primary and secondary nodes are switched, a new primary node will be elected and the previous one becomes a secondary node. The following figure shows the process of a switchover.
(Recommended) Connecting to a Replica Set Instance
A DDS replica set consists of the primary, secondary, and hidden nodes. Hidden nodes are invisible to users. When you use a URL to connect to a replica set instance, you can connect to the IP addresses and ports of the primary and secondary nodes only.
You are advised to use the following URL to connect a replica set instance:
mongodb://rwuser:****@192.168.0.148:8635,192.168.0.96:8635/test?authSource=admin&replicaSet=replica
In the preceding URL, 192.168.0.148:8635 and 192.168.0.96:8635 indicate the IP addresses and ports of the primary and secondary nodes, respectively. If you use this method, the connectivity can be ensured even when a switchover occurs. In addition, using multiple IP addresses and port numbers can enhance the read and write performance of the entire database.
(Not Recommended) Connecting to a Replica Set Instance
Use the following URL to connect a replica set instance:
mongodb://rwuser:****@192.168.0.148:8635/test?authSource=admin&replicaSet=replica
In the preceding URL, 192.168.0.148:8635 indicates the IP address and port number of the current primary node. If a switchover occurs or the primary node is changed, the client fails to connect to the replica set instance because the IP address and port of the newly elected primary node is unknown. As a result, the database service becomes unavailable. In addition, read and write operations can be performed on a fixed primary node only. The read and write performance cannot be expanded to other nodes.
Connecting to a Replica Set Instance Using Java
Sample code:
MongoClientURI connectionString = new MongoClientURI("mongodb://rwuser:****@192.168.0.148:8635,192.168.0.96:8635/test?authSource=admin&replicaSet=replica"); MongoClient client = new MongoClient(connectionString);
MongoDatabase database = client.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("mycoll");
- rwuser:****: indicates the username and password for starting authentication.
- 192.168.0.148:8635 and 192.168.0.96:8635: indicate the IP addresses and ports of the primary and secondary nodes of the replica set instance.
- test: indicates the name of the database to be connected to.
- authSource=admin: indicates the database to which the username belongs during authentication.
- replicaSet=replica: indicates the name of the replica set instance type.
Last Article: Common Methods for Connecting to a DDS DB Instance
Next Article: Enabling Sharding on a Database
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.