In the replica set cluster, how do i read files from GridFS of a secondary?

204 views
Skip to first unread message

一球成名

unread,
Jul 25, 2011, 9:59:12 PM7/25/11
to mongodb-user
I got an Exception "not master" when i read from the secondary.
In the GridFS structure method, when the files collections count less
then 1000, mongodb will to ensure standard indexes. but secondary is
not allow write operations.
so, how do i read files from a secondary?

Robert Stam

unread,
Jul 25, 2011, 10:23:52 PM7/25/11
to mongodb-user
Which driver are you using (and which version)?

一球成名

unread,
Jul 25, 2011, 10:28:00 PM7/25/11
to mongodb-user
mongo-java-driver-2.5.jar

On 7月26日, 上午10时23分, Robert Stam <rstam10...@gmail.com> wrote:
> Which driver are you using (and which version)?
>
> On Jul 25, 9:59 pm, 一球成名 <mache...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I got an Exception "not master" when ireadfrom the secondary.
> > In the GridFS structure method, when thefilescollections count less

Eliot Horowitz

unread,
Jul 26, 2011, 12:43:24 AM7/26/11
to mongod...@googlegroups.com
Set slaveOk on the DB you pass to GridFS constructor. 

2011/7/25 一球成名 <mach...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


一球成名

unread,
Jul 26, 2011, 1:39:54 AM7/26/11
to mongodb-user
Still get the Exception.

On 7月26日, 下午12时43分, Eliot Horowitz <el...@10gen.com> wrote:
> Set slaveOk on the DB you pass to GridFS constructor.
>
> 2011/7/25 一球成名 <mache...@gmail.com>

Eliot Horowitz

unread,
Jul 26, 2011, 1:40:57 AM7/26/11
to mongod...@googlegroups.com
Can you send the code and exact error?

2011/7/26 一球成名 <mach...@gmail.com>

一球成名

unread,
Jul 26, 2011, 1:58:36 AM7/26/11
to mongodb-user
public FileTest(String cfgPath) {
try {
MemProperties.init(cfgPath);
MongoOptions mongoOptions = new MongoOptions();
mongoOptions.slaveOk = true;
mongoOptions.connectionsPerHost =
Integer.parseInt(MemProperties
.getValue("pool_size"));
ServerAddress address = new ServerAddress(MemProperties
.getValue("mongo_ip"),
Integer.parseInt(MemProperties
.getValue("mongo_port")));
mongo = new Mongo(address, mongoOptions);
// mongo.slaveOk();

picList = ImageReader.readFromDB();
db = mongo.getDB(MemProperties.getValue("mongo_dbname"));
db.slaveOk();
} catch (Exception e) {
e.printStackTrace();
}
}

public void queryFromGridFS(int id) throws Exception {
BasicDBObject query = new BasicDBObject();
query.put("id", id);
GridFS gridFS = new GridFS(db,
MemProperties.getValue("file_bucket"));
GridFSDBFile dbFile = gridFS.findOne(query);
if (dbFile != null) {
dbFile.writeTo(new ByteArrayOutputStream());
System.out.println(gridFS.getDB().getMongo().getAddress());
} else {
System.out.println("miss");
}
// dbFile.writeTo("e:\\test\\" + dbFile.getFilename());
}

Exception in thread "main" com.mongodb.MongoException: not master
at com.mongodb.CommandResult.getException(CommandResult.java:82)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:116)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:
131)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:153)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255)
at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:
347)
at com.mongodb.DBCollection.ensureIndex(DBCollection.java:437)
at com.mongodb.DBCollection.ensureIndex(DBCollection.java:385)
at com.mongodb.gridfs.GridFS.<init>(GridFS.java:77)
at mongo.test.FileTest.queryFromGridFS(FileTest.java:139)
at mongo.test.FileTest.main(FileTest.java:63)


On 7月26日, 下午1时40分, Eliot Horowitz <el...@10gen.com> wrote:
> Can you send the code and exact error?
>
> 2011/7/26 一球成名 <mache...@gmail.com>

Nat

unread,
Jul 26, 2011, 8:29:16 AM7/26/11
to mongod...@googlegroups.com
Sounds like a bug that ensureIndex goes to secondaries. You might want to file a bug report.

一球成名

unread,
Aug 4, 2011, 2:33:13 AM8/4/11
to mongodb-user
Reply all
Reply to author
Forward
0 new messages