ApsaraDB for MongoDB sharded cluster instances provide an endpoint for each mongos node. You can connect to any mongos node to access the databases. Use a connection string URI to achieve load balancing and high availability.
Background information

A MongoDB sharded cluster distributes data across multiple shards for high scalability. A config server stores cluster metadata, and mongos nodes serve as application entry points that read routing information and forward requests to the appropriate shard.
-
Accessing a mongos node is similar to accessing a single mongod process.
-
All mongos nodes are peers. You can access a sharded cluster through one or more mongos nodes.
-
Mongos nodes are stateless and can be scaled out. The overall service capacity is the lesser of the total shard capacity and the total mongos capacity.
-
Distribute application requests evenly across multiple mongos nodes.
About connection string URIs
To connect to a sharded cluster instance, you need to understand the MongoDB Connection String URI format. All official MongoDB drivers support this format.
Example of a connection string URI:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Parameter descriptions:
-
mongodb://: The prefix that identifies this as a connection string URI. -
username:password@: The username and password used to log on to the database. -
hostX:portX: The addresses of mongos nodes. -
/database: The authentication database. -
?options: Additional connection options.
Connect to a sharded cluster instance
ApsaraDB for MongoDB supports connecting to a sharded cluster instance by using a connection string URI, which enables automatic load balancing and high availability.
-
Obtain the connection string URI of the sharded cluster instance. For more information, see Connect to a sharded cluster instance.
-
In your application, use the connection string URI to connect to the instance. For more information, see Connect to a sharded cluster instance using code.
The following Java example connects with the database account test to the admin database:
String user = "test"; String password = "MongoDB****"; String myURI = "mongodb://" + user + ":" + password + "@s-bp1c04c07823****.mongodb.rds.aliyuncs.com:3717,s-bp1500549e0b****.mongodb.rds.aliyuncs.com:3717/admin"; MongoClient mongoClient = new MongoClient(new MongoClientURI(myURI));With this method, the client automatically distributes requests across multiple mongos nodes for load balancing. If the URI contains two or more mongos nodes and one fails, the client automatically fails over to the remaining healthy nodes.
If you have multiple mongos nodes, you can group them by application for access isolation. For example, with four mongos nodes and two applications, you can configure Application A to use mongos 1 and 2 while Application B uses mongos 3 and 4. Specify only the corresponding mongos addresses in each application's URI.
The mongos nodes that the applications access are isolated from each other, but the backend shards remain shared.
Common connection parameters
-
Implement read/write splitting
Add
readPreference=secondaryPreferredto the connection string URI options to route read requests to secondary nodes first.Example:
The database account is test and the database is admin.
mongodb://test:****@s-bp10fb1cf399****.mongodb.rds.aliyuncs.com:3717,s-bp10f49cdf5e****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondaryPreferred -
Limit the number of connections
Add
maxPoolSize=xxto the connection string URI options to limit the connection pool size per client toxx.The following example sets the connection pool size to 100. The database account is test and the database is admin:
mongodb://test:****@s-bp10fb1cf399****.mongodb.rds.aliyuncs.com:3717,s-bp10f49cdf5e****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondaryPreferred&maxpoolsize=100 -
Ensure writes are acknowledged by a majority of nodes
Add
w=majorityto the connection string URI options to require that writes are acknowledged by a majority of nodes before succeeding.Example:
The database account is test and the database is admin.
mongodb://test:****@s-bp10fb1cf399****.mongodb.rds.aliyuncs.com:3717,s-bp10f49cdf5e****.mongodb.rds.aliyuncs.com:3717/admin?readPreference=secondaryPreferred&maxpoolsize=100&w=majority