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