MongoDB replica set (Replica set) ensures high data reliability by storing copies of multiple copies of data, guaranteeing high availability of services through an automated primary and standby switching mechanism. However, when you need to be aware that the posture of the connection replica set is not correct, the service high availability will no longer exist.
What you need to know when using a copy set
The primary node in the MONGODB replication set is not fixed, when encountering a replication set rotation upgrade, primary outage, network partition and other scenarios, the replica set may elect a new primary, and the original primary will be downgraded to secondary, that is, the main standby switch occurs.
All in all, the primary node in the MongoDB replica set is not fixed, not fixed, not fixed, important things to say 3 times.
When connecting a replica set, if you specify the address of the primary directly to connect, then may be able to read and write data correctly, but once the replica set has a primary standby switch, your connected primary will be downgraded to secondary, you will not be able to continue to write operations, which will seriously affect your online services.
Therefore, the production environment should not directly connected to the primary, do not directly even primary, do not directly connected to primary.
So much to say, exactly how to connect the replica set. pose for the correct connection to the replica set
To properly connect the replica set, you need to know the connection string URI of the next MongoDB, and all the official driver support to connect MongoDB in connection String way.
Here is the main content of the connection string
mongodb://[username:password@]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]] [/[database][?options]]
The mongodb://prefix, which represents this is a connection String username:password@ If authentication is enabled, you need to specify the Ip:port information of the user password Hostx:portx the replica set member, multiple members separated by commas/ Database authentication, where is the user account? options specify additional connection options
As an example of connecting aliclouddb for MongoDB, when you purchase an Alibaba Cloud MongoDB replica set, you get the name of the replica set and the address information of the replica set members.
For user convenience, the console also generates a connection string connecting the replica set and connects to the command via the MONGO shell.
For example, through Java to connect, more demo
Mongoclienturi connectionString = new Mongoclienturi ("mongodb://root:****@ Dds-bp114e3f1fc441342.mongodb.rds.aliyuncs.com:3717,dds-bp114e3f1fc441341.mongodb.rds.aliyuncs.com:3717/admin? replicaset=mgset-677201 "); Replace with root password
mongoclient client = new Mongoclient (connectionString);
Mongodatabase database = Client.getdatabase ("MyDB");
Mongocollection collection = Database.getcollection ("Mycoll");
When you connect a MongoDB replica set with the correct connection string, the client automatically detects the primary and standby relationships of the replica set, and automatically switches the write to the new master when the primary and standby relationships change, to ensure that the service is highly available. Common Connection Parameters how to achieve read and write separation.
Add readpreference=secondarypreferred in options, read requests take precedence over secondary nodes for read and write separation, read more options refer to read Preferences How can I limit the number of connections?
Add maxpoolsize=xx to the options to limit the client connection pool to within XX. How do I ensure that data is written to most nodes before it returns?
Adding w= majority to the options ensures that the write request is successfully written to most nodes for confirmation to the client, and more write options refer to write Concern