MONGO replica set cluster mode for read and write separation

Source: Internet
Author: User

The secondary node in the replica set is not readable by default. In applications where there is less write-read, the replica sets is used to achieve read-write separation. By specifying Slaveok at the time of connection, or by specifying the secondary in the main library, the pressure of reading is shared by the primary and only the write operation is assumed.

If you access MONGO through the shell, you want to query in secondary. The following error will appear:
imageset:secondary> db.fs.files.find ()
error: {"$err": "Not Master and Slaveok=false", "Code": 13435}
There are two ways to implement a query from a machine:
The first method: Db.getmongo (). Setslaveok ();
The second method: Rs.slaveok ();
but this way has a disadvantage is that the next time through MONGO into the instance, the query will still error, for this can be done by the following ways
VI ~/.mongorc.js
add a line of Rs.slaveok ();
that way, every time you go through the MONGO command, you can query.


If the secondary is accessed through Java, the following exception will be reported
Com.mongodb.MongoException:not talking to master and retries used up
There are many ways to solve this problem.
The first method: Call Dbfactory.getdb () in Java code. SLAVEOK ();
the second approach: calling in Java code
Dbfactory.getdb (). Setreadpreference (Readpreference.secondarypreferred ());//In replication focus first read secondary, If secondary can't access it, read it from master.
or
Dbfactory.getdb (). Setreadpreference (Readpreference.secondary ()), or//Read only from secondary, and cannot be queried if the secondary is not accessible
The third method: Adding slave-ok= "true" when configuring MONGO also supports reading directly from secondary
<mongo:mongo id= "MONGO" host= "${mongodb.host}" port= "${mongodb.port}" >
<mongo:options slave-ok= "true"/>
</mongo:mongo>


This article is from the "Let Me Men Grow Together" blog, please be sure to keep this source http://wujingfeng.blog.51cto.com/5725921/1676307

MONGO replica set cluster mode for read and write separation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.