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 the machine: first Method: Db.getmongo (). Setslaveok (); the second method: Rs.slaveok (); However, there is a disadvantage of this approach, the next time through the MONGO into the instance, the query will still error, for this can be done by the following ways
VI ~/.mongorc.js
Add a line Rs.slaveok (), so that each time through the MONGO command to enter can be queried if it is accessed through Java secondary, 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>
(original) Let MongoDB's secondary support read operation