首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。
对于replica set 中的secondary 节点默认是不可读的,
[mongodb@ligh bin]$ mongo 127.0.0.1:33333
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:33333/test
SECONDARY> db.user.find()
error: { "$err" : "not master and slaveok=false", "code" : 13435 }
SECONDARY> db.getMongo()
connection to 127.0.0.1:33333
SECONDARY> db.getMongo().setSlaveOk();
not master and slaveok=false
?
在主库上设置?slaveok=ok
[mongodb@ligh bin]$ mongo 127.0.0.1:33333
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:33333/test
PRIMARY>?db.getMongo().setSlaveOk();
PRIMARY>?
在从库进行测试
SECONDARY> db.user.find()
{ "_id" : ObjectId("4eb68b1540643e10a0000000"), "id" : 1, "name" : "zhangsan" }
{ "_id" : ObjectId("4eb68b1540643e10a0000001"), "id" : 2, "name" : ";lisi" }
另外可参考如下解决方案:
http://stackoverflow.com/questions/8990158/mongdodb-replicates-and-error-err-not-master-and-slaveok-false-code
?
转自 http://blog.163.com/lgh_2002/blog/static/44017526201301123736391/