Replica sets in MongoDB store data in multiple copies for improved data reliability and support automatic primary/secondary failover for high service availability. However, the services are not highly available if a replica set is not properly connected.
The primary node in a replica set is not always the same. In cases such as replica set rotation and update, primary node breakdown, and network partitioning, the replica set may elect a new primary node and the original primary node is downgraded to a secondary node, resulting in primary/secondary failover. This leads to constant change of the primary node.
If the replica set is connected using the primary address, data can be read and written correctly until the primary node is downgraded to a secondary node in the event of primary/secondary failover, which interrupts the write operation and seriously affects online services. Therefore, do not directly connect to the replica set using the primary address in the production environment.
A connection string includes the following content:
mongodb:// is a prefix indicating a connection string.
username:password@indicates the user name and password that must be specified if authentication is enabled.
hostX:portXindicates the IP address and port number of each member in the replica set. Multiple members are separated by commas (,).
/databaseindicates the database to which the user account belongs during authentication.
?optionsindicates additional connection options.
To implement read/write splitting, add
readPreference=secondaryPreferredto options so that read requests are prioritized to secondary nodes. For more information about read options, see Read preferences.
Connections limit: Add maxPoolSize=xx to options to limit the client connection pool to a specified number (xx) of connections.
To confirm writes with the client only after data is successfully written to a majority of nodes, add w= majority to options. For more information about write options, see Write Concern.
Connection to ApsaraDB for MongoDB is used as an example here. When you buy an ApsaraDB for MongoDB replica set, you can obtain the name of the replica set and the addresses of replica set members. See the following figure.
For ease of use, the MongoDB console generates replica set connection strings and Mongo shell connection commands. See the following figure.
The following shows how to connect to ApsaraDB for MongoDB using Java.
MongoClientURI connectionString = new MongoClientURI("mongodb://root:****@dds-*****.mongodb.rds.aliyuncs.com:3717,****.aliyuncs.com:3717/admin?replicaSet=mgset-677201"); // ****Replace it with the root password.
MongoClient client = new MongoClient(connectionString);
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("mycoll");
When a replica set is connected using a correct connection string, the client automatically checks the primary/secondary relationship. If the relationship changes, writes are automatically switched to the new primary node to guarantee high service availability.