MongoDB series four: Solve the secondary read operation

Source: Internet
Author: User
Tags mongodb

http://blog.itpub.net/26812308/viewspace-2124660/

The secondary node in replica sets is not readable by default.
Use replica sets for 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.

On the secondary, visit MONGO and make a query. The following error will appear:rs:secondary> Db.sponsor.find ();
Error:error: {"OK": 0, "errmsg": "Not Master and Slaveok=false", "Code": 13435}


One, the use of the shell there are two ways to implement secondary query: 1,
> Db.getmongo (). Setslaveok ();

2.
echo "Rs.slaveok ();" > ~/.mongorc.js can be queried directly after it is connected.

Second, access secondary through Java

Exception: Com.mongodb.MongoException:not talking to master and retries used up
1.
Dbfactory.getdb (). Slaveok ();

2.
Dbfactory.getdb (). Setreadpreference (Readpreference.secondarypreferred ());
First read secondary in replication set, read from master if secondary is not accessible
Or
Dbfactory.getdb (). Setreadpreference (Readpreference.secondary ());
Only read from secondary, if secondary cannot access the query

3.
Added slave-ok= "True" when configuring MONGO, supports reading directly from secondary.

<mongo:options slave-ok= "true"/>

MongoDB series four: Solve the secondary read operation

Related Article

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.